If you’re like most web designers, you spend a lot of time in Photoshop working on images. But with the right tools, you can save a lot of time by automating some tasks. In this article, we’ll show you how to use JavaScript to automate common tasks in Photoshop, like cropping and resizing images. First, let’s look at how we can crop an image using JavaScript. We’ll start by creating a new file in Photoshop and saving it as cropped.jpg. Next, we’ll open the cropped.jpg file in our editor of choice and add some basic HTML code to it: Now that we’ve created the file, all we need to do is add some JavaScript code to our editor of choice (we’re using JSFiddle here), and we’ll be ready to go: var img = document.getElementById(“cropped”); img.onload = function(){ // Cropping code }; In this code block, we’re first getting hold of the img element that contains our cropped image (in this case, cropped.jpg). We then set up an event handler for onload , which will be triggered when the image has been loaded into memory (in other words, when Photoshop has finished loading it). Within our handler function, we simply call a function that performs our cropping operation: function crop() { // Cropping code } We could have just as easily written this same code in Python or Java – it’s really just a matter of choosing your preferred programming language – but for simplicity’s sake, we’ve written it in JavaScript here. Now all we need to do is save our edited file (in this case cropped-jsfiddle-1.js) and reload the page in your browser – voila! Our cropped image will appear within the img element on load: ..
What could JavaScript POSSIBLY have to do with an Image Editor? Using this platform independent scripting language, today we’ll learn to create automated tasks either Mac or PC versions of Photoshop will run. Photoshop has an API (Application Programming Interface) that allows those with know-how to create scripted tasks and automate even surprisingly complicated actions. With the advantage of using a language as powerful as JavaScript, geeky programmers can come up with clever uses of conditional logic, variables, and sometimes applications outside of Photoshop. Take a look through these simple JavaScripts and learn how to use them in Photoshop.
Hello World!
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.
To create this basic document, you’ll need a text editor like Komodo Edit, Notepad 2, or Fraise for Mac. Make sure to save your JavaScript file as a .JSX or a .JS file. Photoshop will easily read either in OS X or Windows. Save your file in any place you can locate later.
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.
Navigate to File > Scripts > Browse to look for your saved JavaScript file.
Navigate to where you have saved your “Hello World” JavaScript file and load it.
Photoshop creates a document to the specifications in the JavaScript file: 2 by 4 inches, with the text object “Hello, World.”
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:
// 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.
Find your JavaScript file.
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.
JavaScript, Applescript, or Visual basic have guides for scripting available for download, going as far back as Photoshop CS2. Download any and all of them here.