通常,当您编写.jsx脚本来自动化一个Adobe产品(如InDesign、Illustrator或Photoshop)时,您需要从ExtendScript集成开发环境中编写、调试和执行该脚本。是否可以绕过ExtendScript并从第三个程序运行该脚本?
我认为Adobe产品有一个内置的JavaScript解释器,ExtendScript可以连接到它来访问Adobe对象模型并使其软件自动化。我希望能够直接连接到该解释器并运行jsx文件,就像我在ExtendScript中所做的那样。
发布于 2012-01-27 13:37:44
你用的是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编辑器中打开插图词典。
发布于 2014-09-04 11:02:01
对于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)Wscript.Echo将返回.jsx文件返回的最后一行。.jsx文件示例可以是:
function main(argv) {
alert(argv[0]);
return "test";
}运行后,您应该看到Illustrator (或任何adobe程序)警告"hello“,然后"test”将返回到stdout (您应该在命令提示符窗口中看到它)。
发布于 2014-03-26 09:54:56
这在windows中有效:
"C:\Program Files (x86)\Adobe\Adobe 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 )然后,您可以在x.lst中处理每一行。注意:整个"@ for“应该在一行上,为了可读性,我将它分成多行。
如果你认为只会有一个Photoshop (而不是Elements),那么你可以将"echo %%f"改为
"%%f\..\..\Photoshop.exe" C:\completepathto\my.jsx https://stackoverflow.com/questions/3846626
复制相似问题