Typically when you’re writing a .jsx script to automate an Adobe product (like InDesign, Illustrator or Photoshop), you write, debug and execute the script from the ExtendScript IDE. Is it possible to bypass ExtendScript and run the script from a third program?
I think Adobe products have a built-in JavaScript interpreter which ExtendScript can connect to to access the Adobe object models and automate their software. I’d like to be able to connect directly to that interpreter and run jsx files just as I would in ExtendScript.
Are you on a Mac? If so, you can use AppleScript with the
osascripttool to execute your JavaScript. Here are some examples:Running JSX and Returning a Value
Save this as ~/temp/foo.scpt:
And save this as ~/temp/foo.jsx:
Now, from the command line run
osascript ~/temp/foo.scptIt will print the number of layers in the active Illustrator document.Getting data out of the JavaScript is limiting. You can’t print to stdout from within the JavaScript. Instead, place the value you want to return as the last statement of the JSX file; it will be printed by
osascript. (Here’s why: The last value in the JSX file is the return value of thedo javascriptAppleScript statement. That is also the last value in the AppleScript file, andosascriptprints the final value.)The value you return from JavaScript can be a number, a string, an array, or anything else that retains its value when converted to a string. If you want to return a complex object, you’ll need to #include a JSON library and call
.toJSONString()on the object.Passing Arguments to JSX
To pass arguments to the JSX code, follow this example:
File ~/temp/args.scpt:
File ~/temp/args.jsx
And then run
osascript args.scpt 50 30 10 80Debugging
The
do javascriptcommand also has options for launching the ExtendScript debugger. For details, open the Illustrator dictionary in AppleScript Editor.