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

时间:2018-04-09 17:28:30

标签: javascript adobe adobe-illustrator extendscript

*编辑以使问题更清晰*

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

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');

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

{{1}}

2 个答案:

答案 0 :(得分:1)

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

更改以下行

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

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

答案 1 :(得分:0)

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