Photoshop Scripting

To script Photoshop (or any Mac OS X application that uses apple scripting) using Python, you need appscript. Appscript is a wrapper around Applescript and provides an (almost) intuitive OO-approach to scripting. You can compile it yourself, or use my precompiled package. To install the precompiled package, unpack it in /Library/Python/2.3 . There should now be a couple of folders and files: aem, appscript, HTMLTemplate.py and HTMLTemplate.pyc, LaunchServices, osaterminology and osax.


Using it

Very important: appscript won't work with the regular command-line python, because it needs access to Mac OS X's window manager. Therefore, you should use pythonw instead of just python.

Once you've installed everything, try the following. Start an interactive python shell by typing pythonw on the command line. Then:

Import appscript

from appscript import app, k

(This opens Python as an application in the Dock)

Get a reference to the photoshop application

a = app('Adobe Photoshop CS.app')

Activate the application

a.activate()

Make a new document

doc = a.make(new=k.document, with_properties={k.width: 400, k.height: 100})

Open a document

from Carbon.File import FSSpec
a.open(FSSpec('~/Desktop/test.gif'))
doc = a.documents

FSSpec returns a reference to the file systems' file object. It's a pity you can't directly access the file object. The reference to the document is now "doc".

Make a new layer

newLayer = doc.make(new=k.art_layer)

Changing properties afterwards doesn't seem to work. So, I am not able to change the fill opacity once I've created the layer. Pretty annoying.

Rotate the canvas

a.rotate_canvas(newDocument, angle=5)

Change the mode

a.change_mode(doc, to=k.RGB)

Printing

a.print_(doc)

Documentation

The full documentation can be found in the Photoshop Scripting Reference. You can compile the documentation yourself for Photshop, or any application that is apple-scriptable using the htmldoc module. Do the following:

from appscript.htmldoc import doc
doc('Adobe Photoshop CS.app', '~/Desktop/PhotoshopDoc.html')

If you compile the module yourself, you might get the following error (in version 0.7.0):

  File "/System/Library/Frameworks/Python.framework/Versions/2.3/lib/python2.3/
  site-packages/appscript/htmldoc/parser.py", line 145, in lambda
  return lambda code: typebycode.get(code) or 'AEType(%r)' % esc(code)
NameError: global name 'esc' is not defined

I don't know what this is, but I've just written an esc function that returns the given value, and it seems to work. If you're using the precompiled package, the fix is already made.


Still to find out

  • Using colors. I can't make a color using a.make(new=k.RGB_color), because that gives "in make new, duplicate, etc. class can't be an element of container".
  • Using a.filter(). See the FAQ.
  • Using a.fill(). This is what I have: a.fill(d.selection, withcontents=a.foregroundcolor) -- it returns "bad parameter data or unable to coerce the data supplied"

Frequently Asked Questions

Q: What is that strange 'k' parameter that I need to import?
A: 'k' is defined in appscript.keywordwrapper and is a generic wrapper for application-specific type and enum names. This means that using k, you can access any class that the program defines. For Photoshop CS, this means the application, document, art_layer, channel, font, layer and other classes. Internally, it uses the getattr attribute to cover any possible parameter.

Q: Why can't I use a.filter() ?
A: Apparently, appscript has this name reserved for its own use. This severly limits the usefullness of Python scripting. I'll try to find a way around this.


Troubleshooting

Q: I get "RuntimeError: Can't send Apple events: no access to Window Manager."
A: Python needs access to the window-manager. The regular version of Python can't provide this access, so you should use pythonw. Just type pythonw instead of python in front of your scripts.

Q: I get "Too many direct parameters".
A: A function call can have zero or one parameters, but never more. Mostly, the first parameter is a reference to the document, selection, ... . The rest of the parameters are named, and you should look them up in the Photoshop Scripting Reference.

Footer image