Question

Is it possible to call a javascript file function in applescript? I can get it to work when written 100% in the applescript file or I can use "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.

Update:

I posted it when someone else ask but PathtoJSX is the "Path to Javascript file (.jsx)" so PathtoJSX = file "desktop:yourFile.jsx"

Was it helpful?

Solution

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:

The contents of the jsx file could be:

testFunc(arguments);

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

and the AppleScript could be:

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. 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.

OTHER TIPS

Has this changed since it was first solved? The following worked for me:

JavaScript

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

testFunc(arguments);

AppleScript

tell application id "com.adobe.Photoshop"
    activate
    do javascript of file "pathToFile" with arguments {"foo", "bar"}
end tell

See also:

Applescript + Javascript and scriptListener

AppleScript wants you to escape double quotes in inline JavaScript

Try this:

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

Regards.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top