The Art of AppleScript

Command Photoshop Exports

Custom File Export Menu Commands for Adobe Photoshop

Use Adobe Photoshop and AppleScript Mac Automation Scripting to create custom menu commands for Photoshop that export multiple images to your exact specifications including file system location in one quick and easy step every time.

Command Photoshop

By creating our own custom file export menu command for Photoshop, we can change the export process from interactive form-filling to one where Photoshop essentially reads the details it needs for each export out of our own written file specifications. You can create just one file export menu command that is customized perfectly to the way you work, or you can create dozens of file export menu commands, each customized to a particular project or type of work output.

We can create custom menu commands for Photoshop using AppleScript, Script Editor, and Script Menu, all of which are built into every Mac. Write the AppleScript code for your menu command in Script Editor and Save the command into Photoshop’s script folder.

To run your command within Photoshop, launch Script Menu. When Photoshop is the frontmost application, Script Menu will show the contents of Photoshop’s script folder.

Same Export, Different Variables

Every time you export image files from Photoshop, the process is the same except for 6 variables:

A list of the values for these variables defines a specification for your Photoshop output.

export the current Photoshop document as a file: - with the source name - PNG format - width: source - height: source - for 2 pixel ratios - to the folder ~/Output/

The problem with this output specification is that only humans can understand it. But if we translate the specification into AppleScript, it can be read by humans and Photoshop.

tell application "Adobe Photoshop CC 2017" activate (* set the output folder to ~/Output/ *) set theOutputFolderPath to prepOutputFolder("Output", "") of me (* export with the source name, PNG format, width: source, height: source, for 2 pixel ratios, and to the output folder *) photoshopFileExport("", ".png", "", "", 2, theOutputFolderPath) of me (* when all image files are exported *) tell application "Finder" to open folder theOutputFolderPath display notification "image files exported" with title (the name as text) subtitle (my name as text) end tell (* include photoshopFileExport and prepOutputFolder subroutines here *)

Filling in Details

Although Photoshop can understand an output specification that’s written in AppleScript, it will need a decoder ring of sorts — in the form of subroutines — that fill in a bunch of details it needs to turn the specification into action. The subroutines are reused every time you export files, no matter what the specification.

You may want to write your own file export subroutines, or you may want to use my subroutines under MIT License. Paste the following code block at the end of an AppleScript in Script Editor to make the subroutines available within that AppleScript.

on photoshopFileExport(theFileName, theFileExtension, thePixelWidth, thePixelHeight, theHighestPixelRatio, theOutputFolderPath) tell application "Adobe Photoshop CC 2017" set theSavedRulerUnits to the ruler units of the settings set the ruler units of the settings to pixel units set theSourceDocument to the current document if thePixelWidth is equal to "" then set thePixelWidth to (the width of theSourceDocument) / theHighestPixelRatio end if if thePixelHeight is equal to "" then set thePixelHeight to (the height of theSourceDocument) / theHighestPixelRatio end if set theExportPixelRatio to "" repeat with thePixelRatio from 1 to theHighestPixelRatio set theSavedHistoryState to the current history state of theSourceDocument resize image theSourceDocument width (thePixelWidth * thePixelRatio) height (thePixelHeight * thePixelRatio) if thePixelRatio is greater than 1 then set theExportPixelRatio to "@" & thePixelRatio & "x" if theFileName is equal to "" then set theFullFileName to the name of theSourceDocument set AppleScript's text item delimiters to "." set theFileName to the first text item of theFullFileName set AppleScript's text item delimiters to "" end if set theExportFilename to theOutputFolderPath & theFileName & theExportPixelRatio & theFileExtension if theFileExtension is equal to ".png" then export theSourceDocument in file theExportFilename as save for web with options {class:save for web export options, web format:PNG, interlaced:false, png eight:false, transparency:true} else if theFileExtension is equal to ".jpeg" then export theSourceDocument in file theExportFilename as save for web with options {class:save for web export options, web format:JPEG, quality:80, matte:{class:RGB color, red:0, green:0, blue:0}, interlaced:false} end if set the current history state of theSourceDocument to theSavedHistoryState end repeat set the ruler units of the settings to theSavedRulerUnits end tell return end photoshopFileExport on prepOutputFolder(theOutputFolderName, theSubFolderName) tell application "Finder" set theHomeFolderPath to the path to the home folder as text set theHomeFolder to theHomeFolderPath as alias set theOutputFolderPath to theHomeFolderPath & theOutputFolderName & ":" if not (exists folder theOutputFolderPath) then set theOutputFolder to make new folder at theHomeFolder with properties {name:theOutputFolderName} else set theOutputFolder to theOutputFolderPath as alias end if if theSubFolderName is equal to "" then set theTargetFolderPath to theOutputFolderPath else set theSubFolderPath to theHomeFolderPath & theOutputFolderName & ":" & theSubFolderName & ":" set theTargetFolderPath to theSubFolderPath if not (exists folder theSubFolderPath) then set theSubFolder to make new folder at theOutputFolder with properties {name:theSubFolderName} else set theSubFolder to theSubFolderPath as alias end if end if end tell return theTargetFolderPath end prepOutputFolder

Work with Multiple Specifications in One Step

To export files using multiple specifications in one step — for example to generate a set of icons in multiple sizes — simply write each specification on a new line.

tell application "Adobe Photoshop CC 2017" activate (* set the output folder to ~/Output/ *) set theOutputFolderPath to prepOutputFolder("Output", "") of me (* export iPhone icons, PNG format, width: 60, height: 60, for 3 pixel ratios, and to the output folder *) photoshopFileExport("iphone", ".png", 60, 60, 3, theOutputFolderPath) of me (* export iPad icons, PNG format, width: 76, height: 76, for 3 pixel ratios, and to the output folder *) photoshopFileExport("ipad", ".png", 76, 76, 3, theOutputFolderPath) of me (* when all image files are exported *) tell application "Finder" to open folder theOutputFolderPath display notification "image files exported" with title (the name as text) subtitle (my name as text) end tell (* include photoshopFileExport and prepOutputFolder subroutines here *)

Work with Multiple Source Files in One Step

To work with multiple source files in one step, wrap export commands in an open block and Save your AppleScript as a Mac application, which will create an AppleScript droplet. Drop source image files on the droplet for processing in Photoshop.

Within the droplet script we repeat through multiple files, processing them one at a time in Photoshop. We also need to use multiple output folders to avoid output files with the same name simply overwriting each other as each new file is processed. Here we sort output files into subfolders named for the folders that contain the source images.

(* when files are dropped on an application made from this script *) on open theFiles tell application "Adobe Photoshop CC 2017" activate repeat with theFile in theFiles (* get the name of the source folder that contains the source image *) tell application "Finder" to set theSourceFolderName to the name of the container of theFile (* set the output folder to a subfolder named for the source folder within ~/Output/ *) set theOutputFolderPath to prepOutputFolder("Output", theSourceFolderName) of me open theFile (* export iPhone icons, PNG format, width: 60, height: 60, for 3 pixel ratios, and to the output folder *) photoshopFileExport("iphone", ".png", 60, 60, 3, theOutputFolderPath) of me (* export iPad icons, PNG format, width: 76, height: 76, for 3 pixel ratios, and to the output folder *) photoshopFileExport("ipad", ".png", 76, 76, 3, theOutputFolderPath) of me close the current document saving no end repeat (* when all image files are exported *) tell application "Finder" to open folder theOutputFolderPath display notification "image files exported" with title (the name as text) subtitle (my name as text) end tell end open (* include photoshopFileExport and prepOutputFolder subroutines here *)

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.