<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>toxicsoftware.com &#187; public</title>
	<atom:link href="http://toxicsoftware.com/tag/public/feed/" rel="self" type="application/rss+xml" />
	<link>http://toxicsoftware.com</link>
	<description>RANDOMIZE USR 0</description>
	<lastBuildDate>Sun, 01 Aug 2010 17:49:39 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Run Python Script</title>
		<link>http://toxicsoftware.com/run-python-script/</link>
		<comments>http://toxicsoftware.com/run-python-script/#comments</comments>
		<pubDate>Fri, 14 Dec 2007 16:14:16 +0000</pubDate>
		<dc:creator>schwa</dc:creator>
				<category><![CDATA[Default]]></category>
		<category><![CDATA[Automator]]></category>
		<category><![CDATA[Open Source]]></category>
		<category><![CDATA[public]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[Source]]></category>

		<guid isPermaLink="false">http://toxicsoftware.com/run-python-script/</guid>
		<description><![CDATA[Important see updates at end of post I&#8217;ve written an Automator action that allows you to write python scripts directly inside your Automator workflow. &#8220;Run Python Script&#8221; Automator action (catchy title) is written using Python and PyObjC (now built-in to &#8230; <a href="http://toxicsoftware.com/run-python-script/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p><em>Important</em> see updates at end of post</p>

<p>I&#8217;ve written an Automator action that allows you to write python scripts directly inside your Automator workflow.</p>

<p><a href="http://www.flickr.com/photos/61285556@N00/2110974754" title="View 'RunPythonScriptIcon' on Flickr.com"><img src="http://farm3.static.flickr.com/2237/2110974754_6e5540298b_o.png" alt="RunPythonScriptIcon" /></a></p>

<p>&#8220;<a href="http://bitbucket.org/schwa/toxic-public/src/tip/Projects/Automator/RunPythonScriptAction/Output/Run%20Python%20Script.action.tar.bz2">Run Python Script</a>&#8221; Automator action (catchy title) is written using Python and <a href="http://pyobjc.sourceforge.net/">PyObjC</a> (now built-in to Mac OS X 10.5). Apple already provides &#8220;Run AppleScript&#8221; and &#8220;Run Shell Script&#8221; 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&#8217;m releasing all the <a href="http://bitbucket.org/schwa/toxic-public/src/tip/Projects/Automator/RunPythonScriptAction/">source code</a> to the action under the <a href="http://www.opensource.org/licenses/bsd-license.php">BSD Open Source</a> license.</p>

<p>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&#8217;s &#8220;Run Shell Script&#8221; 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.</p>

<p>The default Python script for a new action in a workflow follows:</p>

<pre><code>import sys

def main(input, *args, **kwargs):
    '''Your script goes here.'''
    print 'Hello world'
    return sys.stdout
</code></pre>

<p><a href="http://www.flickr.com/photos/61285556@N00/2110978140" title="View 'RunPythonScriptMain' on Flickr.com"><img src="http://farm3.static.flickr.com/2286/2110978140_4c336656f7_m.jpg" alt="RunPythonScriptMain" /></a></p>

<p>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.</p>

<p>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 <a href="http://morguefile.com/">morguefile</a> 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 &#8220;Contact Book&#8221; from the images.</p>

<p><a href="http://www.flickr.com/photos/61285556@N00/2110196661" title="View 'RunPythonScriptScreenshot' on Flickr.com"><img src="http://farm3.static.flickr.com/2169/2110196661_e9f178f475_m.jpg" alt="RunPythonScriptScreenshot" /></a></p>

<p>The <a href="http://bitbucket.org/schwa/toxic-public/src/tip/Projects/Automator/RunPythonScriptAction/">source code</a> 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).</p>

<p>I&#8217;m calling the Action a 0.1 release and have some plans/ideas for further releases. See the <a href="http://bitbucket.org/schwa/toxic-public/src/tip/Projects/Automator/RunPythonScriptAction/TODO.txt">TODO list</a>.</p>

<p><em>Update</em>: I&#8217;ve added a <a href="http://toxicsoftware.com/s3_uploader/">sample script</a> showing how to upload files to Amazon S3 from within a workflow.</p>

<p>The code in subversion is a lot newer than the binary I&#8217;m linking to. If possible do a  checkout and build the plugin yourself.</p>

<p><em>Update</em>: This Automator action has issues on Snow Leopard and some Leopard versions. Use at your own risk.</p>

<p><em>Update</em>: Automator&#8217;s &#8220;Run Terminal Script&#8221; 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&#8217;t bother to update it to Snow Leopard.</p>

<p><img src="http://toxicsoftware.com/wordpress/wp-content/uploads/Screen-shot-2009-10-16-at-11.02.36-AM.png" alt="Screen shot 2009-10-16 at 11.02.36 AM.png" border="0" width="25%" height="25%" /></p>
]]></content:encoded>
			<wfw:commentRss>http://toxicsoftware.com/run-python-script/feed/</wfw:commentRss>
		<slash:comments>22</slash:comments>
		</item>
	</channel>
</rss>
