是否有可能从ExtendScript之外执行JSX脚本? [英] Is it possible to execute JSX scripts from outside ExtendScript?

查看:829
本文介绍了是否有可能从ExtendScript之外执行JSX脚本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通常情况下,当你写一个.jsx脚本来自动的Adobe产品(如InDesign中,Illustrator或Photoshop中),你编写,调试以及从ExtendScript IDE执行脚本。是否有可能绕过ExtendScript,并从第三程序运行脚本?

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 an third program?

我觉得Adobe产品有一个内置的JavaScript跨preTER它ExtendScript可以连接到访问的Adobe对象模型和自动化他们的软件。我希望能够直接连接到内部preTER和运行JSX文件,就像我会在ExtendScript。

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.

推荐答案

您在Mac上?如果是这样,你可以使用的AppleScript与 osascript 工具来执行你的JavaScript。下面是一些例子:

Are you on a Mac? If so, you can use AppleScript with the osascript tool to execute your JavaScript. Here are some examples:

另存为〜/温度/ foo.scpt:

Save this as ~/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

和保存以此为〜/温度/ foo.jsx:

And save this as ~/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〜/温度/ foo.scpt 将打印层活动的Illustrator文件的数量。

Now, from the command line run osascript ~/temp/foo.scpt It will print the number of layers in the active Illustrator document.

获取数据出来的JavaScript被限制。您无法从JavaScript的内打印到标准输出。相反,将要返回的JSX文件的最后一条语句的值;它会通过打印osascript 。 (这里的原因:在JSX文件中的最后一个值是做的JavaScript AppleScript的声明,也是在AppleScript的文件中的最后一个值,而<$ C的返回值。 $ C> osascript 打印最终值。)

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 the do javascript AppleScript statement. That is also the last value in the AppleScript file, and osascript prints the final value.)

您从JavaScript返回值可以是数字,字符串数组,或其他任何时候转换为一个字符串,它保留了它的价值。如果你想返回一个复杂的对象,你需要#包括一个JSON库,并调用 .toJSONString()的对象。

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.

要传递参数给JSX code,按照这个例子:

To pass arguments to the JSX code, follow this example:

文件〜/温度/ args.scpt:

File ~/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

文件〜/温度/ args.jsx

File ~/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

做的JavaScript 命令也有启动ExtendScript调试器选项。有关详细信息,在AppleScript的编辑器中打开Illustrator的字典。

The do javascript command also has options for launching the ExtendScript debugger. For details, open the Illustrator dictionary in AppleScript Editor.

这篇关于是否有可能从ExtendScript之外执行JSX脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆