Scripting Photoshop with JavaScript : Hello World Style


   Who would have thought that it was possible to script Photoshop with such a language like JavaScript. One would think that it would be some proprietary language, or at the very least, Action script, which is a JavaScript type of language. Alas, JavaScript is the language of choice here and I won’t argue it as JavaScript is after all the assembly language of the web.

     So how does it all work anyways, and why would one use it? I for one, need to create three different blog images for each post, and creating each one of those documents is highly annoying, so scripting it is the natural choice. Where is the entry point then for this, how does one start? First off, you need two things, one woudl be a notepad type of editor, and the second would be Photoshop (duh), so lets get started:

 Quick Questions

Q. Where are the scripts stored?

A. They are stored at (Winders) C:\Program Files\{Your version of Photoshop, mine is CS2}\Presets\Scripts

image

Q. What file type are they?

A. They are JSX (the ‘X’ makes them, cool, that is why they added it => the evangelists told me so)

Q. Does it have to follow some convention, a type of API?

A. Yes, it does, but for the most part it is object oriented and all the objects are static in nature so you can just call api.foo whenever you want.

Q. Can we get started Already (*cough* code please)?

A. Of course!

   For our example we need to create three documents, so lets do that by first defining our type of unit measurement, which will be pixels:

var startMonkey = app.preferences.rulerUnits;
app.preferences.rulerUnits = Units.PIXELS;

   What we have done above is take the default unit measurements that is stored in our preferences and we put them in a variable called “startMonkey.” Next, we will create and open our new documents:

var newFoo = app.documents.add(630,215,128,"Foo");
newFoo = null;

var newSmallFoo = app.documents.add(70,113,128,"FooSmall"); newSmallFoo = null;

var newLongFoo = app.documents.add(218,60,128,"FooLong");
newLongFoo = null;

var newScreencastFoo = app.documents.add(388, 250,128,"FooScreencast");
newScreencastFoo = null;

   What we did above is define three documents, added them to the current application by calling “app.documents.add” and passing in parameters of width, height, pixel ratio, and document name.

  Now lets put the default ruler measurement back at the end of the document:

app.preferences.rulerUnits = startMonkey;
 //This is the variable we defined earlier

When we run this we are presented with exactly what we were looking for:

To  RUN this SCRIPT

1. File >> Script >> {Name of the script}

ScriptAccessPhotoshop

2. Done.

ScriptAccessPhotoshop'

Sweet, the script works and I never have to refer to my notebook again to get the sizes of those images (why did I even make them so screwed up?). In the next portion, we are going to extend this example to run some custom actions in JavaScript and talk about what actions are (think Macros for Photoshop).w00t!