运行 illustrator extendscript 通过 automator applescript 或 bash?

Run illustrator extendscript through automator applescript or bash?

以下命令将 运行 位于我桌面上的 SCRIPT_ABC.jsx。有没有办法获取 SCRIPT_ABC.jsx 的内容并将 javascript extendscript 专门放在 automator 中,这样当我将它保存为应用程序时,它将包含在程序中?

 on run {input, parameters}

        tell application "Adobe Illustrator" to open file "Macintosh HD:Users:NAME:Desktop:SCRIPT_ABC.jsx"

        return input
    end run

您可以将 .jsx 文件复制到 Automator 应用程序包的 Contents/Resources 文件夹中,然后使用 运行 AppleScript 操作来获取资源

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

注意:当您重新创建应用程序包时,您将需要再次将有问题的文件复制到包 /Contents/Resource 文件夹中,因为 Automator 会在您每次保存时创建一个新包。

另外,添加文件后可能需要关闭 Automator/the 脚本编辑器。最后,测试生成的应用程序并查看它是否在请求的应用程序中启动文件。

将整个 jsx 作为文本正文粘贴到您的 applescript 中,然后使用 do javascript 命令将其 运行。 Applescript 有一个很大的好处,就是能够将参数传递给 jsx (do javascript myScript with arguments { textVariableHere, anotherOneHere }) 并获得返回的结果!

详细编辑:

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 & "'")

所以你在这里看到的是一个 javascript 作为字符串嵌入到 applescript 中的例子。对我来说幸运的是,我有 AppleScript Debugger 应用程序,这是我很久以前支付的并且非常值得,它具有“粘贴为字符串文字”功能,可以自动转义引号和其他内容。很有帮助。

但是您可以看到将变量作为 applescript 参数传递给 javascript 的魔力,这可以防止人们在 applescript 中构建字符串以放入变量时可能遇到的很多问题。

虽然你不必使用 AppleScript 参数将数据放入 jsx,因为你可以使用上面提到的字符串构建(当参数方式可用时我不推荐),巨大的回报实际上是将返回的 jsx 数据返回到 applescript 中。真是整洁的东西。

所以你有它,整个辣酱玉米饼馅!我很想知道是否有类似的本机方法来使用 Windows 和 VBS + JSX 组合进行类似的设置 - 如果有人知道类似的事情,请在下面的评论中告诉我。 谢谢!

PS: 在 jsx 脚本 的主要函数的函数调用中使用的单词 arguments 实际上是 AppleScript 使用的 关键字,并且一定要这么拼,不然不行

更多编辑:

好吧,也许你不想将任何巨大的 jsx 粘贴到你的 applescript 中并转义所有引号等。

我明白了。不用担心!您可以执行以下操作:

确保你的 jsx 与你的应用程序一起安装在某个地方(它可以是二进制的),所以将代表你的 jsx 主体的以下代码保存为一个 jsx 文件:

var docName = app.activeDocument.name;
    alert("Hello from Adobe JSX. Current document name is: '" + docName + "'\n" + args[0]);
    return docName;

在您的 AppleScript 中,您只需将 jsx 部分更改为以下内容:

set myJavascript to "
#target illustrator
function test(args){
    #include '/Users/YourName/Wherever/myAppleScriptJsxTest.jsx'
};
test(arguments);
"

砰!效果完全一样!不是吗?

这是通过 Adob​​e Illustrator 执行 ~/script.jsx(可以是任何 jsx 脚本)的 AppleScript:

tell application "Adobe Illustrator"
    do javascript "(function(){//@include '~/script.jsx'}());"
end tell

或者您可以将 Adob​​e Illustrator 设置为默认打开所有 jsx 文件。之后双击任何 jsx 文件将使 Illustrator 执行脚本。它适用于 Windows 和 MacOS。