Important see updates at end of post
I’ve written an Automator action that allows you to write python scripts directly inside your Automator workflow.
“Run Python Script” Automator action (catchy title) is written using Python and PyObjC (now built-in to Mac OS X 10.5). Apple already provides “Run AppleScript” and “Run Shell Script” actions with Automator which give Automator a high degree of flexibility. However Python is my preferred scripting language and by writing a custom action purely for Python I was able to take advantage of some PyObjC features that in my opinion make my action superior to the provided Apple scripting action. I’m releasing all the source code to the action under the BSD Open Source license.
One of the main advantages of the action is that Python (with the help of PyObjC) understands the AppleEvent descriptors that Automator uses to pass data between workflow actions. This allows the user to create a custom action that accepts and provides data of any type. In contrast Apple’s “Run Shell Script” action can only accept and provide text (usually limited to file paths). Some work does need to be done to make working with any data (which is represented by NSAppleEventDescriptor objects) more convenient.
The default Python script for a new action in a workflow follows:
import sys
def main(input, *args, **kwargs):
'''Your script goes here.'''
print 'Hello world'
return sys.stdout
This is pretty simple. The action will optionally convert the input into Python types (currently limited to converting typeAlias AppleEvent descriptors into path strings) to allow simple processing. Output is (optionally) converted from Python types to native Automator types. See the Examples directory for more.
The following screenshot shows the action used in a real workflow. This workflow asks the user for a keyword and then the Python action downloads photos from the morguefile public stock photo archive that are related to the keyword. Once download it performs some processing on the files (scales them to 640 by 480) and produces a PDF “Contact Book” from the images.
The source code uses a 10.5 specific technique to create Python based bundles. It also has a simple Python syntax colouring NSTextView (using Python itself to colourise the source).
I’m calling the Action a 0.1 release and have some plans/ideas for further releases. See the TODO list.
Update: I’ve added a sample script showing how to upload files to Amazon S3 from within a workflow.
The code in subversion is a lot newer than the binary I’m linking to. If possible do a checkout and build the plugin yourself.
Update: This Automator action has issues on Snow Leopard and some Leopard versions. Use at your own risk.
Update: Automator’s “Run Terminal Script” looks like it supports and understands Python native. That means, although my .action was better (syntax highlighting, better control of input and output, better logging, etc), I probably won’t bother to update it to Snow Leopard.



