Scripting with the ExtendScript Toolkit
Recently, we've been looking at the scripting feature in Adobe Photoshop. While you can write your scripts with pretty much any program that can read and write text files, this isn't necessarily the best way to do so. It's time to introduce two tools that come with Adobe Creative Suite that can make scripting much easier.
Running the ExtendScript Toolkit is easy. You'll find it in the same Creative Suite program group on your Start menu as the rest of the Create Suite applications you have installed. Once you start it, you'll need to use the dropdown list in the upper left to target your script to Photoshop. By default, scripts you write in the ExtendScript Toolkit will run against the Toolkit itself. If you have both 32-bit and 64-bit builds of Photoshop installed, they will both be listed as will any other installed Adobe applications that are scriptable. If you select an application that is not currently running, the Toolkit will prompt you to start it when needed.
The main window in the Toolkit is where you will write your code. If you already have a script you want to work on, you can use the standard File menu options to open it. You'll notice that the syntax of your script is helpfully color coded based on the function of each word to make it more readable. This alone is enough reason to make use of the ExtendScript Toolkit but there's much more. As you type, the Toolkit will attempt to auto-complete words for you based on a list of language keywords, functions, and the like. There are also a number of other standard IDE features such as finding matching curly braces, undo and redo, syntax checking, and so forth. If you have experience programming in other contexts, you should feel right at home with the ExtendScript Toolkit. If you're new to programming, you'll grow to appreciate this sort of thing, trust me.
At the top right of the main window you should find a row of buttons with standard icons for play, pause, stop and similar functions. To run your script, click on Play button. If you then switch over to Photoshop, you should be able to see the results. If your script contains a syntax error, something goes wrong or things still don't make sense to the scripting engine when run, the offending line will be highlighted so you can correct it. You should find an error message describing the issue at the bottom of the window.
A large part of the time spent programming ends up being debugging. Thankfully, the ExtendScript Toolkit provides all the standard debugging features. You can set a breakpoint by clicking on the line number to the left of any line to force your code to stop at that point so you can check the current state of things. A red circle will appear at that line. If you click on it again, the icon will turn gray and the breakpoint will be disabled temporarily. Click on it a third time and the breakpoint will be deleted along with the icon that indicated where it was. Breakpoints can also be made conditional if you don't always want to see them. Check the documentation for details.
Down the right hand side of the ExtendScript Toolkit are a number of smaller but quite helpful panels.
The "Scripts" panel lets you easily select from the existing scripts known to a given target (a scriptable program such as Photoshop). It also lets you maintain a list of "Favorites" for easy access. Use the fly-out menu in the upper right of the panel to add or remove Favorites.
The "Breakpoints" panel lets you manage all the currently defined breakpoints in your open script.
The "Call Stack" panel lets you keep track of how you got to where you are during debugging. It's not much use for simple scripts, but if you start to create nested function calls, it can definitely help you see the forest for the trees.
The "Data Browser" panel lets you easily keep track of defined objects and also shows the current value of variables during debugging.
The "Functions" panel shows you what functions you have created in your script. Functions help to modularize code to make it easier to understand and maintain. Double clicking on a listed function will cause your cursor to jump to its definition in the main code window.
ExtendScript Toolkit also supports the notion of code profiling which means that it can keep track of how often various parts of your script get executed during testing and how long they took to run. Advanced developers can use this to better optimize their code and ensure all parts have been adequately tested.
A feature even beginners will appreciate is the Object Model Viewer found on the Help menu. Here you will find all the various objects Adobe has defined and their properties. Consider it a quick reference so you can avoid consulting the full documentation all the time. There's a search box to easily find things too.
It can take time and the right mindset to make use of Photoshop scripting. Some people take to it more quickly than others so don't worry if all this seems a tad complicated at first. As hard as it may be to believe, scripting is generally regarded as being easier to learn than other kinds of programming. Without debating the merits of that assertion, the ExtendScript Toolkit should make scripting at least somewhat easier for you, and that's what's important.