* Edited to make Issue Clearer *
I've created a tool in Extendscript for Adobe Illustrator that will align and proportionately resize a selected group with a selected object named "Guide". The below code is the script, which works when run on it's own:
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();
However, I wanted to create a palette that can be used to run this script, so that the user doesn't have to access the script through the menu. But, when I use the below script to create a palette with a button that calls that function, it stops at the line "var items = selection;". It sometimes gives an error stating that there is no document, but usually just runs until reaching that line, then stops (I added some $.writeln lines to see what where it was stopping). I tried changing that line to "var items = app.activeDocument.selection;" but that gave me an error stating that "app" was undefined. Any thoughts?
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');
You will have to change your window from a palette
to a dialog
. Apparently Illustrators implementation can't access the document
from the palette. See this Thread in the Adobe forums https://forums.adobe.com/thread/841889 If you really need a palette there seems to be a workaround using BridgeTalk to run the code. Which seems silly to me :-/