一般情况下,当你编写一个.jsx脚本来自动化Adobe产品(如InDesign、Illustrator或Photoshop)时,你需要在ExtendScript IDE中编写、调试和执行脚本。是否可以绕过ExtendScript并从第三方程序运行脚本?
我认为Adobe产品有内置的JavaScript解释器,ExtendScript可以连接到该解释器以访问Adobe对象模型并自动化其软件。我希望能够直接连接到该解释器并像在ExtendScript中一样运行jsx文件。
一般情况下,当你编写一个.jsx脚本来自动化Adobe产品(如InDesign、Illustrator或Photoshop)时,你需要在ExtendScript IDE中编写、调试和执行脚本。是否可以绕过ExtendScript并从第三方程序运行脚本?
我认为Adobe产品有内置的JavaScript解释器,ExtendScript可以连接到该解释器以访问Adobe对象模型并自动化其软件。我希望能够直接连接到该解释器并像在ExtendScript中一样运行jsx文件。
你是在使用Mac电脑吗?如果是,你可以使用AppleScript和osascript
工具来执行JavaScript。以下是一些示例:
将以下内容保存为~/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中打印到stdout。相反,将要返回的值作为JSX文件的最后一条语句放置;它将被osascript
打印出来。(原因在于:JSX文件中的最后一个值是do javascript
AppleScript语句的返回值。这也是AppleScript文件中的最后一个值,并且osascript
会打印最终值。)
您从JavaScript返回的值可以是数字、字符串、数组或任何其他在转换为字符串时保留其值的对象。如果您想返回复杂对象,则需要#include一个JSON库并在对象上调用.toJSONString()
。
要向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编辑器中打开Illustrator字典。
对于Windows用户,您可以使用vbs脚本。通过向cscript
命令提供参数来将参数传递给.jsx脚本,例如: 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(您应该在命令提示符窗口中看到它)。
这在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 )
"%%f\..\..\Photoshop.exe" C:\completepathto\my.jsx
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
在使用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
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
这些是在OSX上的路径,对于Photoshop和Illustrator应该很容易找到Windows上的等效路径,但对于InDesign来说,通过打开脚本面板并使用面板上下文菜单打开脚本文件夹可能更容易。
我知道你可以通过打开Flash并将.jsfl脚本的路径作为参数传递来从命令行运行脚本,但似乎对于InDesign中的.jsx文件不起作用。
希望对你有所帮助。
/Adobe Illustrator CC/en_US/Scripts
。 - George Profenza这个问题很旧了。我假设:
我不确定您是否想要向脚本传递参数(这种情况下,我的简单解决方案将不起作用,或者可能需要一些恶心的解决方法)。
幸运的是,After Effects 有一个简单的方法来实现这一点。您可以启动 cmd 并键入以下命令:
afterfx -r C:/Users/me/myscript.jsx
您可以使用Extend Script来运行它。 在创意云上有一个免费的扩展程序,可以帮助您在Illustrator、After Effects、Photoshop和Premiere Pro中快速运行脚本。 您可以在Adobe Exchange(liveview)上找到它。
do javascript "..."
更改为do script "..." language javascript
。 - crishoj