The following command will run the SCRIPT_ABC.jsx
which is located on my desktop. Is there a way to take the contents of the SCRIPT_ABC.jsx
and put the javascript extendscript exclusively inside automator so when I save it out as an application it will be included within the program?
on run {input, parameters}
tell application "Adobe Illustrator" to open file "Macintosh HD:Users:NAME:Desktop:SCRIPT_ABC.jsx"
return input
end run
Paste the entire jsx into your applescript as a text body, then use the do javascript
command to run it. Applescript has a great benefit of being able to pass in arguments to the jsx (do javascript myScript with arguments { textVariableHere, anotherOneHere }
) and obtain the returned result too!
Elaborated edit:
set myJavascript to "
#target illustrator
function test(args){
var docName = app.activeDocument.name;
alert(\"Hello from Adobe JSX. Current document name is: '\" + docName + \"'\\n\" + args[0]);
return docName;
};
test(arguments);
"
set myInputVar to "The input string here."
tell application "Adobe Illustrator"
set myResult to do javascript myJavascript with arguments {myInputVar}
end tell
display alert ("Returned Document Name is: '" & myResult & "'")
So what you see here is an example of a javascript embedded inside the applescript as a string. Lucky for me, I have AppleScript Debugger app which I paid for a long time ago and is well-worth it, and it has the "Paste as string literal" function which auto-escapes the quotes and stuff. Very helpful.
But you can see the magic of passing in a variable to the javascript as applescript arguments which prevents a lot of issues one could have doing string-building inside applescript to put variables in.
Although you don't have to use AppleScript arguments to put data into the jsx, as you can use the string-building mentioned above (which I do not recommend when the arguments way is available for use), the huge payoff is actually getting the returned jsx data back into the applescript. Really neat stuff.
So there you have it, the whole enchilada! I would be curious to see if there's a comparable native way to do a similar setup with Windows and VBS + JSX combo - if anyone is aware of something like this please let me know in the comments below.
Thanks!
PS:
The word arguments
used in the function call of the main function of the jsx script is actually a key-word used by AppleScript and it has to be spelled out like so, otherwise it will not work.
More Edit:
Okay, so maybe you don't wanna paste no huge jsx into your applescript and escape all the quotes, etc.
I get it. No worries! You can do the following:
Ensure your jsx is somewhere installed along with you app (it could be binary), so save the following code which represents your jsx body as a jsx file wherever:
var docName = app.activeDocument.name;
alert("Hello from Adobe JSX. Current document name is: '" + docName + "'\n" + args[0]);
return docName;
And in your AppleScript you simply change the jsx portion to the following:
set myJavascript to "
#target illustrator
function test(args){
#include '/Users/YourName/Wherever/myAppleScriptJsxTest.jsx'
};
test(arguments);
"
BOOM! Works exactly the same! Isn't that something?
You can copy the .jsx file to the Contents/Resources
folder of your Automator application bundle, and use a Run AppleScript action to get the resource
on run {input, parameters}
try
set thePath to path for resource "SCRIPT_ABC" extension "jsx"
tell application "Adobe Illustrator" to open (thePath as POSIX file)
on error errmess
display dialog errmess
end try
return input
end run
Note: You will need to copy the file in question again to the bundle /Contents/Resource
folder when you recreate your application bundle since Automator creates a fresh bundle each time you save it.
Also, it may be required to close Automator/the Script Editor after you have added the file. Finally, test the resulting app and see if it launches the file in the requested app.