Running a jsx script with AppleScript in Photoshop using do javascript

Explorer ,
Apr 08, 2020 Apr 08, 2020

Copy link to clipboard

Copied

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!

TOPICS
Actions and scripting, Mac

Views

827

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct Answer

Adobe Community Professional , Apr 08, 2020 Apr 08, 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('selectAllLa...

Likes

Translate

Translate
Adobe Community Professional ,
Apr 08, 2020 Apr 08, 2020

Copy link to clipboard

Copied

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

 

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

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Apr 09, 2020 Apr 09, 2020

Copy link to clipboard

Copied

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!

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Apr 09, 2020 Apr 09, 2020

Copy link to clipboard

Copied

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!

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Apr 15, 2020 Apr 15, 2020

Copy link to clipboard

Copied

Hi, sorry for the delay I just saw your answer now. Thank you for the tip, you probably saved me a lot of time again !

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Feb 22, 2021 Feb 22, 2021

Copy link to clipboard

Copied

I know it's been a year since this was posted but I just spent an hour trying to find an answer about how to run jsx scripts for photoshop from command line and this is the first answer I've found that worked, so I just wanted to say thank you so much for sharing this!

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Feb 22, 2021 Feb 22, 2021

Copy link to clipboard

Copied

LATEST

It took me a lot of time and effort to track down code that worked, it appears that there is a lot of older code on various web pages and forums that no longer works with newer versions of Photohsop. Glad it helped you!

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines