The Art of AppleScript

The AppleScript Interface

Texting with Adobe Photoshop and Illustrator and the Mac Desktop

Use AppleScript Mac Automation Scripting to interact in written text with Adobe Photoshop, Adobe Illustrator, and other applications and system components of the Mac Desktop.

Coding for Creativity

A key feature of AppleScript Mac Automation Scripting is that we can use it to get work done and solve problems even if we don’t have any computer programming experience. AppleScripts can be read and written and Copy/Pasted and tweaked and customized by artists, photographers, designers, writers, editors, managers, and everyone who gets work done with a Mac. Not because we want to become computer programmers, but because we want to automate away our grunt work and spend more of our time on our creative work.

Creative and Grunt

The work you do with Photoshop or Illustrator can be divided into two broad categories:

creative work
drawing, painting, layout, typography, concept, execution, story, tone, color, style, culture, delight
grunt work
templating, importing, exporting, converting, resizing, processing, batch processing, measuring, aligning, fixing

AppleScript Mac Automation Scripting is a way to describe your grunt work tasks to your Mac applications as lists of things to do. Your Mac applications answer by doing the grunt work for you, freeing up more of your time, energy, and focus for creative work.

The Photoshop and Illustrator Text Commands Window

You may not even have realized that Photoshop and Illustrator and other Mac apps have a text commands window because it is subtly hidden in the Mac’s built-in Script Editor application. Launch Script Editor and create a new document and you are ready to text with your Mac apps in order to ask them to do work for you.

Write the following message in Script Editor on a Macintosh with Adobe Photoshop installed and Photoshop will answer. This conversation is at the heart of AppleScript Mac Automation Scripting.

tell application "Adobe Photoshop CC 2017" display notification "message received" with title (the name as text) subtitle (my name as text) end tell

A Quiet Dialogue

Many of the features of Photoshop and Illustrator that you already know how to use with a pen have text equivalents you can use by typing.

For example, you can choose to make a new Photoshop document by tapping a menu command with a pen.

How to make a new Photoshop document: 1) activate Photoshop 2) tap the File menu 3) tap New

Or you can choose to make a new document by typing a text command to Photoshop in Script Editor.

tell application "Adobe Photoshop CC 2017" activate make new document end tell

The same is true of opening, saving, exporting, resizing, and many other features of Photoshop, Illustrator, and other Mac apps.

Powerful Wordplay

The text interface reveals its true power when we string together a list of text commands into a script that we can run in one step. Each line of the script executes an additional text command that builds on what was accomplished in the previous lines.

New Layered Document

When we tell Photoshop to make a new document it always makes us a flat image. To make a layered document instead, we can write a script where we make a new document on one line and then on the following line we make a new layer. The result of the script is a new layered document.

tell application "Adobe Photoshop CC 2017" activate make new document make new art layer in the current document with properties {visible:true} end tell

Custom Menu Commands

A script can contain 10, 20, 100, or more text commands. Running a script from a menu command within Photoshop or Illustrator enables you to unlock all of that power with one tap as you work interactively and creatively.

We can create custom menu commands for Mac applications like Photoshop and Illustrator using a built-in Mac system component: Script Menu. When Photoshop is the frontmost application, Script Menu shows AppleScripts that were specifically written for Photoshop.

If we run the above New Layered Document script from Script Menu, we have essentially created a new Photoshop feature.

Running scripts from a menu command within Photoshop or Illustrator enables us to maintain focus in that app and on our creative work even as we quickly and easily activate powerful scripts that do significant amounts of grunt work for us.

Robot English

If you look at The AppleScript Language as a dialect of English called “Robot English” then AppleScript will not disappoint you. AppleScript is constructed so that it can at least be read by people who have little to no experience with AppleScript or any kind of computer code, but it also has to be understood completely by robots.

In English we can send a message to a friend with casual imprecision.

To: a friend If in the United States say “Howdy.” If in Canada say “Bonjour.” If in Australia say “G’day.” If in Britain say “How do?” If elsewhere say “Hello.”

Say Hello

In AppleScript we turn up the precision of our language to “Military” and then turn it up again to “Robot” in order to send a message to a Mac application named Photoshop. But the message is essentially the same.

tell application "Adobe Photoshop CC 2017" set theSystemInfo to the system info set theLocalLanguage to the user locale of theSystemInfo if theLocalLanguage is equal to "en_US" then say "Howdy" else if theLocalLanguage is equal to "en_CA" then say "Bonjour" else if theLocalLanguage is equal to "en_AU" then say "G’day" else if theLocalLanguage is equal to "en_GB" then say "How do?" else say "Hello" end if end tell

Cast of Characters

AppleScripts are scripts in the sense that they are computer code, and they are also scripts in the sense of a dramatic play, where the story is told as a dialogue between characters. Applications can tell each other to perform various tasks based on their unique talents.

Throughout The Art of AppleScript, Photoshop and Illustrator will be the stars of the show. But we’ll write demanding scripts for them that require a broad set of features that Photoshop and Illustrator don’t have. Therefore we need to fill out the cast with supporting actors so that complete workflow dramas can be performed.

a complete cast of application characters
Application Functionality License
Photoshop pixel image tools Creative Cloud
Illustrator vector graphics tools Creative Cloud
BBEdit text file tools free
Safari Web browser tools built-in
Unix Web server tools built-in
iTunes audio video tools built-in
Finder file system tools built-in
Script Editor AppleScript tools built-in

The scripts we write will generate the same work output that we would make ourselves if working interactively with a Mac because the scripts are using the same applications that we use.

Reasoning Together

The talents of each particular application can be combined into one script.

to come

-- to come

Talking About the File System

There is an incredible amount of grunt work involved with specifying the names and file system locations of files that we create as we work. Issuing file system -based commands like Save and Export as text enables us to include target file system locations and how we want things named and prevent us having to see slow, error-prone, time-wasting file system choosers while we work.

Your Home Folder

When you browse the file system interactively with Finder, your home folder is typically the top of the hierarchy. When you talk about the file system in AppleScript, your home folder will also typically be the top of the hierarchy. The difference is just that you are using text commands instead of touch commands.

Because the name of your home folder is unique, we speak about it only in general terms as “the home folder” and the Mac connects the dots. You can refer to other folders like your Creative Cloud Files folder as subfolders of your home folder.

tell application "Finder" activate set theHomeFolder to the path to the home folder as alias open theHomeFolder try open folder "Creative Cloud Files" of theHomeFolder end try end tell

Your Scripts Folder

In addition to a home folder, you also have a hidden scripts folder where you can keep your AppleScripts. Photoshop and Illustrator have their own script folder locations within your script folder.

Because the name and location of your scripts folder can change, we speak about it only in general terms as “the scripts folder.”

tell application "Finder" activate set theScriptsFolder to the path to the scripts folder as alias open theScriptsFolder try open folder "Photoshop CC" of folder "Applications" of theScriptsFolder open folder "Adobe Illustrator CC 2017" of folder "Applications" of theScriptsFolder end try end tell

Optimizing for Speed and Simplicity with Subroutines

A key feature of AppleScript Mac Automation Scripting is that scripts can be quickly developed in minutes, as they’re required. Another key feature is that scripts are meant to be read, written, and used by people with little or no computer programming experience, so simplicity is very beneficial. To gain speed and simplicity, we can use AppleScript subroutines, which are reusable code blocks that we can Copy/Paste into any script in order to add functionality to that script.

A Routine Subroutine Example

The application scripting folders for Photoshop, Illustrator, BBEdit, and the other applications we will be scripting throughout The Art of AppleScript are not there by default. Rather than ask you to create them manually, we can solve this problem with a short script and a subroutine that will also serve as an example of subroutines.

Prepare Application Scripting Folders

Finder knows folders, so we can tell Finder to prepare an application scripting folder for each application and notify us when it’s done. We’ll write as short a script as possible, and include only the most basic information necessary, which is the names of the scripting folders we want Finder to create.

tell application "Finder" activate prepareApplicationScriptingFolderFor("Photoshop CC") of me prepareApplicationScriptingFolderFor("Adobe Illustrator CC 2017") of me prepareApplicationScriptingFolderFor("BBEdit") of me prepareApplicationScriptingFolderFor("Safari") of me prepareApplicationScriptingFolderFor("iTunes") of me prepareApplicationScriptingFolderFor("Finder") of me prepareApplicationScriptingFolderFor("Script Editor") of me set theCompletionMessage to "application scripting folders prepared" display notification theCompletionMessage with title (the name as text) subtitle (my name as text) end tell (* include prepareApplicationScriptingFolderFor subroutine here *)

Because Finder does not inherently know how to “prepare an application scripting folder” for an application, we also have to provide some explicit details that describe exactly how we’re expecting each folder to be prepared. We can offload that routine task to a subroutine that Finder can reuse every time it prepares an application scripting folder.

Not only do we write much less code when we write a subroutine, we also enjoy the luxury of asking for that subroutine’s functionality with a single line of code because we have defined within the subroutine what it means to “prepare an application scripting folder.” We can support each additional application that we might choose to script in the future by adding one line of code, enabling this script to be a living document that adapts easily to changing conditions.

To use an AppleScript subroutine, simply Paste it at the bottom of any AppleScript.

on prepareApplicationScriptingFolderFor(theApplicationScriptingFolderName) tell application "Finder" set theScriptsFolderPath to the path to the scripts folder as text set theScriptsFolder to theScriptsFolderPath as alias set theApplicationScriptingContainerFolderName to "Applications" set theApplicationScriptingContainerFolderPath to theScriptsFolderPath & theApplicationScriptingContainerFolderName & ":" if not (exists folder theApplicationScriptingContainerFolderPath) then set theApplicationScriptingContainerFolder to make new folder at theScriptsFolder with properties {name:theApplicationScriptingContainerFolderName} else set theApplicationScriptingContainerFolder to theApplicationScriptingContainerFolderPath as alias end if set theApplicationScriptingFolderPath to theScriptsFolderPath & theApplicationScriptingContainerFolderName & ":" & theApplicationScriptingFolderName & ":" if not (exists folder theApplicationScriptingFolderPath) then set theApplicationScriptingFolder to make new folder at theApplicationScriptingContainerFolder with properties {name:theApplicationScriptingFolderName} else set theApplicationScriptingFolder to theApplicationScriptingFolderPath as alias end if open theApplicationScriptingFolder end tell return end prepareApplicationScriptingFolderFor

Example Scripts

AppleScripts made from the example code on this page.

Code Reuse

The blocks of example code on this page and the attached example scripts are open source software that everyone can use and modify and customize for their own purposes under MIT License.

(* Copyright 2005 Simon White http://simonwhite.com/ MIT License Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. *)

Author

Simon White

Last updated .


Keywords: artist, animator, designer-developer, agile, agile-developer, agile-development, full-stack, developer, coder, graphic-artist, web-animator, illustrator, adobe-illustrator, photoshop, adobe-photoshop, vector-graphics, raster-graphics, pixel-graphics, bitmap-graphics, wacom, mac, macintosh, apple-mac, unix, animation, artwork, code, digital-pen, digital-pencil, digital-ink, hand-drawn, hand-coded, handmade, interactive, html, svg, animated-svg, html5, css3, es5, unobtrusive-javascript, dom-scripting, responsive-animations, responsive-layouts, w3c, iso, audio, classlist, cssanimations, csstransforms, csstransitions, eventlistener, inlinesvg, json, opacity, preserve3d, queryselector, requestanimationframe, rgba, webaudio, xhrresponsetypejson.