Sign Up Now
Twitter Facebook Flickr Buzz
PhotoshopSociety.org
 
Search:   
 
Social Networks

LOGIN     

Go
Forgot Password? Go Join Now
Sign Up for Starter's Pack (Free)
Call (800) 223-8720
Email custserv@photoshopsociety.org
 
Need Web Solutions? Get Free Sample Issue

Inside Photoshop: Search Articles

  Search Library:  
 
2018 |  2017 |  2016 |  2015 |  2014 |  2013 |  2012 |  2011 |  2010 |  2009 |  2008 |  2007 |  2006 |  2005 | 
 

Start writing your own scripts by learning AppleScript basics

Added on Sunday 27th of February 2005 12:25 am EST
 

Applications:

Adobe Photoshop CS, AppleScript

Operating System:

Macintosh OS X 10.3 or later

 

In the June 2005 issue of Inside Photoshop, we introduced you to the basics of using scripts to automate tasks in Photoshop. In this article, we’ll walk you through the process of planning and executing scripts for Photoshop. By tackling small projects like this one first, you’ll be better suited to take on more-complex scripting exercises later on.

 

Tell Photoshop what to do

In this article, we’ll go deeper into the world of scripting Photoshop. We’ll begin by explaining how to plan a script before you write the code. Next, we’ll go over how to use Apple’s free Script Editor application and Common AppleScript commands. Then, we’ll detail the object model used by AppleScript and other scripting languages so you’ll be familiar with the types of commands Photoshop needs. Finally, we’ll tell you about the role of dictionary files and when you should use them to save time and effort.

 

Planning a script

Before we start programming our script, it’s important to know exactly what we want it to do. Creating a basic flowchart, such as the one shown in Figure A, will help us break its functions down into smaller tasks. For example, any time the script must make a decision, we’ll want to use an “if/then” sequence (see “Common AppleScript commands” later in this article).

 

 

 

Figure A:

This basic flowchart outlines the tasks our sample script will perform.

 

Working with Script Editor

Apple’s free Script Editor application is found in the AppleScript folder inside the Applications folder. Launching this application opens an untitled script window, as shown in Figure B.

 

Figure B:

Don’t be thrown off by the unassuming Script Editor window, it’s actually a very powerful tool.

 

The Script Editor window has four buttons at the top including:

   Record. Allows you to automatically create scripts based on the actions you take while recording. Unfortunately, Photoshop doesn’t support this feature.

   Stop. Interrupts a script that’s running.

   Run. Executes the script. If any part of your script is uncompiled, Script Editor attempts to compile it before the script runs.

   Compile. Verifies the syntax of the script. If Script Editor finds a problem, it highlights the first error and displays an error message.

 

Inline Note:

Note: Compiling is only the first line of defense when debugging a script. Even if a script has proper syntax, logical errors can prevent it from running as desired.

End Note

 

By default, when you first enter code in Script Editor, it appears as purple Courier text, as shown in Figure C. Successfully compiled code changes to Verdana, as shown in Figure D. In addition, Script Editor uses color and formatting combinations to highlight operators, variables, comments, and other elements. The default settings are shown in Figure E. You can customize all these settings using the Formatting pane of Script Editor’s Preferences dialog box. New code you add to a compiled script automatically appears as uncompiled, so you can easily see the status of the code at a glance.

 

Figure C:

An uncompiled script in Script Editor appears purple.

 

Figure D:

After clicking the Compile button, the same script appears with commands, variables, and other elements formatted.

 

Figure E:

You can change the default display formatting for compiled scripts in the Formatting dialog box.

 

Script Editor uses indenting to indicate code within a particular command set. Note that we didn’t indent anything when we entered our original code in Figure C. Script Editor automatically adjusts the indenting when it compiles the script. We can enter tabs as we type if desired, but it isn’t necessary.

 

Common AppleScript commands

Our sample script will use several of the most common AppleScript commands. Here’s a quick overview of the ones we’ll use.

 

The tell command

The tell command indicates which application or element you’re giving instructions to, as shown in this example:

Art: please make sure that indents in code samples appear as shown in the final version.

 

tell application “Adobe Photoshop CS”

  change mode of current document to CMYK

end tell

 

AppleScript syntax also requires us to indicate where the tell command code stops with the statement end tell.

 

The display dialog command

This command creates a basic dialog box like the one shown in Figure F. By default, dialog boxes have two buttons: a highlighted OK button that causes the script to continue, and a Cancel button that stops the script.

 

Figure F:

This basic dialog box was created using the display dialog command.

 

To display a text string in a dialog box, use quotation marks to set it apart from the code, as in the following example:

 

display dialog "No images to convert."

 

You can also use a dialog box to display the value of a variable:

 

display dialog x

 

In this example, the dialog box displays the value of the variable x, provided the value can be converted to text.

You can also mix text and variables by setting them apart with an ampersand (&). For example, the dialog box shown in Figure F was created using variables named source_folder and item_count with this line of code: 

 

display dialog "The folder " &

=> source_folder & " contains " &

=> item_count & " Items.  Begin conversion?"

 

The set command

The