*编辑使问题更清晰*
我在 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;” 但这给了我一个错误,指出“应用程序”未定义。有什么想法吗?
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');