AppleScriptでiTunesのアートワークからアイコンを作成するまで
アイコンを作成する際に戸惑ってしまったので簡単にメモしておくことにします.
今回はiconutil というMac特有のアイコン作成コマンドを用いることにします.
Optimizing for High Resolution
iconutilを使う条件として,
- 画像の形式がPNG
- カラープロファイルがsRGB
- アルファチャンネルがON ← これを忘れてて悲惨だった
基本的にはこのサイトのようにすればアイコンを作成できます.
MacアプリのRetina対応アイコン(*.icns)を作成する方法 | DOTAPON Blog
慣れれば簡単ですが,上の3つの条件を満たしてないと"Unsuported image format"とエラーを吐きます.
今回ではiTunesのraw data から作成した画像は全てsRGBを満たしていたので(人によって違うかも)アルファチャンネルだけなんとかしました.
アルファチャンネルをセットする方法として,
- Photoshop
- imageMagick の-alpha オプション
の2つを案としてあげましたが,今回はコマンドベースで実行できるimageMagickで行いました.
imageMagickのインストールはhomebrew もしくは Macportがあれば簡単にできるので省略します.
ソース
tell application "iTunes" if (count of artwork of current track) ≥ 1 then tell application "iTunes" to tell artwork 1 of current track set d to raw data end tell set thePath to ((path to pictures folder) as text) & "applet.iconset" set icon_512 to open for access file (thePath & ":icon_512x512.png") with write permission set eof icon_512 to 0 write d to icon_512 close access icon_512 tell application "Image Events" launch set myImage to open file (thePath & ":icon_512x512.png") save myImage in file (thePath & ":icon_512x512.png") as PNG --iTunesのアートワークから作成した画像は拡張子がpngだけではないためPNGで保存する end tell delay 1 do shell script "sips -z 512 512 " & (POSIX path of thePath) & "/icon_512x512.png" --512x512のサイズにリサイズ do shell script "/usr/local/bin/convert " & (POSIX path of thePath) & "/icon_512x512.png -alpha Set " & (POSIX path of thePath) & "/icon_512x512.png" --imageMagickのconvertコマンドを使用,アルファチャンネルを追加している do shell script "iconutil -c icns " & (POSIX path of thePath) --iconsを作成 end if end tell
アートワークからアイコンを作りたいなんて機会は滅多にありませんが,次回は前回作成した曲の変わり目に通知を入れるスクリプトと組み合わせて通知にアートワークを表示してみたいと思います.
rinringo429.hatenablog.com