0

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"

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

The key for me was the 'arguments' variable name to be passed in, being required. I was trying to do my own 'args' argument but it didn't work - so this answer helped with that.
1

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

Comments

0

Try this:

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

Regards.

2 Comments

This is a redundant post. The question was already answered with the same information. Please read all answer before posting.
This is useful because it shows a different syntax of calling a JS file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.