Getting access to app.activeDocument
was written by M.O.Z.G, August 28, 2014 - Tags: Adobe, coding, developing, ExtendScript, help, Illustrator, notes, script, ScriptUI, snippet - Category: Blog
During the development of the next script for Illustrator, faced with a very interesting behavior. In my case, one of the active palette called a helper dialog boxes that have just had to initiate work with the active document, based on the input of these data. Then I noticed that the dialog boxes can't receive / send data to the active document. During the search such problems and their solutions, I realized that should to think in the direction of BridgeTalk, declaring the target application. Scripts called in this way worked well, but were nuances. Firstly the script should be placed in quotes, with all its consequences, and variables are declared after the call, don't read
Solution turned out to be pretty simple, but not obvious - instead of calling individual commands through BridgeTalk, one only has to call the dialog window. That allows him and all functions called from, refer to the document, not breaking the usual style of coding.
For clarity, "two-button" version of the script that demonstrates the problem. Can try to make a call not through btExecute, and you will see that it will not work.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
#target Illustrator function btExecute($script) { var bt=new BridgeTalk(); bt.target = "Illustrator"; bt.body = $script; return bt.send(); } var docCurrent = app.activeDocument; var mainWindow = new Window ("palette", "Title"); var createButton = mainWindow.add ("button", undefined, "Create"); mainWindow.show (); createButton.onClick = function () { btExecute( 'createDialog () ') } function createDialog($type) { var createDialog = new Window ("dialog", "Create New Item"); var createAgree = createDialog.add ("button", undefined, "OK"); createAgree.onClick = function() { alert(docCurrent.name); } createDialog.show(); } |
I write mostly for myself, but if there's valuable insights on the subject - it is not forbidden)
If you found an error, highlight it and press Shift + Enter or click here to inform us.