ごんれのラボ

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

Illustratorドキュメントに配置されている画像の上にファイル名を配置する

概要

Illustrator ドキュメントに配置されている画像の上にファイル名を配置する。
ファイル名のテキストフレーム配置用に FileName というレイヤーを生成するので、ファイル名配置後に好きな書式設定を適用する想定。
テキストフレームは配置画像の天地左右センターに配置される

使い方

  1. AppleScript にコピペして適当なところに保存
  2. ドキュメントを開く
  3. AppleScript を実行する
  4. 配置画像の上にファイル名のテキストフレームが生成される
  5. お好きにどうぞ

Before before

After before

こんなときに使ってみたら

  • 画像補正部署にドキュメントに貼り込まれた状態のデザインと一緒に配置画像名を伝えたいとき
  • (カスタマイズして)ファイル名と拡大率を表示して、Photoshop で縮小するときの参考にしたいとき
  • (カスタマイズして)ファイル名と解像度を表示して、お客さんに高解像度の画像を寄越せとお願いしたいとき(これ便利だった)

ソースコード

tell application "Adobe Illustrator"
    tell document 1
        set ruler origin to {0, 0}
        
        try
            set nameLayer to layer "FileName"
        on error
            set nameLayer to make layer at beginning with properties {name:"FileName"}
        end try
        
        --配置されているファイル分繰り返す
        repeat with i from 1 to count placed item
            try
                tell placed item i --配置ファイルi個目に命令
                    set myFilePath to file path as alias --ファイルパスを取得
                    set myFilePath to myFilePath as Unicode text
                    set myName to last item of my splitupDelimiter(myFilePath, ":") --ファイル名を抽出
                    
                    --座標値取得
                    set myPlacedBox to geometric bounds
                    set P_x1 to (item 1 of myPlacedBox) as real
                    set P_y1 to (item 2 of myPlacedBox) as real
                    set P_x2 to (item 3 of myPlacedBox) as real
                    set P_y2 to (item 4 of myPlacedBox) as real
                    set P_Height to (P_y1 - P_y2) as real
                    set P_Width to (P_x2 - P_x1) as real
                end tell
                
                tell nameLayer
                    set myNameItem to make text frame at beginning with properties {contents:myName}
                    
                    tell myNameItem
                        set justification of paragraph 1 to center --センター揃え
                        set fill color of paragraph 1 to {cyan:0.0, magenta:0.0, yellow:0.0, black:0.0}
                        --幅と高さを取得
                        set myNameBounds to geometric bounds
                        set N_x1 to (item 1 of myNameBounds) as real
                        set N_y1 to (item 2 of myNameBounds) as real
                        set N_x2 to (item 3 of myNameBounds) as real
                        set N_y2 to (item 4 of myNameBounds) as real
                        set N_Height to (N_y1 - N_y2) as real
                        set N_Width to (N_x2 - N_x1) as real
                        
                        --座標値を変更
                        set position to {P_x1 + ((P_Width - N_Width) / 2), P_y1 - ((P_Height - N_Height) / 2)}
                    end tell
                end tell
            end try
        end repeat
    end tell
end tell

--分割サブルーチン
to splitupDelimiter(targetStr, splitStr)
    set str to targetStr as Unicode text
    set OriginalDelimiters to AppleScript's text item delimiters
    set AppleScript's text item delimiters to splitStr
    set itemsList to text items of str
    set AppleScript's text item delimiters to OriginalDelimiters
    --itemsListにリストで代入されているからあとはご自由に
    return itemsList
end splitupDelimiter

カスタマイズするとしたら

拡大率、回転率なんかを一緒に書き出したら便利。
行方不明だけど、昔書いたのでやろうと思えばできる。

もっと詳しく

質問、要望があれば、Twitter にて。