レイヤーとして開く(Acorn,Pixelmator Pro)

Finderで選択した画像をAcornまたはPixelmator Proでレイヤーとして開くスクリプトです。
同サイズの複数のスクリーンショットの同じ範囲を切り抜きたいと思い、何か手軽に読み込めないかと思い作りました。
上段:Acorn用
下段:Pixelmator Pro用

use AppleScript version "2.4"
use scripting additions
use framework "Foundation"

property NSURLTypeIdentifierKey : a reference to current application's NSURLTypeIdentifierKey
property |NSURL| : a reference to current application's |NSURL|
property NSPredicate : a reference to current application's NSPredicate
property NSArray : a reference to current application's NSArray

tell application "Finder"
set aSel to the selection as alias list
end tell
if aSel = {} then return

set filRes to filterAliasListByUTI(aSel, "public.image") of me

tell application "Image Events"
set wList to {}
set hList to {}
repeat with i in filRes
set aDoc to open (contents of i)
set {w, h} to dimensions of aDoc
close aDoc
set the end of wList to w
set the end of hList to h
end repeat
end tell
set maxWidth to (last item of (sortList(wList))) as number
set maxHeight to (last item of (sortList(hList))) as number

tell application "Acorn"
activate
set aDoc to make new document with properties {width:maxWidth, height:maxHeight}
repeat with i in filRes
add image of aDoc from file i at index (count of (layers of aDoc))
end repeat
delete layer 1 of aDoc
end tell

on sortList(aList as list)
set array to current application's NSArray's arrayWithArray:aList
set anObject to array's firstObject()
if anObjectmissing value and (anObject's |class|'s isSubclassOfClass:(current application's NSString)) then
set comparator to "localizedCaseInsensitiveCompare:"
else
set comparator to "compare:"
end if
return (array's sortedArrayUsingSelector:comparator) as list
end sortList

########
--Alias listから指定UTIに含まれるものをエイリアスのリストで返す
on filterAliasListByUTI(aList, targUTI)
set newList to {}
repeat with i in aList
set j to POSIX path of i
set tmpUTI to my retUTIfromPath(j)
set utiRes to my filterUTIList({tmpUTI}, targUTI)
if utiRes is not equal to {} then
set the end of newList to j as POSIX file as alias
end if
end repeat
return newList
end filterAliasListByUTI

--指定のPOSIX pathのファイルのUTIを求める
on retUTIfromPath(aPOSIXPath)
set aURL to |NSURL|'s fileURLWithPath:aPOSIXPath
set {theResult, theValue} to aURL's getResourceValue:(reference) forKey:NSURLTypeIdentifierKey |error|:(missing value)

if theResult = true then
return theValue as string
else
return theResult
end if
end retUTIfromPath

--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
########
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"

property NSURLTypeIdentifierKey : a reference to current application's NSURLTypeIdentifierKey
property |NSURL| : a reference to current application's |NSURL|
property NSPredicate : a reference to current application's NSPredicate
property NSArray : a reference to current application's NSArray

tell application "Finder"
set aSel to the selection as alias list
end tell
if aSel = {} then return

set filRes to filterAliasListByUTI(aSel, "public.image") of me

tell application "Image Events"
set wList to {}
set hList to {}
repeat with i in filRes
set aDoc to open (contents of i)
set {w, h} to dimensions of aDoc
close aDoc
set the end of wList to w
set the end of hList to h
end repeat
end tell
set maxWidth to (last item of (sortList(wList))) as number
set maxHeight to (last item of (sortList(hList))) as number

tell application "Pixelmator Pro"
activate
set aDoc to make new document with properties {width:maxWidth, height:maxHeight}
repeat with i in filRes
tell aDoc
make new image layer at the beginning of layers with properties {file:i}
end tell
end repeat
delete last layer of aDoc
end tell

on sortList(aList as list)
set array to current application's NSArray's arrayWithArray:aList
set anObject to array's firstObject()
if anObjectmissing value and (anObject's |class|'s isSubclassOfClass:(current application's NSString)) then
set comparator to "localizedCaseInsensitiveCompare:"
else
set comparator to "compare:"
end if
return (array's sortedArrayUsingSelector:comparator) as list
end sortList

########
--Alias listから指定UTIに含まれるものをエイリアスのリストで返す
on filterAliasListByUTI(aList, targUTI)
set newList to {}
repeat with i in aList
set j to POSIX path of i
set tmpUTI to my retUTIfromPath(j)
set utiRes to my filterUTIList({tmpUTI}, targUTI)
if utiRes is not equal to {} then
set the end of newList to j as POSIX file as alias
end if
end repeat
return newList
end filterAliasListByUTI

--指定のPOSIX pathのファイルのUTIを求める
on retUTIfromPath(aPOSIXPath)
set aURL to |NSURL|'s fileURLWithPath:aPOSIXPath
set {theResult, theValue} to aURL's getResourceValue:(reference) forKey:NSURLTypeIdentifierKey |error|:(missing value)

if theResult = true then
return theValue as string
else
return theResult
end if
end retUTIfromPath

--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
########

更新履歴

inserted by FC2 system