sipsコマンドでリサイズするアップルスクリプト
iTunesのアートワーク用に画像を600x600にリサイズしたい。しかし、リサイズ用の手頃なアプリケーションが無い。以前、こちらをお薦めしたが、もっと簡単にしたかったのでsipsのコマンドをアップルスクリプトから呼び出すようにした。sipsについてはこちらの記事(OS X ハッキング! (73))(http://journal.mycom.co.jp/column/osx/073/index.html)を。
以下がそのスクリプト。OSX 10.4.11で確認。使い方は、ドロップレットにして画像ファイルをドロップする、あるいはFinderで画像ファイルを選択してスクリプトを実行する。画像ファイルと同じフォルダにリサイズ後の画像ファイルが作られる。
(*iTunes用のアートワーク作成用スクリプト*)
(*600x600へリサイズする*)
property DO_LIST : {"png", "jpg"}
property MAX_SIZE : 600
on open theList
toControl(theList)
end open
on run
--Finder選択項目を調べる
tell application "Finder"
set theSelected to selection
set theLocationList to {}
repeat with theItem in theSelected
if class of theItem is folder then
exit repeat
end if
set theUrl to URL of theItem
set thePath to POSIX path of POSIX file theUrl
--ファイル名のURLデコード
set theCmd to "echo " & quoted form of thePath & " | perl -pe '~s/%([a-fA-F0-9][a-fA-F0-9])/pack(\"C\", hex($1))/eg;'"
set decodedItem to do shell script theCmd
set end of theLocationList to (POSIX file decodedItem as alias)
end repeat
end tell
--なければ選択ダイアログを表示
if theLocationList is {} then
choose file with prompt "画像ファイルを選んでください" without invisibles
toControl({result})
else
toControl(theLocationList) of me
end if
end run
on adding folder items to theFolder after receiving theList
toControl(theList)
end adding folder items to
on toControl(theList)
repeat with curItem in theList
set theFileInfo to info for curItem
if (name extension of theFileInfo) is in DO_LIST then
else
display alert "処理できません" message name of theFileInfo as warning
exit repeat
end if
set cmd to "sips -Z " & MAX_SIZE & " " & quoted form of POSIX path of curItem & " --out " & quoted form of POSIX path of newFile(curItem) of me
do shell script cmd
end repeat
end toControl
--ファイル名に(resized)を付けて返す
-- newFile:HFS形式のStringを返す,oldFile:alias
on newFile(oldFile)
set theFileInfo to info for oldFile
set pareDir to (alias ((oldFile as string) & "::"))
set stringName to name of theFileInfo as string
set nameSize to (count of character of stringName) - (count of character of name extension of theFileInfo) - 1 -- 1 is dot size
set addName to "(resized)" as string
set newName to (items 1 thru nameSize of stringName) & addName & "." & name extension of theFileInfo as string
set returnName to pareDir & newName as string
return returnName
end newFile
Finderの選択項目を取り出すのに苦労している。
tell application "Finder"
first item of selection
end tell
まず、上記のような記述は出来ない。"Finder でエラーが起きました:item 1 of selection を取得できません。"と出てエラーとなる。
この場合、以下のように記述するとうまく行く。
tell application "Finder"
set theSelected to selection
first item of theSelected
end tell
あと、ファイルのパスはURLプロパティで取り出せる。ただし、日本語部分はエンコードされているので、alias形式にするためにperlを使ってデコードしている。
| 固定リンク | コメント (0) | トラックバック (0)




最近のコメント