AddThis Social Bookmark Button

Listen Print

AppleScript: Getting Photoshop and Outlook toTango

by Bruce W. Perry
05/23/2001

In an odd role reversal, many of us repeat the same computing tasks hour after hour (open emailer, create new message window, type in email address, etc.) instead of viewing these actions as sequences that can be automated. The computer is the machine that is designed to make life easier, right? Automating repetitive tasks is what AppleScript is designed for, freeing us from the bonds of machine-like behavior.

To show you what AppleScript can do at an intermediate level, this article will walk you through a script that saves an image file, then sends the file as an attachment to a new email message.

Tools

What do you need to write or run this script, other than a Macintosh? Adobe Photoshop 5.5 will save the image file to the desktop, and Outlook Express 5.02 will create and send the email message. The script was written and compiled in Script Editor, the free development tool that comes with every Macintosh. This script was tested on Mac OS 9.0.4 and 9.1, as well as on Mac OS X (automating Photoshop and Outlook in the classic environment). The code can be viewed in its entirety.

Assumptions

I am assuming that you have an image open in Photoshop's native file format (perhaps an important digital photo that you have scanned into the computer). You are so excited about this image (which depicts you finishing a triathlon) that you are going to send it to that person at work who has doubted your athletic potential. I also assume that your machine is already connected to the Internet. When you launch this AppleScript by double-clicking its icon it will take the following actions:

  1. Tell Photoshop to perform the SaveAsJpeg action on the open file. This action needs to have already been defined by you within Photoshop. The next section of this article briefly describes Photoshop actions. The jpeg file gets saved on the desktop.

  2. The script then asks the user to choose the file, using a dialog window that provides access to the Mac file system. The script has to know the filename and location to insert the file as an attachment to the new email message.

  3. The script tells Outlook Express to create a new email message window with the file attached, and then to send the message.

Photoshop Actions

First, we want to save the digital photo as a jpeg. Photoshop has its own automation tool called "actions." Figure 1 shows the SaveAsJpeg action we have defined for use by our script.

Figure 1
Figure 1

In essence, we are using AppleScript to automate Photoshop's own automation tool. This may seem redundant but Photoshop has limited built-in scriptability with AppleScript, unless you purchase and install a Photoshop plug-in called PhotoScripter from Main Event Software. Without a PhotoScripter license one of the few things you can do with Photoshop and AppleScript is to tell the image editor to execute one of its own actions.


This year O'Reilly & Associates is expanding its presence in the Macintosh developer and administrator markets. As part of this move, O'Reilly is working with Apple to produce titles on Mac OS X. For details, read Tim O'Reilly's article, O'Reilly and Apple Team Up.

This is exactly what we have done with the beginning of our script, which looks like this:

Related Reading

AppleScript in a Nutshell
By Bruce W. Perry

Table of Contents
Index
Sample Chapter
Author's Article

Read Online--Safari Search this book on Safari:
 

Code Fragments only

display dialog ¬
"Saving the jpeg now." giving up after 4

saveTheImage()

The first line displays a dialog window telling the user what is going on, then the window is automatically dismissed after four seconds (but the script continues). The time limit was included to keep the script going, even if the user never clicks on the window's "OK" button.

Then the script calls the saveTheImage() handler. Handlers, or subroutines, in AppleScript are blocks of code that are defined in one place for convenience, then called one or more times in the script by the handler name. They are like functions or methods in other languages. Here is the saveTheImage() subroutine definition:

on saveTheImage()
	tell application "Adobe® Photoshop® 5.5"
		activate
		do script "SaveAsJpeg"
	end tell
end saveTheImage

The subroutine definition begins with the reserved word on (you can also use to) followed by the subroutine name and a set of empty parentheses. As in other languages, such as JavaScript, you can pass any parameters to a subroutine by including them within the parentheses, separated by commas. A script calls a subroutine by providing its name and the parentheses (as well as any parameters), such as "saveTheImage()".

When the script calls saveTheImage(), Photoshop 5.5 becomes the active application on the desktop, as its windows and menus become highlighted. This is accomplished in saveTheImage() by using the activate command. Photoshop has to be made the active application because the SaveAsJpeg action that we have defined requires some user intervention.

The commands that the script sends to Photoshop are nested within a tell block, which is how AppleScript specifies the program that will receive its script commands.

The Action

Remember that an action is an automation tool that Photoshop provides. AppleScript tells Photoshop to play, or trigger, an action with Photoshop's do script command. The do script command is followed by the name of the action in quotes, for example:

do script "SaveAsJpeg"

What does the SaveAsJpeg action do? Just in case the user has forgotten to open up that prized Photoshop image file, the action first displays a dialog box with which the user can select the file. The native Photoshop files can be identified with a .psd suffix.

The user can dismiss the dialog box by clicking cancel if the user already has the native file open in Photoshop. The action then flattens the image if it has more than one layer. Next the action saves the file in jpeg format, giving the user the opportunity to name the file (if the original file is called "newimage.psd," then the user might save it to "newimage.jpg").

Telling "Me"

This next line of code follows the call to saveTheImage():

tell me to activate

Me is an AppleScript reserved word that specifies the script applet itself, in this case the one that is controlling Photoshop and Outlook Express. We have to make the script applet the active application instead of Photoshop, so that the user can see (and optionally respond to) the next dialog window. Without doing this the script applet dialog window might be hidden by Photoshop's windows.


Visit the O'Reilly Network's Mac DevCenter for the latest information on Apple technologies.

We then tell the user what's going on with another call to the display dialog command:

display dialog "setting up and sending" ¬
	"the email message" giving up after 4

This dialog window will also disappear in four seconds, allowing the script to continue merrily on its way. Clicking the Cancel button in the dialog window produced by the display dialog command will stop the script's execution, and the email will not be sent.

Sending the Email

Now that we have saved the jpeg, we can prepare and send our new email message. This is the purpose of the next line in the script:

sendTheEmail()

You may recognize this as another subroutine, or handler, call. This subroutine creates what Outlook Express calls a "draft window" object, which is a window that contains all of the email information and any attached files. Figure 2 shows the definition of a draft window in Outlook's Dictionary, which is where you will find the commands and objects that a program makes available to AppleScript.

Figure 2
Figure 2

You can open a program's Dictionary window by dragging the program's icon over the ScriptEditor icon, or by using ScriptEditor's "File: Open Dictionary..." menu item.

Here is what the sendTheEmail handler looks like.

Targeting Outlook

Before we make Outlook the front-most application, we first get the user to identify the image file with this line of code:

set theAttachment to (¬
		choose file with prompt "Select the file attachment:")

This statement uses the choose file scripting addition command, which displays a window that allows the user to select a file from the file system. Scripting additions are extensions to the AppleScript language that increase the number of commands that you can use in your script. Some scripting additions are made available with the standard Mac OS installation (such as choose file and display dialog), and others are written by third parties and can be added to the computer.

The next part of the script tells Outlook to create a new draft window with default values for the email recipient, subject, the Post Office Protocol (POP) account that Outlook will use to send the message, and the body of the message. A more complex script could get one or more recipient addresses from user interaction or Outlook's Address Book.

The subroutine makes a new draft window and attaches the specified file to the message with these lines:

set draftWindow to make new ¬
		draft window with properties ¬
		{subject:emailSubject, content:emailContent,¬
		to recipients:emailDestination, account:emailAccount} 
					
	make attachment at draftWindow ¬
		with data {file:theAttachment, encoding:binhex}

The first code statement uses the previously initialized variables to provide the draft window's attributes, namely the subject, content, who will receive the email (the to recipients property or attribute), and the POP account that will be used to send the email.

The second code statement creates a new attachment (literally, an "attachment" object that Outlook defines in its Dictionary) for the draft window. The theAttachment variable points to the jpeg file that will be attached to the email.

An Outlook attachment has an encoding property, which the script sets to a predefined value of binhex. This is because we already know that our doubtful colleague uses a Mac, and therefore the email program will probably be able to decode a binhex-encoded attachment. In Outlook, the value for this property can be any one of these constants: no encoding, 7bit encoding, 8bit encoding, binhex, base64, uuencode, AppleSingle, AppleDouble, quoted printable, unknown encoding.

Finally, the script sends the email using the send Outlook command, with the line send draftWindow. The email-related code is nested inside an AppleScript try statement, which allows the script to handle any errors that this code might cause.

Now you have an AppleScript that can be reconfigured to automate several other tasks. You can take pieces of this script to build more complex email- or image-related scripts, thus greatly reducing your manual labor.


Bruce W. Perry is an independent software developer and writer. Since 1996, he has developed Web applications and databases for various nonprofits, design and marketing firms, ad agencies, and digital-music specialists. Before working in the Web field, Perry remained tethered to his portable and desktop Macs while writing environmental law books and newsletters. When not hacking or writing, he loves cycling, duathlons, and climbing mountains in the U.S. and Switzerland. He lives in the Newburyport, Massachusetts area with his wife Stacy LeBaron and daughter Rachel. He has recently finished writing O'Reilly's AppleScript in a Nutshell. Bruce may be reached at bwperry@parkerriver.com


O'Reilly & Associates will soon release (June 2001) AppleScript in a Nutshell.