toxicsoftware.com

RANDOMIZE USR 0

toxicsoftware.com header image 2

Run Python Script

December 14th, 2007 · 13 Comments · Default

I’ve written an Automator action that allows you to write python scripts directly inside your Automator workflow.

RunPythonScriptIcon

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

RunPythonScriptMain

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.

RunPythonScriptScreenshot

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.

Tags: ·····

13 responses so far ↓

  • 1 Run Python Script: Amazon S3 Uploader // Dec 14, 2007 at 11:32 pm

    [...] Login ← Run Python Script [...]

  • 2 Run Python // Dec 15, 2007 at 5:39 am

    [...] Here’s something that we’ll cover in a future show. It’s a Automator action that allows you to run a Python script just like how you can run AppleScript.http://toxicsoftware.com/run-python-script/   [...]

  • 3 has // Dec 15, 2007 at 7:10 am

    Ooh. This _is_ interesting.

    “Some work does need to be done to make working with any data (which is represented by NSAppleEventDescriptor objects) more convenient.”

    Email me if you’d like to discuss using appscript for this. I’ll be happy to help out with advice and/or code.

    HTH

    has

    http://appscript.sourceforge.net
    http://rb-appscript.rubyforge.org

  • 4 schwa // Dec 15, 2007 at 8:04 am

    Yeah I was thinking appscript would make the perfect tool for accessing the inputs and generating outputs. I’m going to be on the road for a couple of weeks though so I probably wont be able to integrate appscript any time soon.

  • 5 PyObjC: Run in Python automator action. — jessenoller.com // Dec 15, 2007 at 9:36 am

    [...] This is an excellent post from yesterday - the author outlines how, using 10.5 PyObjC bindings and the Leopard Automator, you can add a “run python script” automator action. [...]

  • 6 Automator World : Archive » Run Python Script // Dec 15, 2007 at 10:57 am

    [...] Function: Allows you to embed arbitrary Python code inside an Action to process anything. Author: Jonathan Wight [...]

  • 7 links for 2007-12-16 « Donghai Ma // Dec 15, 2007 at 11:17 pm

    [...] Run Python Script (tags: python automator script programming) [...]

  • 8 Digginestdogg // Dec 15, 2007 at 11:47 pm

    Glad HAS weighed in on Appscript–his work on that coupled with PyObjC has made Python my “go to” language on OS X if C/C++/Obj-C or Java does not _have_ to be used for some reason.

    HAS is an unsung hero in OS X-land. I hope the official recognition comes one day (if that is important to HAS). The community knows the immense value all too well already.

  • 9 Michael Tsai - Blog - Automator - Run Python Script // Dec 16, 2007 at 11:57 am

    [...] Jonathan Wight has written an Automator action that lets you type Python code directly into an Automator workflow. It even provides syntax highlighting and Edit With BBEdit. PyObjC is used to help implement the action, and it’s also available to the code run by the action, so this should make it much easier to do interesting things from Automator. [...]

  • 10 Brian Kerr | links for 2007-12-17 // Dec 17, 2007 at 12:25 am

    [...] Run Python Script (Automator action) “The action will optionally convert the input into Python types (currently limited to converting typeAlias AppleEvent descriptors into path strings) to allow simple processing.” (tags: mac automator python) [...]

  • 11 ≈ Relations › links for 2007-12-21 // Dec 20, 2007 at 7:24 pm

    [...] Run Python Script I’ve written an Automator action that allows you to write python scripts directly inside your Automator workflow. (tags: software opensource macosx python automator action runpythonscript) [...]

  • 12 Jason Foreman // Jan 12, 2008 at 11:57 pm

    This is a fantastic idea. I’ve followed your lead by creating a Run Ruby Script action.

    This action uses the new Scripting Bridge framework in Leopard to turn the NSAppleEventDescriptors into more user-friendly objects. Feel free to poach the idea for use in your own action.

  • 13 Prune’s Blog » Blog Archive » Faire du python dans automator (Leopard) // Mar 31, 2008 at 2:51 pm

    [...] Voila le site d’une personne qui a une solution. Cela interessera particulierement mon ami Akhen, qui fete son anniversaire demain. Tiens, c’est pour toi : http://toxicsoftware.com/run-python-script/ [...]