是否可以从ExtendScript外部执行JSX脚本?

Tho*_*mas 31 javascript adobe extendscript indesign-server

通常,当您编写.jsx脚本以自动化Adobe产品(如InDesign,Illustrator或Photoshop)时,您可以从ExtendScript IDE编写,调试和执行脚本.是否可以绕过ExtendScript并从第三个程序运行脚本?

我认为Adobe产品有一个内置的JavaScript解释器,ExtendScript可以连接到它来访问Adobe对象模型并自动化他们的软件.我希望能够像在ExtendScript中一样直接连接到该解释器并运行jsx文件.

Eva*_*van 37

你在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
Run Code Online (Sandbox Code Playgroud)

并将其保存为〜/ 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;
Run Code Online (Sandbox Code Playgroud)

现在,从命令行运行osascript ~/temp/foo.scpt它将打印活动Illustrator文档中的图层数.

从JavaScript中获取数据是有限的.您无法从JavaScript中打印到stdout.而是将要返回的值作为JSX文件的最后一个语句放置; 它将被打印出来osascript.(原因如下:JSX文件中的最后一个值是do javascriptAppleScript语句的返回值.这也是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
Run Code Online (Sandbox Code Playgroud)

文件〜/ 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]);
}
Run Code Online (Sandbox Code Playgroud)

然后运行 osascript args.scpt 50 30 10 80

调试

do javascript命令还具有启动ExtendScript调试器的选项.有关详细信息,请在AppleScript编辑器中打开Illustrator词典.

  • Windows怎么样?有任何想法吗? (3认同)

jfi*_*izz 14

对于Windows用户,您可以使用vbs脚本.通过提供参数的参数传递给.jsx脚本cscript像这样的命令:cscript test.vbs "hello".test.vbs看起来像这样:

Dim appRef
Dim javaScriptFile
Dim argsArr()

Dim fsObj : Set fsObj = CreateObject("Scripting.FileSystemObject")
Dim jsxFile : Set jsxFile = fsObj.OpenTextFile("C:\Users\path\test.jsx", 1, False)
Dim fileContents : fileContents = jsxFile.ReadAll
jsxFile.Close
Set jsxFile = Nothing
Set fsObj = Nothing

javascriptFile = fileContents & "main(arguments);"

Set appRef = CreateObject("Illustrator.Application")

ReDim argsArr(Wscript.Arguments.length-1)

For i = 0 To Wscript.Arguments.length-1
    argsArr(i) = Wscript.Arguments(i)
Next

Wscript.Echo appRef.DoJavaScript(javascriptFile, argsArr, 1)
Run Code Online (Sandbox Code Playgroud)

Wscript.Echo会返回由.jsx文件返回的最后一行..jsx文件示例可能是:

function main(argv) {
    alert(argv[0]);
    return "test";
}
Run Code Online (Sandbox Code Playgroud)

当然,你应该SEEE的Illustrator(或任何土坯程序)警报"你好",然后"测试"将返回到标准输出(你应该看到它在命令提示符窗口).

  • 如果您安装了多个版本的Adobe应用程序,请使用Illustrator.Application.xx,其中xx是版本号(十进制)以获得所需的版本号. (2认同)
  • 像宣传的那样工作! (2认同)

Ste*_*sed 5

这适用于Windows:

"C:\ Program Files(x86)\ Adob​​e\Adob​​e Photoshop CS5\Photoshop.exe"C:\ completepathto\my.jsx

注意Photoshop的路径.它必须引用,因为它包含空格.

有很多技巧可以搞清楚Photoshop的加载位置.这是一个找到已加载Photoshop的位置并将其放在x.lst中的位置

@REM  The Presets\Scripts doesn't really restrict where the loop is looking, 
@REM  thus the "IF EXIST" is needed.  The FIND makes sure that the 
@for /R "%ProgramFiles(x86)%\Adobe" %%f in (Presets\Scripts) 
  DO @IF EXIST %%f 
     (echo %%f | FIND /I "Adobe Photoshop C" >> x.lst )
Run Code Online (Sandbox Code Playgroud)

然后,您可以处理x.lst中的每一行.注意:整个"@for"应该在一行上,我将它分成多行以便于阅读.

如果您认为只有一个Photoshop(而不是Elements),那么您可以将"echo %% f"更改 为

"%%f\..\..\Photoshop.exe" C:\completepathto\my.jsx 
Run Code Online (Sandbox Code Playgroud)