1
3

More than 3 years have passed since last update.

一つの画像ファイルから複数のサイズの画像を作成するAppleScript

Last updated at Posted at 2020-04-20

 Xcodeで iOS アプリを作成する時に必要なアイコン用の複数のサイズの画像を作成するAppleScript を作成したので、コードとポイントを記す。
MacOSX 10.15での実行を確認しています。

 やりたいこと

 iOSアプリケーションを開発する際、iPhone, iPad, さらに Mac Catalyst それぞれのアプリケーションを作成するには複数のサイズのアイコン(App, Notification, Settings, Spotlight)を作成する必要がある。一つひとつの作業は簡単だが、サイズの種類が多く、全てを作成するには手間がかかる。そこで、AppleScript により一つのファイルから複数のサイズのアイコンを一気に作成したい。

画像のサイズを変更する方法

 AppleScript で画像のサイズを変更するには、AppleScript専用の補助アプリケーションとしてMac OS に標準で装備されているImage Events というツールを利用する。

Image Eventsについては以下を参照。
https://qiita.com/ynomura/items/76bde7935af4b82ab8b1

 なお、MacOS 10.15 Catalina では、Image Events が標準では利用できなくなっているので、システム環境設定のセキュリティ設定からフルディスクアクセスに登録する必要がある。
 詳しくは「AppleScriptの穴」を参照。
http://piyocast.com/as/archives/8301

AppleScriptコード

 作成したいアイコンのサイズは、targetImageSize として定義しておく。以下の例では {58, 76, 87, 120, 152, 167, 180} という7種類のサイズを作成する。
 元となる画像ファイルを大きなサイズで作成しておき、ファイル選択ダイアログchoose fileで選択し originalFile に設定るす。さらに、フォルダ選択ダイアログchoose folderで、サイズを変更したファイルを保存するフォルダを指定する。
 repeat では、アイコンサイズ・リストの要素の数だけファイルをコピーし、ファイル名にアイコンサイズを追加し、画像サイズを変更する処理を繰り返している。

 ややつまずいた点が、Image Events の open コマンドへの引数が furl だという点。そのため、画像ファイルをimageFileに設定する際にはPOSIX path ofでパスを指定している。

※以下のスクリプトをスクリプトエディタに貼り付ければ実行できます。

set targetImageSize to {58, 76, 87, 120, 152, 167, 180} --作成するアイコンサイズ
set originalFile to choose file with prompt "元ファイルを選択してください" --大きなサイズの元ファイルを指定
set targetFolder to choose folder with prompt "コピー先フォルダを指定してください" --コピー先フォルダは空にしておいて下さい。エラー処理などはしていません。

tell application "Finder"

    set extentionName to name extension of originalFile as string --拡張子
    set l to the length of (name of file originalFile as string) --作成するアイコンの数(アイコンサイズのリストの要素をカウント)
    set fileName to characters 1 thru (l - (the length of extentionName) - 1) of (name of file originalFile as string) --ファイル名(拡張子なし)

    repeat with i in targetImageSize
        duplicate originalFile to targetFolder --ファイルをコピー
        set targetFileName to fileName & "(" & (i as string) & ")." & extentionName as text --
        set name of alias (targetFolder & fileName & "." & extentionName as text) to targetFileName --ファイル名にアイコンサイズを追加
        set imageFile to POSIX path of (targetFolder & targetFileName as text) --イメージファイルを設定

        tell application "Image Events" -- Image Events を呼び出す
            launch
            set aImage to open imageFile
            scale aImage to size i
            save aImage in imageFile
            close aImage
        end tell
    end repeat

end tell

 参考資料

 以下の資料を参考にしました。
AppleScript 全般。ファイル操作や文字列の扱いなど。
長野谷隆昌@Piyomaru Software 著『AppleScript 最新リファレンス』
https://piyomarusoft.booth.pm/items/307646
http://piyocast.com/as/

Image Eventsによる画像のリサイズ方法について。
https://qiita.com/ynomura/items/76bde7935af4b82ab8b1

OS X Catalina で Image Events.app がデフォルトではファイルにアクセスできない設定になっていることについて。
http://piyocast.com/as/archives/8301

1
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
3