The most basic of basic programs, the Photoshop CS5 Scripting Guide provides a sample “Hello World” script to help us leap in and get started. We can see a few things going on here: units are set to inches, a new document is created, and text is added simply, using the Photoshop API.
Make sure you download the Scripting Guide for your version of Photoshop and copy the text from the PDF, rather than retyping it!
Open Photoshop. Any version dating back to Creative Suite 2 will work fine, although your code may be different.
Naturally, this opens up all sort of possibilities. Let’s take a quick look through another basic one, using these same simple tools.
Creating a New Page with the Events Manager
Simply editing the “Hello World” document, we can create a script that will create a standard paper size at a high resolution. The bits about the text object are removed and the comments have been changed, as well.
Note that the app.documents.add has different values than our “Hello World” file. “8.5” and “11” are still inches, but 300 is the resolution of the file.
Here is the code to copy and paste, if you are so inclined:
|// New Canvas Script
// Remember current unit settings and then set units to
// the value expected by this script
var originalUnit = preferences.rulerUnits
preferences.rulerUnits = Units.INCHES
// Create a new 8.5 x 11 inch document and assign it to a variable
var docRef = app.documents.add( 8.5, 11, 300)
// Release references
docRef = null
artLayerRef = null
textItemRef = null
// Restore original ruler unit setting
app.preferences.rulerUnits = originalUnit
Save your document as a .JS or a .JSX file anywhere you care to store it.
If you work on multiple machines with Photoshop, you may wish to save it in your Dropbox folder.
Return to Photoshop.
Under the same File > Scripts menu, you’ll find “Script Events Manager.” Open it.
You’ll get this dialog box. You can apply Scripts to many different Events, but for this one, we’ll have Photoshop run our script upon “Start Application.” You can browse to your script by clicking the pull-down menu that reads “Clean Listener” in the illustrated screenshot.
Add your custom script and click “Done.”
Restarting Photoshop, we find it opens an 8.5 inch by 11 inch page at 300 dpi resolution, just as we scripted.
- Published 11/8/10