Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Are you on a Mac? If so, you can use AppleScript with the <code>osascript</code> tool to execute your JavaScript. Here are some examples:</p> <h2>Running JSX and Returning a Value</h2> <p>Save this as ~/temp/foo.scpt:</p> <pre><code>tell application "Adobe Illustrator" -- 'do javascript' runs any arbitrary JS. -- We're using the #include feature to run another -- file. (That's an Adobe extension to JS.) -- -- You have to pass a full, absolute path to #include. -- -- The documentation alleges that 'do javascript' -- can be passed an AppleScript file object, but -- I wasn't able to get that to work. do javascript "#include ~/temp/foo.jsx" end tell </code></pre> <p>And save this as ~/temp/foo.jsx:</p> <pre><code>var doc = app.activeDocument; var numLayers = doc.layers.length; // The last value in the JSX file will be printed out by // osascript. numLayers; </code></pre> <p>Now, from the command line run <code>osascript ~/temp/foo.scpt</code> It will print the number of layers in the active Illustrator document.</p> <p>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 <code>osascript</code>. (Here's why: The last value in the JSX file is the return value of the <code>do javascript</code> AppleScript statement. That is also the last value in the AppleScript file, and <code>osascript</code> prints the final value.)</p> <p>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 <code>.toJSONString()</code> on the object.</p> <h2>Passing Arguments to JSX</h2> <p>To pass arguments to the JSX code, follow this example:</p> <p>File ~/temp/args.scpt:</p> <pre><code>on run argv tell application "Adobe Illustrator" set js to "#include '~/temp/args.jsx';" &amp; return set js to js &amp; "main(arguments);" &amp; return do javascript js with arguments argv end tell end run </code></pre> <p>File ~/temp/args.jsx</p> <pre><code>function main(argv) { var layer = app.activeDocument.activeLayer; app.defaultStroked = true; app.defaultFilled = true; // Top, left, width, height (in points). // Note that parameters start at argv[0]. layer.pathItems.rectangle(argv[0], argv[1], argv[2], argv[3]); } </code></pre> <p>And then run <code>osascript args.scpt 50 30 10 80</code></p> <h2>Debugging</h2> <p>The <code>do javascript</code> command also has options for launching the ExtendScript debugger. For details, open the Illustrator dictionary in AppleScript Editor.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload