Finderの最前面のWindowがアイコン表示になっている場合に、Window(Folder)内の画像ファイルをFinder上でのアイコン位置情報(X座標のみ)を考慮して横方向に1枚ものの画像に結合するAppleScriptです。
Blog掲載時に利用するために、Finder上で選択中の複数の画像ファイルを1枚ものの画像に(横方向/縦方向に)連結するAppleScriptを便利に使っています(ただし、「動けばいいや」程度で雑に作ったので、普段よりもやっつけ度がかなり高い、、、)。
ただし、画像の並び順については「たぶん、作成日時の古い順」に連結されるといった具合に明示的に並び順を指定できるものではなかったので、本Scriptを作ってみました。 –> Demo Movie
Finder上でicon viewに指定したWindow(Folder)内でLeft–>Rightの順に(X座標のみ評価)座標値をソートして連結します。
AppleScript名:Finderの最前面のアイコン表示ウィンドウ上の画像をX座標の情報をキーにして横連結 |
— Created 2018-04-10 by Takaaki Naganoya — 2018 Piyomaru Software use AppleScript version "2.5" use scripting additions use framework "Foundation" use framework "QuartzCore" use framework "AppKit" use bPlus : script "BridgePlus" –https://www.macosxautomation.com/applescript/apps/BridgePlus.html property |NSURL| : a reference to current application’s |NSURL| property NSUUID : a reference to current application’s NSUUID property NSArray : a reference to current application’s NSArray property NSString : a reference to current application’s NSString property NSImage : a reference to current application’s NSImage property NSWorkspace : a reference to current application’s NSWorkspace property NSPNGFileType : a reference to current application’s NSPNGFileType property NSMutableArray : a reference to current application’s NSMutableArray property NSBitmapImageRep : a reference to current application’s NSBitmapImageRep property NSURLTypeIdentifierKey : a reference to current application’s NSURLTypeIdentifierKey property xGap : 10 –連結時の画像間のアキ(横方向) load framework tell application "Finder" tell front window set curView to current view if curView is not equal to icon view then display dialog "最前面のウィンドウがアイコン表示になっていないので、処理を終了します。" return end if try set posList to position of every file whose kind contains "イメージ" –"イメージ" is "image" or "picture" in Japanese set fileList to (every file whose kind contains "イメージ") as alias list –"イメージ" is "image" or "picture" in Japanese on error display dialog "最前面のウィンドウに画像ファイルが配置されていないため、処理を中止します。" return end try end tell set aList to {} set aLen to length of posList set bLen to length of fileList if aLen is not equal to bLen then return repeat with i from 1 to aLen set posItem to contents of item i of posList set aPath to POSIX path of item i of fileList set the end of aList to (posItem & aPath) end repeat end tell –> {{127, 42, "/Users/me/Desktop/名称未設定フォルダ/000_keynote_print.png"}, {302, 43, "/Users/me/Desktop/名称未設定フォルダ/999_indesign_print.png"}} set bList to sort2DListAscendingWithFirstItemKey(aList) of me –> {{127, 42, "/Users/me/Desktop/名称未設定フォルダ/000_keynote_print.png"}, {302, 43, "/Users/me/Desktop/名称未設定フォルダ/999_indesign_print.png"}} set cList to getEveryIndicatedItemsFrom2DList(bList, 3) of me –> {"/Users/me/Desktop/名称未設定フォルダ/000_keynote_print.png", "/Users/me/Desktop/名称未設定フォルダ/999_indesign_print.png"} –Finder最前面の画像ファイルをpathからImageを読み込んでArrayに入れる set imgList to NSMutableArray’s new() repeat with i in cList set aPath to contents of i set imgRes to (my isImageAtPath:aPath) if imgRes as boolean = true then set aNSImage to (NSImage’s alloc()’s initWithContentsOfFile:aPath) (imgList’s addObject:aNSImage) end if end repeat –KVCで画像の各種情報をまとめて取得 set sizeList to (imgList’s valueForKeyPath:"size") as list –NSSize to list of record conversion set maxHeight to ((NSArray’s arrayWithArray:sizeList)’s valueForKeyPath:"@max.height") as real set totalWidth to ((NSArray’s arrayWithArray:sizeList)’s valueForKeyPath:"@sum.width") as real set totalCount to ((NSArray’s arrayWithArray:sizeList)’s valueForKeyPath:"@count") as integer –出力画像作成 set tSize to current application’s NSMakeSize((totalWidth + (xGap * totalCount)), maxHeight) set newImage to NSImage’s alloc()’s initWithSize:tSize –順次画像を新規画像に上書き set xOrig to 0 repeat with i in (imgList as list) set j to contents of i set curSize to j’s |size|() set aRect to {xOrig, (maxHeight – (curSize’s height())), (curSize’s width()), (curSize’s height())} set newImage to composeImage(newImage, j, aRect) of me set xOrig to xOrig + (curSize’s width()) + xGap end repeat –デスクトップにPNG形式でNSImageをファイル保存 set aDesktopPath to current application’s NSHomeDirectory()’s stringByAppendingString:"/Desktop/" set savePath to aDesktopPath’s stringByAppendingString:((NSUUID’s UUID()’s UUIDString())’s stringByAppendingString:".png") set fRes to saveNSImageAtPathAsPNG(newImage, savePath) of me –2つのNSImageを重ね合わせ合成してNSImageで返す on composeImage(backImage, composeImage, aTargerRect) set newImage to NSImage’s alloc()’s initWithSize:(backImage’s |size|()) copy aTargerRect to {x1, y1, x2, y2} newImage’s lockFocus() set v2 to system attribute "sys2" if v2 ≤ 12 then –To macOS 10.12.x set bRect to current application’s NSMakeRect(x1, y1, x2, y2) set newImageRect to current application’s CGRectZero set newImageRect’s |size| to (newImage’s |size|) else –macOS 10.13 or later set bRect to {{x1, y1}, {x2, y2}} set newImageRect to {{0, 0}, (newImage’s |size|)} end if backImage’s drawInRect:newImageRect composeImage’s drawInRect:bRect newImage’s unlockFocus() return newImage end composeImage –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 –指定のパスのファイルが画像かどうかをチェック on isImageAtPath:aPath set aURL to |NSURL|’s fileURLWithPath:aPath set {theResult, theValue} to aURL’s getResourceValue:(reference) forKey:NSURLTypeIdentifierKey |error|:(missing value) return (NSImage’s imageTypes()’s containsObject:theValue) as boolean end isImageAtPath: on sort2DListAscendingWithFirstItemKey(aList as list) set sortIndexList to {0} –Key Item id: begin from 0 set sortOrders to {true} –ascending = true set sortTypes to {"compare:"} set resList to (current application’s SMSForder’s subarraysIn:(aList) sortedByIndexes:sortIndexList ascending:sortOrders sortTypes:sortTypes |error|:(missing value)) as list return resList end sort2DListAscendingWithFirstItemKey –2D Listの各要素から、指定アイテムの要素だけを取り出してリストで返す on getEveryIndicatedItemsFrom2DList(aList as list, anItem as integer) –this item No. begins from 1 set outList to {} repeat with i in aList set j to contents of item anItem of i set the end of outList to j end repeat return outList end getEveryIndicatedItemsFrom2DList |
More from my site
(Visited 184 times, 1 visits today)