AppleScript running JavaScript

Hi, new AppleScripter here . . .

I’m trying to use AppleScript to run Photoshop CS2 and run a JavaScript in Photoshop using a variable passed from the AppleScript.

For example:


Applescript determines FilePath X and stores it in variable myVarAS

AppleScript tells Photoshop to

do JavaScript "

var myVar = ’ " & myVarAS & " ’
scriptaction( myVar )
function scriptaction( myVar)
javacode ‘blah, blah, blah’
save file in filepath ( myVar )

I’m pretty sure I can get this to work, if one of you guys would tell me how to pass the variable from AppleScript to JavaScript!

If I hard card the path in the JavaScript, it writes the WBMP file just fine. But no matter what I try, I can’t get the JavaScript to recognize the variable!

Is there some secret code or magic mumbo-jumbo I have to write, or can I only perform this rite on new moons or something?

What gives?

Adobe’s guide recommends the “do javascript” with an external script. But this would make implementing over many workstations a task, due to the need of copying and linking two files, maybe even customizing for each workstation. Besides, that doesn’t work, either!!!

So my desired solution is to include the java code in my applescript. I just need it to work!

Any help would be appreciated!

Thank you.


I found someone had posted a simple way to do it on another forum, but I’ll post it here:

FIrst of all, if I enable the “ScriptingListener” plugin in Photoshop and perform my desired task, it records the actual Java code needed in a text file on my desktop. All I have to do is activate the listener, export the “Save for Web” feature of my choice, quit without saving and deactivate the listener. I’ve got 99% of the coding done!

All I have to do now is open the text file in TextEdit, crop to the area that does my action, change all quotes to back-slash () plus qoute, (example change " to "), select all and copy to the clipboard. I’m ready to place it in my AppleScript.

Find the activation area in the AppleScript, and say:

set theJavaCode to "{

paste from the clipboard and endcap the code with }"

then say

do javascript theJavaCode

Easy, right?

All that is difficult is what variable do you want to pass. I wanted to pass the path to my desire drive, and found that if I “Listened” while choosing my desktop, the script would fail every time. But once I actually selected my network drive while listening, the script recorded “/Volumes” before the path, and it worked!

So, in the javacode where it identifies the output path as something like:

desc4.putPath( id37, new File( "/Drive of choice/folder of choice" ) );

you change it to the path variable that your AppleScript built (remembering to use forward-slashes instead of colons).

If your variable is made by AppleScript like this:

set mFilePath to “/Volumes/Drive of choice/folder of choice/sub-folder of choice”

then change your javacode to something like this:

desc4.putPath( id37, new File( "" & mFilePath & "" ) );

I hope this helps anyone out there. It is a great trick, and I have to give credit to Mr. Ron Bishop and his willingness to post his methods on the web. Thanks Ron!!!