脚本UI:脚本单独工作,但从UI按钮调用时不能


*

已编辑以使问题更清晰 *

我在 Adobe Illustrator 的 Extendscript 中创建了一个工具,该工具将使用名为"Guide"的选定对象对齐并按比例调整所选组的大小。 下面的代码是脚本,它在自己运行时工作:

function proofTool() {
    var items = selection;
    if ( items.length != 2 ) //makes sure that 2 items are selected
    {
        alert("Select the and group the artwork and select the guide to center it on before running this script.");
    }
    else
    {
        //assigns selected guide to guide and other selection to artwork
        var artwork = null;
        var guide = null;
        for ( i = 0; i != 2; ++i )
        {
            if ( items[ i ].name == "Guide" )
            {
                guide = items[ i ];
            }
            else
            {
                artwork = items[ i ];
            }
        }
        // makes sure that things are sleected and got assigned
        if ( ( null == artwork ) || ( null == guide ) )
        {
            alert("Select the and group the artwork and select the guide to center it on before running this script.");
        }
        else
        {
            //Resizes artwork proportionately to fit in Guide area
            if (artwork.width >= artwork.height) 
            {
                var scale = (artwork.width / guide.width);
            } 
            else 
            {
                var scale = (artwork.height / guide.height);
            }
            artwork.width /= scale;
            artwork.height /= scale;
            //centers artwork on center of selected guide
            var guideXPos = (guide.position[0]+guide.width/2);
            var guideYPos = (guide.position[1]-guide.height/2);
            artwork.position = [guideXPos-(artwork.width/2), guideYPos+(artwork.height/2)];
            redraw();
        }//Close of Position/re-size If/Else
    }//Close of item select
}//Close of proofTool function
proofTool();

但是,我想创建一个可用于运行此脚本的调色板,以便用户不必通过菜单访问脚本。 但是,当我使用以下脚本创建一个带有调用该函数的按钮的调色板时,它会停在"var items = selection;"行。 它有时会给出一个错误,指出没有文档,但通常只是运行直到到达该行,然后停止(我添加了一些 $.writeln 行以查看它在哪里停止(。 我尝试将该行更改为"var items = app.activeDocument.selection;",但这给了我一个错误,指出"app"未定义。 有什么想法吗?

var win = new Window ('palette', 'Proof Tool');
    var okButton = win.add ('button', undefined, 'OK');
    okButton.onClick = proofTool;
function proofTool() {
    $.writeln ('Check 01');
    var items = selection;
    $.writeln ('Check 01.1');
    if ( items.length != 2 ) //makes sure that 2 items are selected
    {
        $.writeln ('Check 02');
        alert("Select and group the artwork and select the guide to center it on before running this script.");
    }
    else
    {
        $.writeln ('Check 03');
        //assigns selected guide to guide and other selection to artwork
        var artwork = null;
        var guide = null;
        for ( i = 0; i != 2; ++i )
        {
            if ( items[ i ].name == "Guide" )
            {
                guide = items[ i ];
            }
            else
            {
                artwork = items[ i ];
            }
        }
        // makes sure that things are sleected and got assigned
        if ( ( null == artwork ) || ( null == guide ) )
        {
            $.writeln ('Check 04');
            alert("Select and group the artwork and select the guide to center it on before running this script.");
        }
        else
        {
            $.writeln ('Check 05');
            //Resizes artwork proportionately to fit in Guide area
            if (artwork.width >= artwork.height) 
            {
                var scale = (artwork.width / guide.width);
            } 
            else 
            {
                var scale = (artwork.height / guide.height);
            }
            artwork.width /= scale;
            artwork.height /= scale;
            //centers artwork on center of selected guide
            var guideXPos = (guide.position[0]+guide.width/2);
            var guideYPos = (guide.position[1]-guide.height/2);
            artwork.position = [guideXPos-(artwork.width/2), guideYPos+(artwork.height/2)];
            redraw();
        }//Close of Position/re-size If/Else
    }//Close of item select
}//Close of proofTool function
win.show();
$.writeln ('Check 06');

您必须将窗口从palette更改为dialog。显然,Illustrators 实现无法从调色板访问document。在 Adobe 论坛中查看此主题 https://forums.adobe.com/thread/841889 如果您真的需要一个调色板,似乎有一种使用 BridgeTalk 来运行代码的解决方法。这对我来说似乎很愚蠢:-/

您使用的是哪个版本的 Illustrator?

更改以下行

var win = new Window ('palette', 'Proof Tool');

var win = new Window ('dialog', 'Proof Tool');

最新更新