你在 Mac 上吗?如果是这样,您可以使用 AppleScript 和该osascript
工具来执行您的 JavaScript。这里有些例子:
运行 JSX 并返回一个值
将其另存为 ~/temp/foo.scpt:
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
并将其保存为 ~/temp/foo.jsx:
var doc = app.activeDocument;
var numLayers = doc.layers.length;
// The last value in the JSX file will be printed out by
// osascript.
numLayers;
现在,从命令行运行osascript ~/temp/foo.scpt
它将打印活动 Illustrator 文档中的层数。
从 JavaScript 中获取数据是有限制的。您无法从 JavaScript 中打印到标准输出。相反,将要返回的值作为 JSX 文件的最后一条语句;它将由osascript
. (原因如下:JSX 文件中的最后一个值是do javascript
AppleScript 语句的返回值。这也是 AppleScript 文件中的最后一个值,并osascript
打印最终值。)
您从 JavaScript 返回的值可以是数字、字符串、数组或任何其他在转换为字符串时仍保留其值的内容。如果要返回复杂对象,则需要#include JSON 库并调用.toJSONString()
该对象。
将参数传递给 JSX
要将参数传递给 JSX 代码,请按照以下示例操作:
文件 ~/temp/args.scpt:
on run argv
tell application "Adobe Illustrator"
set js to "#include '~/temp/args.jsx';" & return
set js to js & "main(arguments);" & return
do javascript js with arguments argv
end tell
end run
文件 ~/temp/args.jsx
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]);
}
然后运行 osascript args.scpt 50 30 10 80
调试
该do javascript
命令还具有用于启动 ExtendScript 调试器的选项。有关详细信息,请在 AppleScript 编辑器中打开 Illustrator 词典。