脚本UI:脚本单独运行,但在从 UI 按钮调用时不起作用

ScriptUI: Script works alone, but not when called from UI button

* 已编辑以使问题更清晰 *

我在 Adob​​e 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;" 行。它有时会给出一个错误,指出没有文档,但通常只有 运行s 直到到达该行,然后停止(我添加了一些 $.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');

您必须将 window 从 palette 更改为 dialog。显然 Illustrators 实现无法从调色板访问 document。请参阅 Adob​​e 论坛中的此线程 https://forums.adobe.com/thread/841889 如果您真的需要一个调色板,似乎有一个使用 BridgeTalk 来 运行 代码的解决方法。这对我来说似乎很愚蠢:-/

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

更改下一行

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

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