— Created 2024-06-28 by Takaaki Naganoya use AppleScript version "2.5" use scripting additions use framework "Foundation" use framework "AppKit" use mLib : script "calcLibAS" use imgDispLib : script "imageDisplayLib"
property NSColor : a reference to current application’s NSColor property NSString : a reference to current application’s NSString property NSImage : a reference to current application’s NSImage property NSScreen : a reference to current application’s NSScreen property NSBezierPath : a reference to current application’s NSBezierPath property NSBitmapImageRep : a reference to current application’s NSBitmapImageRep
–パラメータから下地になる画像を作成する set aSize to current application’s NSMakeSize(300, 300) set anImage to NSImage’s alloc()’s initWithSize:aSize set backlColor to (NSColor’s colorWithCalibratedRed:0.0 green:0.0 blue:0.0 alpha:1.0) set anImage to drawImageWithColorFill(anImage, {0, 0, 300, 300}, backlColor) of me
–描画色 set drawColor to (NSColor’s colorWithCalibratedRed:0.0 green:1.0 blue:0.0 alpha:1.0)
–Mexican Hat set D to {} set rDnum to pi / 180
repeat 256 times set end of D to 100 end repeat
repeat with y from -180 to 180 by 6 repeat with x from -180 to 180 by 4 set u to 120 + (x / 3) – (y / 6) if u < 0 or u ≥ 240 then — else set r to rDnum * (sqrt ((x ^ 2) + (y ^ 2))) set z to 100 * (cos (r – 30)) * (cos (3 * r)) set v to 40 – y / 6 – z / 2
if (item u of D) > v then set anImage to psetOnImage(anImage, {u, 256 – v}, drawColor) of me copy v to (item u of D) end if end if end repeat end repeat
–結果表示 dispImage(anImage, "Mexican Hat") of imgDispLib
on psetOnImage(anImage, posDat, fillColor) set retinaF to (NSScreen’s mainScreen()’s backingScaleFactor()) as real –> 2.0 (Retina) / 1.0 (Non Retina)
anImage’s lockFocus() –描画開始
set origX to (item 1 of posDat) / retinaF set origY to (item 2 of posDat) / retinaF set sizeX to (1) / retinaF set sizeY to (1) / retinaF
set theRect to {{x:origX, y:origY}, {width:sizeX, height:sizeY}}
set theNSBezierPath to NSBezierPath’s bezierPath (theNSBezierPath’s appendBezierPathWithRect:theRect)
return anImage –returns NSImage end psetOnImage
on drawImageWithColorFill(anImage, drawList, fillColor) set retinaF to (NSScreen’s mainScreen()’s backingScaleFactor()) as real –> 2.0 (Retina) / 1.0 (Non Retina)
anImage’s lockFocus() –描画開始
set origX to (item 1 of drawList) / retinaF set origY to (item 2 of drawList) / retinaF set sizeX to (item 3 of drawList) / retinaF set sizeY to (item 4 of drawList) / retinaF
set theRect to {{x:origX, y:origY}, {width:sizeX, height:sizeY}}
set theNSBezierPath to NSBezierPath’s bezierPath (theNSBezierPath’s appendBezierPathWithRect:theRect)
set dList to name of every document –> {"ゆっくり素材れいむ.pxd", "ゆっくり素材まりさ.pxd"}
tell front document set d1Name to name of it if d1Name contains "れいむ" then set d2Name to retAnItemByCond(dList, "れいむ") of me else set d2Name to retAnItemByCond(dList, "まりさ") of me end if
tell layer "root" tell layer "顔" set dList to every layer repeat with i in dList set j to contents of i tell j set ddList to (name of every layer whose visible is true) if ddList is not equal to {} then set aCon to contents of first item of ddList set the end of allD to {"root", "顔", name of j, aCon} end if end tell end repeat end tell end tell end tell
tell document d2Name repeat with i in allD copy i to {L1, L2, L3, L4} tell layer L3 of layer L2 of layer L1 set tmpL to name of every layer repeat with ii in tmpL set jj to contents of ii tell layer jj if jj = L4 then set visible to true else set visible to false end if end tell end repeat end tell end repeat end tell end tell
on retAnItemByCond(aList, aParam) repeat with i in aList set j to contents of i
if j does not contain aParam then return j end if end repeat end retAnItemByCond
repeat with i in allDocs set j to contents of i set dRes to exportKeynoteDocToPDF(j) of me set pdfPath to (POSIX path of dRes) set pdfPath to chkExistPOSIXpathAndIncrementChildNumberFull(pdfPath) of me set jRes to (my jpegFromPath:(pdfPath) compressFactor:1.0) set the end of outPathList to jRes end repeat end tell
return outPathList
–Keynote書類からPDF書き出し on exportKeynoteDocToPDF(targKeynoteDoc)
tell application "Keynote" set dCount to count every document if dCount = 0 then –オープン中のKeynote書類がない場合はリターン return false end if
set aPath to file of targKeynoteDoc end tell
set tmpPath to (path to desktop) as string set curPath to (NSString’s stringWithString:(POSIX path of aPath))’s lastPathComponent()’s stringByDeletingPathExtension()’s stringByAppendingString:".pdf" set outPath to (tmpPath & curPath) set outPath to chkExistPOSIXpathAndIncrementChildNumberFull(outPath) of me –do shell script "touch " & quoted form of POSIX path of outPath –Error 6を回避するための記述
tell application "Keynote" –set anOpt to {class:export options, export style:IndividualSlides, all stages:false, skipped slides:true, PDF image quality:Good} –set anOpt to {class:export options, export style:IndividualSlides, all stages:false, skipped slides:true, PDF image quality:Better} set anOpt to {class:export options, export style:IndividualSlides, all stages:false, skipped slides:true, PDF image quality:Best} export targKeynoteDoc to POSIX file outPath as PDF with properties anOpt end tell
return ((POSIX file outPath) as alias) end exportKeynoteDocToPDF
on jpegFromPath:imagePath compressFactor:compFactor — 0.0 = max compression, 1.0 = none — build destination path set pathNSString to current application’s NSString’s stringWithString:imagePath set destPath to pathNSString’s stringByDeletingPathExtension()’s stringByAppendingPathExtension:"jpg"
— load the file as an NSImage set theImage to current application’s NSImage’s alloc()’s initWithContentsOfFile:imagePath if theImage = missing value then return false
set theData to theImage’s TIFFRepresentation() set newRep to current application’s NSBitmapImageRep’s imageRepWithData:theData set theData to (newRep’s representationUsingType:(current application’s NSJPEGFileType) |properties|:{NSImageCompressionFactor:compFactor, NSImageProgressive:false})
set theResult to (theData’s writeToFile:destPath atomically:true) as boolean return destPath as string end jpegFromPath:compressFactor:
–POSIX path stringを与えると、ファイル名の重複を検出して、ファイル名の名称回避を行って、ファイル名のみを返す on chkExistPOSIXpathAndIncrementChildNumberFull(a) set aa to POSIX path of a set aStr to NSString’s stringWithString:aa set bStr to aStr’s lastPathComponent()
set cStr to (bStr’s pathExtension()) as string set dStr to (bStr’s stringByDeletingPathExtension()) as string set eStr to (aStr’s stringByDeletingLastPathComponent()) as string
set aManager to NSFileManager’s defaultManager() set aRes to (aManager’s fileExistsAtPath:aStr) as boolean if aRes = false then return aa
set hitF to false repeat with i from 1 to 65535
set tmpPath to (eStr & "/" & dStr & "_" & (i as string) & "." & cStr) set tmpStr to (NSString’s stringWithString:tmpPath) set aRes to (aManager’s fileExistsAtPath:tmpStr) as boolean set bRes to ((tmpStr’s caseInsensitiveCompare:eStr) is not equal to (NSOrderedSame)) as boolean
if {aRes, bRes} = {false, true} then set hitF to true exit repeat end if
end repeat
if hitF = false then return false
–ファイルパス(フルパス)を返す return (tmpStr as string) end chkExistPOSIXpathAndIncrementChildNumberFull
set paramRec to NSMutableDictionary’s dictionary()
repeat with i in qList set keyAndValues to (i’s componentsSeparatedByString:"=") (paramRec’s setObject:(keyAndValues’s objectAtIndex:1) forKey:(keyAndValues’s objectAtIndex:0)) end repeat
return paramRec end parseURLParamsAsDict
–NSImageを指定パスにPNG形式で保存 on saveNSImageAtPathAsPNG(anImage, outPath) set imageRep to anImage’s TIFFRepresentation() set aRawimg to NSBitmapImageRep’s imageRepWithData:imageRep
set pathString to NSString’s stringWithString:outPath set newPath to pathString’s stringByExpandingTildeInPath()
set myNewImageData to (aRawimg’s representationUsingType:(NSPNGFileType) |properties|:(missing value)) set aRes to (myNewImageData’s writeToFile:newPath atomically:true) as boolean
return aRes –成功ならtrue、失敗ならfalseが返る end saveNSImageAtPathAsPNG
set targFrame to current application’s NSMakeRect(0, 0, newWidth, newheight) set targImage to current application’s NSImage’s alloc()’s initWithSize:{newWidth, newheight}
set aKey to {current application’s NSImageHintInterpolation} set aObj to current application’s NSNumber’s numberWithInt:(current application’s NSImageInterpolationLow) set hintRec to current application’s NSDictionary’s dictionaryWithObject:aObj forKey:aKey
return targImage end resizeNSImage
–NSImageを指定パスにPNG形式で保存 on saveNSImageAtPathAsPNG(anImage, outPath) set imageRep to anImage’s TIFFRepresentation() set aRawimg to current application’s NSBitmapImageRep’s imageRepWithData:imageRep set pathString to current application’s NSString’s stringWithString:outPath set newPath to pathString’s stringByExpandingTildeInPath() set myNewImageData to (aRawimg’s representationUsingType:(current application’s NSPNGFileType) |properties|:(missing value)) set aRes to (myNewImageData’s writeToFile:newPath atomically:true) as boolean return aRes –成功ならtrue、失敗ならfalseが返る end saveNSImageAtPathAsPNG
# 色指定 set aColor to my makeNSColorFromRGBA255val(252, 2, 128, 255)
# 同じ大きさの塗りつぶし画像を作成 tell current application’s NSImage’s alloc() tell initWithSize_(sourceImg’s |size|()) lockFocus() aColor’s |set|() set aRect to current application’s NSMakeRect(0, 0, its |size|()’s width, its |size|()’s height) current application’s NSBezierPath’s fillRect:aRect unlockFocus() set fillImg to it end tell end tell
# NSImage –> CIimage set sourceImage to my convNSImageToCIimage(sourceImg) set fillImage to my convNSImageToCIimage(fillImg)
set newImage to my filterImage2(fillImage, sourceImage)
my restoreClipboard({newImage}) end main
on filterImage2(fillImage, sourceImage)
set redValue to 0.295719844358 set greenValue to 0.295719844358 set blueValue to 0.295719844358 set alphaVlaue to 1.0 set aCIColor to current application’s CIColor’s alloc()’s initWithRed:redValue green:greenValue blue:blueValue alpha:alphaVlaue
tell current application’s CIFilter — CIFilter(モノクロ化) tell filterWithName_("CIColorMonochrome") setDefaults() setValue_forKey_(sourceImage, "inputImage") setValue_forKey_(aCIColor, "inputColor") set filteredImage to valueForKey_("outputImage") end tell
— CIFilter(モノクロ化) tell filterWithName_("CIAdditionCompositing") setDefaults() setValue_forKey_(fillImage, "inputImage") setValue_forKey_(filteredImage, "inputBackgroundImage") set theCIImage to valueForKey_(current application’s kCIOutputImageKey) end tell end tell
# CIImage –> NSImage set imageRep to current application’s NSCIImageRep’s alloc()’s initWithCIImage:theCIImage set rNSImage to current application’s NSImage’s alloc()’s initWithSize:(imageRep’s |size|()) rNSImage’s addRepresentation:imageRep
return rNSImage end filterImage2
# NSImage –> CIimage on convNSImageToCIimage(aNSImage) set tiffDat to aNSImage’s TIFFRepresentation() set aRep to current application’s NSBitmapImageRep’s imageRepWithData:tiffDat set newImg to current application’s CIImage’s alloc()’s initWithBitmapImageRep:aRep return newImg end convNSImageToCIimage
— クリップボードの内容をNSImageとして取り出して返す on getClipboardASImage() set thePasteboard to current application’s NSPasteboard’s generalPasteboard() set theAttributedStringNSArray to thePasteboard’s readObjectsForClasses:({current application’s NSImage}) options:(missing value) set theNSImage to theAttributedStringNSArray’s objectAtIndex:0 return theNSImage end getClipboardASImage
on restoreClipboard(theArray as list) set thePasteboard to current application’s NSPasteboard’s generalPasteboard() thePasteboard’s clearContents() thePasteboard’s writeObjects:theArray end restoreClipboard
on makeNSColorFromRGBA255val(redValue as integer, greenValue as integer, blueValue as integer, alphaValue as integer) set aRedCocoa to (redValue / 255) as real set aGreenCocoa to (greenValue / 255) as real set aBlueCocoa to (blueValue / 255) as real set aAlphaCocoa to (alphaValue / 255) as real set aColor to current application’s NSColor’s colorWithCalibratedRed:aRedCocoa green:aGreenCocoa blue:aBlueCocoa alpha:aAlphaCocoa return aColor end makeNSColorFromRGBA255val
set iRes to checkImageIsWhite(i) of blankImageKit of me
set b1Dat to current application’s NSDate’s timeIntervalSinceReferenceDate() set c1Dat to b1Dat – a1Dat
set aName to (current application’s NSString’s stringWithString:i)’s lastPathComponent() as string
if iRes = true then set jRes to "White" else set jRes to "Black" end if
–log {aName, jRes}
set the end of aList to {aName, jRes, c1Dat} end repeat
set fLabels to {"File name", "Result", "Estimate(Seconds)"} set aRes to (display table by list aList main message "Blank Image check" size {800, 600} labels fLabels) return aList
script blankImageKit use AppleScript version "2.7" — High Sierra (10.13) or later use framework "Foundation" use framework "AppKit" use scripting additions property parent : AppleScript
property NSData : a reference to current application’s NSData property |NSURL| : a reference to current application’s |NSURL| property NSColor : a reference to current application’s NSColor property NSImage : a reference to current application’s NSImage property NSBezierPath : a reference to current application’s NSBezierPath property NSBitmapImageRep : a reference to current application’s NSBitmapImageRep
–Compare Original Data and on checkImageIsWhite(aFile) set aPOSIXpath to POSIX path of aFile set aURL to |NSURL|’s fileURLWithPath:(aPOSIXpath)
set aNSImage to NSImage’s alloc()’s initWithContentsOfURL:(aURL) set bNSImage to NSImage’s alloc()’s initWithContentsOfURL:(aURL)
set fillColor1 to NSColor’s clearColor() set blankNSImage1 to drawImageWithFilledColor(aNSImage, fillColor1) of me
set fillColor2 to makeNSColorFromRGBAval(65535, 65535, 65535, 65535, 65535) of me –white set blankNSImage2 to drawImageWithFilledColor(bNSImage, fillColor2) of me
set aTiff to blankNSImage1’s TIFFRepresentation() set bTiff to blankNSImage2’s TIFFRepresentation()
set chkWhite to (aTiff’s isEqualToData:bTiff) as boolean
return chkWhite end checkImageIsWhite
on getSizeOfImage(anNSImage) set aSize to anNSImage’s |size|() set aClass to class of aSize if aClass = record then copy aSize to theSize –To macOS 10.12.x else –macOS 10.13 or later set sizeX to (item 1 of item 2 of aSize) set sizeY to (item 2 of item 2 of aSize) set theSize to {width:sizeX, height:sizeY} end if return theSize end getSizeOfImage
–指定サイズの画像を作成し、背景を指定色で塗る on drawImageWithFilledColor(anImage, fillColor) set aSize to getSizeOfImage(anImage) of me
anImage’s lockFocus()
set theRect to {{x:0, y:0}, {width:(width of aSize), height:(height of aSize)}} set theNSBezierPath to NSBezierPath’s bezierPath theNSBezierPath’s appendBezierPathWithRect:theRect fillColor’s |set|() theNSBezierPath’s fill()
anImage’s unlockFocus()
return anImage end drawImageWithFilledColor
–aMaxValを最大値とする数値でNSColorを作成して返す on makeNSColorFromRGBAval(redValue as integer, greenValue as integer, blueValue as integer, alphaValue as integer, aMaxVal as integer) set aRedCocoa to (redValue / aMaxVal) as real set aGreenCocoa to (greenValue / aMaxVal) as real set aBlueCocoa to (blueValue / aMaxVal) as real set aAlphaCocoa to (alphaValue / aMaxVal) as real set aColor to NSColor’s colorWithCalibratedRed:aRedCocoa green:aGreenCocoa blue:aBlueCocoa alpha:aAlphaCocoa return aColor end makeNSColorFromRGBAval
–ファイルパスの加工処理 set newImgName to ((current application’s NSString’s stringWithString:(POSIX path of aLut))’s lastPathComponent()’s stringByDeletingPathExtension()’s stringByAppendingString:".jpg") as string set newImgFullPath to (imgParent as string) & "/" & newImgName set newImgFile to POSIX file newImgFullPath
tell application "Pixelmator Pro" activate tell front document tell color adjustments of first layer –ここだけ、ネスティングを分割するとエラーになる set its custom lut to aLut end tell
export to newImgFullPath as JPEG with properties {compression factor:0.5, bits per channel:8} undo end tell end tell end repeat
–後片付け tell application "Pixelmator Pro" tell front document close without saving end tell end tell
on getParentPathFromAlias(aliasPath) set aPath to POSIX path of aliasPath set pathString to current application’s NSString’s stringWithString:aPath set newPath to pathString’s stringByDeletingLastPathComponent() return newPath end getParentPathFromAlias
on getFilesWithUTI(acceptUTI, aliasList) set aList to {}
repeat with i in aliasList set anAlias to i as alias set aUTI to getUTIfromPath(anAlias) of me if aUTI is not equal to missing value then set uRes to filterUTIList({aUTI}, acceptUTI) of me
if uRes is not equal to {} then set the end of aList to contents of i end if end if end repeat
return aList end getFilesWithUTI
–AliasからUTIを求める on getUTIfromPath(anAlias) set aPOSIXpath to POSIX path of anAlias set aURL to current application’s |NSURL|’s fileURLWithPath:aPOSIXpath if aURL = missing value then return missing value set aRes to aURL’s resourceValuesForKeys:{current application’s NSURLTypeIdentifierKey} |error|:(missing value) if aRes = missing value then return missing value return (aRes’s NSURLTypeIdentifierKey) as string end getUTIfromPath
–UTIリストが指定UTIに含まれているかどうか演算を行う on filterUTIList(aUTIList, aUTIstr) set anArray to NSArray’s arrayWithArray:aUTIList set aPred to NSPredicate’s predicateWithFormat_("SELF UTI-CONFORMS-TO %@", aUTIstr) set bRes to (anArray’s filteredArrayUsingPredicate:aPred) as list return bRes end filterUTIList
AppleScript用語辞書的には、「export as lut」コマンドを新設したことと、「color adjustments」コマンドに「custom lut」を指定するオプションを追加しています。
AppleScript名:現在オープン中の写真に指定のLUTを反映させる.scpt
–Pixelmator Pro用語辞書内に掲載されているサンプルを、読みやすいように書き換えた set lutFile to choose file with prompt "Choose the LUT you’d like to apply to the layer:" of type {"com.blackmagicdesign.cube"}
tell application "Pixelmator Pro" tell front document tell color adjustments of first layer –ここだけ、ネスティングを分割するとエラーになる set its custom lut to lutFile end tell end tell end tell
–新規保存ファイルパスの作成 set pathStr to POSIX path of i set filenameStr to (current application’s NSString’s stringWithString:(pathStr))’s lastPathComponent() as string set savePath to outFolStr & filenameStr
tell application "Pixelmator Pro" save front document in file savePath as PNG
tell front document close without saving end tell end tell end repeat
set aFile to choose file of type {"public.svg-image"} set outPath to choose file name with prompt "select output file name"
tell application "Gapplin" open aFile
tell front document export to outPath as PNG with options {class:export options, scale:1.0} close without saving end tell end tell
▲実行後(Mac mini 2014で処理に初回で0.1秒ほど。2回目以降は1桁速くなる) AppleScript側で画像を作成してフィルタ処理をかけたりして、好き放題に合成してFileMaker DB上のフィールドに設定できる。バーコードやQRコードをAppleScript側で生成して設定したり、PDFをレンダリングした画像を設定できるのは便利
AppleScript名:NSImage to FM Object Field v2.scptd
— Created 2020-12-20 by Takaaki Naganoya — 2020 Piyomaru Software use AppleScript version "2.4" use scripting additions use framework "Foundation" use framework "AppKit"
property fmBundleID : ""
set fmBundleID to getBundleID() of me
–DB側から画像化するテキストを取得する using terms from application "FileMaker Pro" tell application ID fmBundleID tell window 1 tell current record set aText to field "originalString" end tell end tell end tell end using terms from
set aWidth to 500.0 –幅 set aHeight to 200.0 –高さ
set fillColor to current application’s NSColor’s blackColor –塗り色 set drawColor to current application’s NSColor’s whiteColor –文字色
set {xPos, yPos} to {1, 5}
–新規画像を作成して背景を塗る set aImage1 to makeImageWithFilledColor(aWidth, aHeight, fillColor) of me
–画像に文字を塗る set aImage2 to drawStringsOnImage(aImage1, aText, "HiraKakuStd-W8", 36.0, drawColor, xPos, yPos) of me
(
my restoreClipboard:{aImage2}) using terms from scripting additions set tmpImg to the clipboard as JPEG picture end using terms from
–DBに画像を設定する using terms from application "FileMaker Pro" tell application ID fmBundleID tell window 1 tell current record set field "imageStore" to tmpImg end tell end tell end tell end using terms from
–画像のうえに指定の文字を描画して画像を返す on drawStringsOnImage(anImage, aText, aFontName, aPoint, drawColor, xPos, yPos) set aString to current application’s NSString’s stringWithString:aText set aDict to current application’s NSDictionary’s dictionaryWithObjects:{current application’s NSFont’s fontWithName:aFontName |size|:aPoint, drawColor} forKeys:{current application’s NSFontAttributeName, current application’s NSForegroundColorAttributeName}
return anImage end drawStringsOnImage
–指定サイズの画像を作成し、背景を指定色で塗る on makeImageWithFilledColor(aWidth, aHeight, fillColor) –Imageの作成 set anImage to current application’s NSImage’s alloc()’s initWithSize:(current application’s NSMakeSize(aWidth, aHeight))
–描画開始 anImage’s lockFocus()
set theRect to {{x:0, y:0}, {width:aWidth, height:aHeight}} set theNSBezierPath to current application’s NSBezierPath’s bezierPath theNSBezierPath’s appendBezierPathWithRect:theRect
return anImage –画像を返す end makeImageWithFilledColor
on getBundleID() –FileMaker Pro 18 set fmProID to "com.filemaker.client.pro12"
–FileMaker Pro 18 Advanced set fmProADVID to "com.filemaker.client.advanced12"
using terms from application "FileMaker Pro" tell current application set aName to name end tell end using terms from
if aName contains "Advanced" then return fmProADVID else return fmProID end if end getBundleID
–クリップボードに内容を設定する on restoreClipboard:theArray — get pasteboard set thePasteboard to current application’s NSPasteboard’s generalPasteboard()
— clear it, then write new contents thePasteboard’s clearContents() thePasteboard’s writeObjects:theArray end restoreClipboard:
tell the front document set aWidth to width set aHeight to height
if {aWidth, aHeight} is not equal to {1024.0, 1024.0} then display dialog "Wrong Image Size (1024×1024 required)" buttons {"OK"} default button 1 with icon 2 with title "Size Error" return end if
repeat with i in resolList resize image width i height i resolution 72 algorithm bilinear export to file (aTargFileBase & "_" & (i as string) & "x" & (i as string) & ".png") as PNG undo end repeat end tell end tell
— Created 2016-03-12 by Takaaki Naganoya — Modified 2019-02-27 by Takaaki Naganoya — Modified 2020–09-21 by Takaaki Naganoya — 2020 Piyomaru Software use AppleScript version "2.4" use scripting additions use framework "Foundation" use framework "AppKit" use framework "SFPSDWriter" –https://github.com/shinyfrog/SFPSDWriter
property |NSURL| : a reference to current application’s |NSURL| property NSString : a reference to current application’s NSString property NSUUID : a reference to current application’s NSUUID property NSImage : a reference to current application’s NSImage property SFPSDWriter : a reference to current application’s SFPSDWriter property NSWorkspace : a reference to current application’s NSWorkspace
property SFPSDResolutionUnitPPI : 1 property SFPSDResolutionUnitPPC : 2
set anWriter to (SFPSDWriter’s alloc()’s initWithDocumentSize:(current application’s CGSizeMake(1200, 400)) andResolution:72.0 andResolutionUnit:(SFPSDResolutionUnitPPI))
set aCount to 1
repeat with yPos from 0 to 400 by 110 repeat with xPos from 0 to 1024 by 110 set aImage to (current application’s NSImage’s alloc()’s initWithSize:{100, 100}) set aColor to current application’s NSColor’s redColor() my drawCircleOnNSIMage(aImage, 100, 0, 0, aColor)
(anWriter’s addLayerWithNSImage:aImage andName:("Layer_" & aCount as string) andOpacity:1.0 andOffset:{x:(xPos as real), y:(yPos as real)}) set aCount to aCount + 1 end repeat end repeat
set aPSD to anWriter’s createPSDData()
set theName to NSUUID’s UUID()’s UUIDString() set pathString to NSString’s stringWithString:("~/Desktop/output_" & theName & ".psd") set newPath to pathString’s stringByExpandingTildeInPath()
aPSD’s writeToFile:newPath atomically:true
# MARK: Call By Reference on drawCircleOnNSIMage(aImage, aRadius, aXpos, aYpos, aColor) set aBezier to generateCircle(aRadius, aXpos, aYpos) of me (aImage)’s lockFocus() aColor’s |set|() aBezier’s fill() –ぬりつぶし (aImage)’s unlockFocus() end drawCircleOnNSIMage
# MARK: circleのBezier曲線を作成して返す on generateCircle(theRadius, x, y) set aRect to current application’s NSMakeRect(x, y, theRadius, theRadius) set aCirCle to current application’s NSBezierPath’s bezierPath() aCirCle’s appendBezierPathWithOvalInRect:aRect return aCirCle end generateCircle
tell front document set myPath to (file of it) if myPath = missing value then return false –Unsaved
tell current slide tell image 1 set aFile to file name –> "switchControl.png" end tell end tell end tell end tell
set myPOSIXPath to POSIX path of (myPath as alias)
–Extract document internal image paths by extracting Keynote’s zip archive set dList to getInternalImagesWithinKeynote(myPath) of me
–Extract file name and ext from image 1 path set pPath to POSIX path of aFile set {aName, aExt} to getExtAndFilenameFromPath(pPath) of me
–Filter Keynote internal image list by image 1’s file name and ext set theArray to current application’s NSArray’s arrayWithArray:dList set thePred to current application’s NSPredicate’s predicateWithFormat_("self.pathExtension == %@ && self.lastPathComponent beginswith %@ ", aExt, aName) set bList to (theArray’s filteredArrayUsingPredicate:thePred) as list –> {"Data/switchControl-226.png", "Data/switchControl-small-227.png"}
–Extrat file set exF to false
repeat with i in bList set outName to getFilenameAndExtFromPath(i) of me
if outName does not contain "-small-" then –Skip Preview Image set outPath to POSIX path of (path to desktop) & outName set bRes to do shell script "unzip -j " & quoted form of myPOSIXPath & " " & quoted form of i & " -d " & " " & outPath
set extractPath to POSIX path of (path to desktop) & outName & "/" & outName do shell script "open " & quoted form of extractPath –Open for test
set exF to true end if end repeat
if exF = false then return false
–Keynote書類中の画像ファイル一覧を取得 on getInternalImagesWithinKeynote(myPath) set kPath to POSIX path of myPath set aRes to do shell script "unzip -Z1 " & quoted form of kPath & " | grep ^Data/" set aList to paragraphs of aRes return aList end getInternalImagesWithinKeynote
–ファイルパスからファイル名部分と拡張子を分離 on getExtAndFilenameFromPath(aPOSIXpath) set pathString to current application’s NSString’s stringWithString:aPOSIXpath set aStr to pathString’s lastPathComponent() set aExt to (aStr’s pathExtension()) set bStr to aStr’s stringByDeletingPathExtension() return {bStr as string, aExt as string} end getExtAndFilenameFromPath
–ファイルパスからファイル名部分のみ取得 on getFilenameAndExtFromPath(aPOSIXpath) set pathString to current application’s NSString’s stringWithString:aPOSIXpath set aStr to pathString’s lastPathComponent() return aStr as string end getFilenameAndExtFromPath