简体   繁体   English

使用Applescript从外部文件调用Javascript函数

[英]Call Javascript function from external file with Applescript

Is it possible to call a javascript file function in applescript? 可以在applescript中调用javascript文件函数吗? I can get it to work when written 100% in the applescript file or I can use "do javascript file some file". 我可以在applescript文件中100%写入时工作,或者我可以使用“do javascript file some file”。 Is there a way to mix the two? 有没有办法混合这两个? Something like this: 像这样的东西:

tell application id "com.adobe.Photoshop"
    tell current document
        do javascript "function(Variable1,Variable1)" in file PathtoJSX
    end tell
end tell

The function is located in the external file but I want to pass it variables from applescript. 该函数位于外部文件中,但我想从applescript传递变量。

Update: 更新:

I posted it when someone else ask but PathtoJSX is the "Path to Javascript file (.jsx)" so PathtoJSX = file "desktop:yourFile.jsx" 当其他人问我但是PathtoJSX是“Javascript文件的路径(.jsx)”时我发布了它,所以PathtoJSX =文件“desktop:yourFile.jsx”

I think what you want to do is have the function part of the code in the file and send the arguments via AppleScript as a list (Array), which is pretty easy to do: 我想你想要做的是在文件中有代码的函数部分,并通过AppleScript作为列表(Array)发送参数,这很容易做到:

The contents of the jsx file could be: jsx文件的内容可以是:

testFunc(arguments);

function testFunc(t) {
 alert('argument one: ' + t[0] + '\r' + 'argument two: ' + t[1]);
}

and the AppleScript could be: 而AppleScript可能是:

tell application id "com.adobe.Photoshop"
  activate
  do javascript PathtoJSX with arguments {"foo", "bar"}
      --I tested using:-- do javascript (choose file) with arguments {"foo", "bar")
end tell

Whatever you use for the JS code (text or file), you just make sure you use the arguments array reference in that code. 无论您使用什么JS代码(文本或文件),您只需确保在该代码中使用arguments数组引用。 Note that if you test this (as I did) by using a text variable instead of an alias (file reference), the return character would need a double escape. 请注意,如果您使用文本变量而不是别名(文件引用)来测试(就像我一样),则返回字符需要双重转义。

Try this: 尝试这个:

 tell application "Adobe Photoshop CS4"
 activate
 do javascript (file "/Users/Michael/Desktop/somescript.jsx") with arguments {"foo", "bar"}
end tell

Regards. 问候。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM