javascriptfunctionapplescriptphotoshop-script

Call Javascript function from external file with Applescript


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"


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.