Skip to main content
Known Participant
June 16, 2010
Answered

Script UI breaks connection with AI Document?

  • June 16, 2010
  • 2 replies
  • 10938 views

Last week I wrote a simple script that interacts with my document and exports layers as images.

The script works great, but I thought I'd add a nice UI to it before I release it to the world, so I spent last Saturday making the GUI.

Now, I've built my UI, and I hook it up to my existing code.

If I  trace the current "activeDocument" at the beginning of my code, before I instantiate the window, I get what you would expect:

alert("Current Window:"+.app.activeDocument);//  Traces -   Current Window:[Document myTestDoc.ai]

Now if I try to reference my document from the window click handler, or after I've closed my window I get this:

alert("Current Window:"+.app.activeDocument);//  Traces -       Current Window:[Document]

alert("Current Window:"+.app.documents[0]);//  Also Traces -   Current Window:[Document]

The script can still see the document, but suddenly it can't introspect it.

"app.documents" yields the correct number of open documents,

"app.name" yields the correct application "Adobe Illustrator"

but anything like the following will fail.

alert(app.documents[0].layers.length);

Any advice would be massively appreciated.

thanks,

-J

This topic has been closed for replies.
Correct answer chris_gebab

OK, I can run this from extendscript Toolkit without any issues. If I run from illustrator then I need the above "alert hack".

Essentially I took your idea from above Chris, and blended it with your .execute() post, and got this. No need for a temp file.. Because I write the second script on the fly, from within the first one.

The code is wonky, but it does what I want, and the end user shouldn't have to worry about it.  I THINK this is the winning formula... Thanks for the help, and the inspiration!

cheers,

-J

var btnPnlResource ="Panel { orientation:'row', \
text: 'Push Button to Test', \
testBtn: Button { text:'Test' }, \
}";

var dlg = new Window("palette", "Execute Test");

dlg.btnPnl = dlg.add(btnPnlResource);
dlg.show();

dlg.btnPnl.testBtn.onClick = traceDoc;
//alert("Move me, don't close me.");
function traceDoc(){
    dlg.close(1);
    var myFile = new File("~/Desktop/test2.jsx");
    var content="alert('Written from script1: current document name:'+app.activeDocument.name)";
    myFile.open("e");
    myFile.writeln(content);
    myFile.close();
    myFile.execute();
}


cool, maybe we find a simple as possible solution.

you don't need the alert, you must set at begin:

#targetengine ????? (I heard "not main" but this seems to be not true, it can be main?)

don't ask me why

#target illustrator
#targetengine another

var btnPnlResource ="panel { orientation:'row', \
text: 'Push Button to Test', \
testBtn: Button { text:'Test' }, \
}";
var dlg = new Window("palette", "Select PATH");
dlg.btnPnl = dlg.add(btnPnlResource);
dlg.show();
dlg.btnPnl.testBtn.onClick = traceDoc;
//alert("Move me, don't close me.");
function traceDoc(){
    // !!!!!!!!   dlg.close(1);
    var myFile = new File("~/Desktop/test2.jsx");
     var content= "#target illustrator\n" +
     "#targetengine another\n" +
    "alert('Written from script1: AREA:'+app.activeDocument.selection[0].area)";
    myFile.open("w");
    myFile.write(content);
    myFile.close();
    myFile.execute();

// !!!!!!!dlg.show() // !!!!!!!!!!!!!!!
}

problem is, the estk asks "should I do", and need some time to react.

faster but only for windows:

(test2.jsx still exists)

#target illustrator
#targetengine another

var appPath = app.path + "/Support%20Files/Contents/Windows/illustrator.exe " ;//unix?
var shellScriptPath = "~/desktop/AiShellScript.bat" ;
var aiScriptPath = "~/desktop/test2.jsx" ;

function Schell(){
     var ssp = new File( shellScriptPath ) ;
     ssp.open("w") ;
     var tex = "@echo off\n"+   // unix?

          "Start \"\" \"" + File(appPath).fsName + "\" " + File(aiScriptPath).fsName + "\"" ;  //unix?
     ssp.write( tex ) ;
     ssp.close() ; ssp = null ;
}

var btnPnlResource ="panel { orientation:'row', \
text: 'Push Button to Test', \
testBtn: Button { text:'Test' }, \
}";
var dlg = new Window("palette", "Select PATH");
dlg.btnPnl = dlg.add(btnPnlResource);
dlg.show();
dlg.btnPnl.testBtn.onClick = traceDoc;
function traceDoc(){
     Schell();
     var myFile = new File( shellScriptPath );
     myFile.execute();
}

but, which case ever, interesting is

-that we don't need to close the window.

- that we can run more then 1 script

- that we can't look at the variables between scripts, even if (declared)targetengine is the same in both scripts, i think it is not really the same.

edit: maybe THIS IS ALL CRAP, because:

illustrator12.executeScript(myAIScript);

but it seems illustrator can't be it's own targetapplication???

     try{
     illustrator.executeScript( "~/desktop/test2.jsx" );
     }catch(e){alert(e)}

this gives me a syntax error

2 replies

Inspiring
June 22, 2010

Cool! Thanks John.

problem is, the estk asks "should I do", and need some time to react.

yes, only except

The Toolkit treats the user's Documents/Adobe Scripts folder, however, as a trusted location; when you double-click a JSX file in that folder, the Toolkit does not display the security alert.

----From JavaScript Tools Guide

Inspiring
June 16, 2010

Hi John,

it's not the palette problem?

can you post the code of the window?

I think, alert: [Document], is not the Doc(instance) but the constuctor ("class").??

Chris

johnwunAuthor
Known Participant
June 16, 2010

Hello Chris,

Thank you for the response.

My code is huge, so I took a MUCH simpler example from the "Sample files" that comes with ExtendScript ToolKit.

(specifically SnpCreateSlider.jsx)

The only change I've made to the code is that I've added a nowTraceAnythingAboutCurrentDocument() function at the bottom.

It is exhibiting the same behavior for me as my code, so maybe I am thinking incorrectly about how the UI dialogs are supposed to work.

Anyway, any and all help is much appreciated.

thanks

-J

//////////////////////////////////////////////////////////////////////////// // ADOBE SYSTEMS INCORPORATED // Copyright 2007 Adobe Systems Incorporated // All Rights Reserved // // NOTICE:  Adobe permits you to use, modify, and distribute this file in accordance with the // terms of the Adobe license agreement accompanying it.  If you have received this file from a // source other than Adobe, then your use, modification, or distribution of it requires the prior // written permission of Adobe. ///////////////////////////////////////////////////////////////////////////// /**   @fileoverview Shows how to use a slider ScriptUI component.   @class Shows how to use a slider ScriptUI component.   <h4>Usage</h4>   <ol>   <li> Open this file and run it in the ExtendScript Toolkit.        You can choose as the target any application that supports ScriptUI   <li> Move the slider in the displayed dialog and watch the label update.   </ol>   <h4>Description</h4>   <p>Creates a dialog containing a slider and a text component. The current value   is reflected in the text.<br />     @constructor Constructor. */ function SnpCreateSlider () {      this.windowRef = null; } /** Create a window, add a slider control and a text label. Define behavior for the slider that updates the text in the label. @return True if the snippet ran as expected, false  otherwise.  @type Boolean */ SnpCreateSlider.prototype.run = function() {      var retval = true;            // Create a palette-type window (a modeless or floating dialog),      var win = new Window("palette", "SnpCreateSlider", [150, 150, 600, 300]);      this.windowRef = win;            // Add a panel to contain the components      win.pnl = win.add("panel", [10, 10, 440, 100], "Move slider around");      // Add some labels that describe the state of the slider      win.pnl.minLbl = win.pnl.add("statictext", [20, 47, 35, 60], "0");      win.pnl.curLbl = win.pnl.add("statictext", [115, 47, 170, 60], "100");      win.pnl.maxLbl = win.pnl.add("statictext", [210, 47, 250, 60], "200");      win.pnl.displayTextLbl = win.pnl.add("statictext", [290, 20, 380, 40], "Current value:");      win.pnl.displayLbl = win.pnl.add("statictext", [385, 20, 425, 40], "100");      // Add a slider control      win.pnl.sliderCtrl = win.pnl.add("slider", [20, 20, 230, 45], 100, 0, 200);      // Add buttons      win.resetButton = win.add("button", [25, 110, 125, 140], "Reset");      win.doneButton = win.add("button",[320, 110, 420, 140] , "Done");      // Define behavior for when the slider value changes      win.pnl.sliderCtrl.onChanging = function()      {           var val = Math.round(win.pnl.sliderCtrl.value);           // Update the label text with the current slider value.           win.pnl.displayLbl.text = val;      };            // Define behavior for the "Exit" button      win.doneButton.onClick = function ()      {                   win.close();         nowTraceAnythingAboutCurrentDocument();      };      // Define behavior for the "Reset" button      win.resetButton.onClick = function()      {           win.pnl.sliderCtrl.value = 100;           win.pnl.curLbl.text = 100;           win.pnl.displayLbl.text= 100;      }      // Display the window      win.show();      return retval; } /** "main program": construct an anonymous instance and run it   as long as we are not unit-testing this snippet. */ if(typeof(SnpCreateSlider_unitTest) == "undefined") {     new SnpCreateSlider().run();     alert("Before the window, this works: app.documents[0].name="+app.documents[0].name); } function nowTraceAnythingAboutCurrentDocument(){         alert("I can still get the number of documents: app.documents.length = "+app.documents.length);  //works...         alert("And even a vague reference to the current document: app.activeDocument: "+app.documents[0]+" but the next line will fail to run.");   //finds [Document], but it's not accessible...         alert("But this fails to run for me:"+activeDocument); //this flat out fails.         alert("This too"+app.documents[0].name);   //this too. }

johnwunAuthor
Known Participant
June 16, 2010

yes, palette is the problem, (i think so, whitout testing).

try

var win = new Window("dialog"

instead of

var win = new Window("palette"

chris


I have some functionality in my UI window that requires "palette."

So... Dare I ask, how the heck do you utilize a palette window with Illustrator?

Are there any examples anywhere that actually DO something with AI?

thanks,

-J