Skip to main content
Known Participant
April 8, 2020
Answered

Running a jsx script with AppleScript in Photoshop using do javascript

  • April 8, 2020
  • 1 reply
  • 4916 views

Hi everybody! I just wrote a script in javascript for Photoshop and it works perfectly. I just wanted to implement an action in automator that opens Photoshop and runs this script. I tried to run an Applescript but I spent hours with the function "do javascript..." following the reference on javascript for Photoshop without success. I dont know if its a problem of syntax or its not the good function. I precise that this only line worked :

do javascript "alert(1)". And it showed 1, but no way to run my script. Even "do javascript "alert("Hello World") " doesnt work. Any help is welcome ! 

ps: Im a beginner!

This topic has been closed for replies.
Correct answer Stephen Marsh

One method to point to an external JSX file:

 

 

tell application "Adobe Photoshop CC 2019"
	activate
	do javascript of file "/Users/username/Desktop/alert.jsx"
end tell

 

 

Or slightly different syntax:

 

 

tell application "Adobe Photoshop CC 2019"
	activate
	do javascript (file "Users:username:Desktop:alert.jsx")
end tell

 

 

To run the JS code inline within the AppleScript:

 

 

tell application "Adobe Photoshop CC 2019"
	activate
	do javascript "app.runMenuItem(stringIDToTypeID('selectAllLayers'));"
end tell

 

 

And if there are double quotes in the code, they need to be escaped as shown here:

 

\"

 

http://community.adobe.com/t5/Photoshop/Request-Add-Scale-Styles-option-to-quot-resize-image-quot/m-p/10637321

 

Good luck Tonio1812 and please let the forum know how it worked out for you!

1 reply

Stephen Marsh
Community Expert
Stephen MarshCommunity ExpertCorrect answer
Community Expert
April 8, 2020

One method to point to an external JSX file:

 

 

tell application "Adobe Photoshop CC 2019"
	activate
	do javascript of file "/Users/username/Desktop/alert.jsx"
end tell

 

 

Or slightly different syntax:

 

 

tell application "Adobe Photoshop CC 2019"
	activate
	do javascript (file "Users:username:Desktop:alert.jsx")
end tell

 

 

To run the JS code inline within the AppleScript:

 

 

tell application "Adobe Photoshop CC 2019"
	activate
	do javascript "app.runMenuItem(stringIDToTypeID('selectAllLayers'));"
end tell

 

 

And if there are double quotes in the code, they need to be escaped as shown here:

 

\"

 

http://community.adobe.com/t5/Photoshop/Request-Add-Scale-Styles-option-to-quot-resize-image-quot/m-p/10637321

 

Good luck Tonio1812 and please let the forum know how it worked out for you!

Tonio1812Author
Known Participant
April 9, 2020

Hi Stephen! Thank you so much, the first two propositions worked perfetly. Its strange because I remember to have tried something very similar to the second... Maybe I was confuse with the "<" and ">" put in the documentation of Adobe about Applescript scritping. In any case, thank you very much!

Stephen Marsh
Community Expert
Community Expert
April 10, 2020

Thank you for the feedback, this courtesy is not always shown, correct answers or likes are not always given.

 

Even though I mostly use a Mac for Photoshop, I don't usually play with AppleScript as JavaScript has wider application. I get the impression that the AS+JS syntax has changed over the years with different versions of Photoshop, so many older code examples one finds no longer work, not sure if this is fact or not, it is just my impression.

 

Anyway, your original inline code snippet for the Hello World alert had incorrect syntax. I mentioned this with the reference to using inline JS code within AS:

 

Incorrect: 

do javascript "alert("Hello World")"

 

Correct:

do javascript "alert(\"Hello World\");"

 

The double quotes within the JS code need to be escaped with a backslash character \" – I only found this out recently myself, I'm sure it is basic 101 stuff for those used to working with AS.

 

As JS does not care if one uses double or single quotes (as long as they are consistent in opening/closing code) – the other option is to use single quotes in the JS code, such as:

 

Alternative:

do javascript "alert('Hello World');"

 

Then there is no need to escape the single quotes!