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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3846626/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 06:21:38  来源:igfitidea点击:

Is it possible to execute JSX scripts from outside ExtendScript?

javascriptadobeextendscriptindesign-server

提问by Thomas

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

通常,当您编写 .jsx 脚本以自动化 Adob​​e 产品(如 InDesign、Illustrator 或 Photoshop)时,您可以从 ExtendScript IDE 编写、调试和执行脚本。是否可以绕过 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.

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

回答by Evan

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

你在 Mac 上吗?如果是这样,您可以使用 AppleScript 和该osascript工具来执行您的 JavaScript。这里有些例子:

Running JSX and Returning a Value

运行 JSX 并返回一个值

Save this as ~/temp/foo.scpt:

将其保存为 ~/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

And save this as ~/temp/foo.jsx:

并将其保存为 ~/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;

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

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

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 javascriptAppleScript statement. That is also the last value in the AppleScript file, and osascriptprints the final value.)

从 JavaScript 中获取数据是有限制的。您无法从 JavaScript 中打印到标准输出。相反,将要返回的值作为 JSX 文件的最后一条语句;它将由osascript. (原因如下:JSX 文件中的最后一个值是do javascriptAppleScript 语句的返回值。这也是 AppleScript 文件中的最后一个值,并osascript打印最终值。)

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.

您从 JavaScript 返回的值可以是数字、字符串、数组或任何其他在转换为字符串时保留其值的内容。如果要返回复杂对象,则需要#include JSON 库并调用.toJSONString()该对象。

Passing Arguments to JSX

将参数传递给 JSX

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

要将参数传递给 JSX 代码,请按照以下示例操作:

File ~/temp/args.scpt:

文件 ~/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

File ~/temp/args.jsx

文件 ~/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]);
}

And then run osascript args.scpt 50 30 10 80

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

Debugging

调试

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

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

回答by jfizz

For Windows users, you can use a vbs script. Pass arguments to the .jsx script by providing arguments to the cscriptcommand like so: cscript test.vbs "hello". test.vbs could look like so:

对于 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)

The Wscript.Echowill return the last line returned by the .jsx file. A .jsx file example could be:

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

function main(argv) {
    alert(argv[0]);
    return "test";
}

When ran, you should seee Illustrator (or whatever adobe program) alert "hello" and then "test" will be returned to stdout (you should see it in the command prompt window).

运行时,您应该看到 Illustrator(或任何 adobe 程序)警报“hello”,然后“test”将返回到 stdout(您应该在命令提示符窗口中看到它)。

回答by Steven the Easily Amused

This works in windows:

这适用于Windows:

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

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

Pay attention to the path to Photoshop. It must be quoted since it contains spaces.

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

There are plenty of tricks for figuring out where Photoshop is loaded. Here is one that finds every location where Photoshop has been loaded and places those in x.lst

有很多技巧可以确定 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 )

You can then process each line in x.lst. NOTE: The entire "@for" should be on ONE line, I split it to multiple lines for readability.

然后您可以处理 x.lst 中的每一行。注意:整个“@for”应该在一行上,为了便于阅读,我将其拆分为多行。

If you believe there will be only one Photoshop (and not Elements) then you could change "echo %%f"to

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

"%%f\..\..\Photoshop.exe" C:\completepathto\my.jsx 

回答by bgmCoder

The straight answer is YES. Illustrator, InDesign and Photoshop can all be scripted through COM. Any program that you make that can access COM (such as a .net language, C++, BCX, Autohotkey or Powerpro) can tell Illustrator, InDesign or Photoshop to do things.

直接的答案是YES。Illustrator、InDesign 和 Photoshop 都可以通过 COM 编写脚本。您制作的任何可以访问 COM 的程序(例如 .net 语言、C++、BCX、Autohotkey 或 Powerpro)都可以告诉 Illustrator、InDesign 或 Photoshop 执行操作。

Here is an example for Powerpro(you will need powerpro's com plugin), and this works in CS4 and CS5:

这是Powerpro的示例(您将需要powerpro的 com 插件),这适用于 CS4 和 CS5:

Function ComLoad() ;;MAKE SURE TO CALL .@ComUnload WHEN EXITING FUNCTION CALLS!
      static objname="Illustrator.Application"
      static com_status, com_type
      static appRef=com.create_object(objname)
      endfunction

Function ComUnload();;this is end the com calls and unload com
    com.unload
    endfunction

After you use the ComLoad() function, you then run any kind of method or function offered by the COM library. Here is how to use Powerpro to tell Illustrator to run your jsx or js file:

使用 ComLoad() 函数后,您可以运行 COM 库提供的任何类型的方法或函数。以下是如何使用 Powerpro 告诉 Illustrator 运行您的 jsx 或 js 文件:

;Run a script from anywhere
Function RunOtherScript(whatscript)
    If (file.Validpath(whatscript) == 0)do
        messagebox("ok","Whoops! That script doesn't exist!","ILL Runscript")
        quit
    endif
    .@ComLoad()
    appRef.DoJavaScriptFile(whatscript)
    .@ComUnload()
    quit

Here is an image of a floating toolbar that I made using Powerpro. The buttons are all attached to com functions. Most of the time I use the com functions to run external jsx scripts.

这是我使用 Powerpro 制作的浮动工具栏的图像。这些按钮都附在 com 功能上。大多数时候我使用 com 函数来运行外部 jsx 脚本。

enter image description here

在此处输入图片说明

[edit]

[编辑]

There is another way! You can use the Adobe Configurator to create new panels which are capable of launching scripts. You can design the panel in any way you like, and the results are quite similar in effect to the powerpro toolbar I've described above. In fact, I moved from the powerpro toolbar to an Adobe Configurator Panel.

还有一种方法!您可以使用 Adob​​e Configurator 创建能够启动脚本的新面板。您可以按照自己喜欢的任何方式设计面板,结果与我上面描述的 powerpro 工具栏的效果非常相似。事实上,我从 powerpro 工具栏移到了 Adob​​e Configurator Panel。

回答by George Profenza

If you place the .jsx files in the right location

如果您将 .jsx 文件放在正确的位置

Photoshop
folder location:
/Applications/Adobe\ Photoshop\ CS5/Presets/Scripts
menu location:
File > Scripts

Illustrator
folder location:
/Applications/Adobe\ Illustrator\ CS5/Presets.localized/en_GB/Scripts
menu location:
File > Scripts

InDesign
folder location:
/Users/{user}/Library/Preferences/Adobe\ InDesign/Version\ 7.0/en_GB/Scripts/Scripts\ Panel
menu location:
Window > Utilities > Scripts

These are the paths on OSX, it should be easy to find the equivalent on Windows for Photoshop and Illustrator, but for InDesign it might be easier to open the Scripts Panel and open the scripts folder using the Panel's context menu.

这些是 OSX 上的路径,在 Windows 上应该很容易找到 Photoshop 和 Illustrator 的等效路径,但对于 InDesign,打开脚本面板并使用面板的上下文菜单打开脚本文件夹可能更容易。

I know that you can run .jsfl scripts from the command line by opening Flash and passing the path to the .jsfl script as an argument, but that doesn't seem to work for .jsx files with InDesign.

我知道您可以通过打开 Flash 并将 .jsfl 脚本的路径作为参数传递来从命令行运行 .jsfl 脚本,但这似乎不适用于使用 InDesign 的 .jsx 文件。

HTH

HTH