LoginSignup
3
3

More than 5 years have passed since last update.

【AppleScript or JavaScript+Illustrator】アイテム用スライスのズレを解消

Last updated at Posted at 2014-08-25

IllustratorでWebデザインなどを行う際にアイテムのスライスを作成しますが、
GUIでは確認できない(見えない)端数によりスライス範囲が1pxズレてしまいます。
ですので、強制的にズレない数値に置き換えます。

ポイントとしては、1回ではうまく置き換わらない(バグか?)ので2回処理します。
また、幅(Width)だけは整数値に置き換えられない(仕様なのか?バグなのか?)ため、
「スライスがズレるのは正の端数 > スライスがズレないのは負の端数」を利用し、
幅が整数値でない場合だけ、見えない負の端数に強制的に置き換えます。

※高さ(Height)は、整数値になるのですが、何故かGUI上では端数になることが。。。(バグ?)

【AppleScriptの場合】

on run
    with timeout of (60 * 15) seconds
        try
            activate
            set start_dialog to display dialog "端数除去によりレイアウトにズレが発生する可能性があります。必ず処理完了後に確認をしてください。" with icon note giving up after 60 * 5
            if (gave up of start_dialog) then return

            tell application id "com.adobe.Illustrator"
                if (count of documents) is 0 then error "ファイルが開かれていません。"

                tell current document
                    activate

                    set select_items to selection
                    if (count of select_items) is 0 then error "アイテムが選択されていません。"

                    repeat with select_item in select_items
                        tell select_item
                            repeat 2 times
                                set old_w to width as real
                                set old_h to height as real
                                set old_x to item 1 of position as real
                                set old_y to item 2 of position as real

                                set new_w to (do javascript "Math.round(" & old_w & ");") as real
                                set new_h to (do javascript "Math.round(" & old_h & ");") as real
                                set new_x to (do javascript "Math.round(" & old_x & ");") as real
                                set new_y to (do javascript "Math.round(" & old_y & ");") as real

                                if old_w is not new_w then set width to (new_w - 1.0E-4)
                                if old_h is not new_h then set height to new_h
                                if (old_x is not new_x) or (old_y is not new_y) then set position to {new_x, new_y}
                            end repeat
                        end tell
                    end repeat
                end tell
            end tell

            delay 0.1

            activate
            set finish_dialog to display dialog "処理完了しました。" with icon note buttons {"OK"} default button 1 giving up after 60 * 15
            if (gave up of finish_dialog) then return

        on error err_txt
            activate
            display dialog err_txt with icon caution buttons {"OK"} default button 1 giving up after 60 * 15
            return
        end try
    end timeout
end run

【JavaScriptの場合】

#target "Illustrator"


try
{
    if (app.documents.length == 0){throw("ファイルが開かれていません。");}

    var select_items = app.activeDocument.selection;
    if (select_items.length == 0){throw("アイテムが選択されていません。");}

    with(app.activeDocument)
    {
        alert("端数除去によりレイアウトにズレが発生する可能性があります。必ず処理完了後に確認をしてください。");

        for (i=0; i<select_items.length; i++)
        {
            for (j=0; j<=1; j++)
            {
                with(select_items[i])
                {
                    var old_w = width;
                    var old_h = height;
                    var old_x = position[0];
                    var old_y = position[1];

                    var new_w = Math.round(old_w);
                    var new_h = Math.round(old_h);
                    var new_x = Math.round(old_x);
                    var new_y = Math.round(old_y);

                    if (old_w != new_w) {width = new_w - 0.0001;}
                    if (old_h != new_h) {height = new_h;}
                    if ((old_x != new_x) || (old_y != new_y)) {position = [new_x, new_y];}
                }
            }
        }

        alert("処理完了しました。");
    }
}
catch(err_str)
{
    alert(err_str);
}
3
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
3
3