ごんれのラボ

iOS、Android、Adobe系ソフトの自動化スクリプトのことを書き連ねています。

レイヤーごとにPNG形式で画像を書き出す

レイヤーごとにJPEG形式で画像を書き出すというスクリプトを書いて、公開しています。

www.macneko.com

そのコメントで「PNG形式で書き出すバージョンもほしい」という要望があったので、書いてみました。

<仕様>

  • 共通して書き出したいオブジェクトは表示レイヤーに入れてある
  • レイヤーには必要なオブジェクトが格納されている
  • レイヤーの表示のオン/オフで書き出すPNGを決める
  • PNGの名前はレイヤー名から取得
  • PNGはPNG24形式
  • エラー処理はすっとばす
--保存場所を決める
set filePath to choose folder with prompt "保存するフォルダを選択してください" as string

--ここからIllustratorの処理
tell application "Adobe Illustrator"
    activate
    set myDoc to document 1
    tell myDoc
        --非表示レイヤーをリスト化して変数へ
        set visibleFalseLayer_list to every layer whose visible is false
    end tell
    
    --非表示レイヤーの数だけ繰り返す
    repeat with a from 1 to count of visibleFalseLayer_list
        tell myDoc
            set currentLayer to item a of visibleFalseLayer_list --処理用に代入
            set visible of currentLayer to true --レイヤーを表示状態に
            set myLayerName to name of currentLayer
            set newFilePath to filePath & myLayerName & ".jpg" as string --保存ファイル名を代入
        end tell
        export myDoc to newFilePath as PNG24 with options {class:PNG24 export options, antialiasing:true, artboard clipping:false, horizontal scaling:100.0, matte:true, matte color:{red:255, green:255, blue:255}, saving as HTML:false, transparency:true, vertical scaling:100.0}
        --antialiasing:trueはアンチエイリアス有効
        --transparency:trueは透過有効
        --vertical scaling:100.0は書き出す倍率(vertical scaling:200.0は縦方向に200%)
        --matte color:{red:255, green:255, blue:255}はRGBってことだと思う?
        tell myDoc
            set visible of currentLayer to false --再度非表示にする
        end tell
    end repeat
end tell
activate
display dialog ((count of visibleFalseLayer_list) as string) & "ファイル書き出しました"

ダウンロードはこちら

レイヤーごとにPNG書き出し