0

* 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');
RobC
  • 22,977
  • 20
  • 73
  • 80
Derek Glissman
  • 67
  • 1
  • 1
  • 14

2 Answers2

0

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 :-/

fabianmoronzirfas
  • 4,091
  • 4
  • 24
  • 41
  • I tried adding this line to the top; I'm still getting the same behavior of it stopping at the line where I declare the `items` variable in my function. The behavior remains the same with it written as either of the below versions: `var items = selection;` `var items = app.activeDocument.selection;` – Derek Glissman Apr 11 '18 at 12:40
  • Ah okay. I get it. Yes that is a wired bug. there is no document Take a look at this thread https://forums.adobe.com/thread/841889 --> "it doesn't work with palettes, only dialogs can do that. You only have one shot to do what you need to do...before you display the palette. Once it displays, it breaks the connection with the app." – fabianmoronzirfas Apr 11 '18 at 18:12
0

Which version of Illustrator you are using?

Change following line

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

to

var win = new Window ('dialog', 'Proof Tool');
Charu Rajput
  • 653
  • 3
  • 15