Simplest solution to use Python to drive an Applescript script
<p>Since Apple has already integrated a full UI automation tool like Applescript (ancient, yes...ugly, yes; but can't find anything better), I was wondering if I can run a Applescript script, inside Python unit test class.</p>

<p>I did find an abandoned project that was integrating AS and Python, but I would like to use something that is stable and reliable; and most of all, easy to implement.</p>

<p>I did look at pyObjC and it is quite a pain to deal with (I know basically nothing about Objective-C), so the last resort that I did try, is to use AS, but I have no way to get results about actions, unless I use something like Python. Unless there is an easier way, that I do not know</p>

<p><a href="https://pypi.python.org/pypi/py-applescript" rel="nofollow">py-applescript</a> provides an easy-to-use wrapper around PyObjC and <code>NSAppleScript</code> for calling AS handlers and converting Python arguments and results to and from their AS equivalents automatically. (Strictly speaking I no longer support py-appscript either - as with py-appscript I'm no longer willing to throw good time after bad - but the py-applescript code is simple enough that any Python user can figure out how to fix or improve it for herself if necessary.) </p>

<p>Alternatively, you can avoid the extra module dependency by using both PyObjC and AppleScriptObjC to <a href="http://appscript.sourceforge.net/asoc.html" rel="nofollow">bridge all the way from Python to AppleScript</a>, though it does require some basic understanding of both ObjC bridges to do it.</p>

<p>The only other option for doing Apple event automation in Python is to use OS X's crappy Scripting Bridge framework via PyObjC, but since SB is riddled with a ton of incomprehensible bugs, defects, and omissions of its own, that's about the last thing you want to inject into your test scripts.</p>

<p>...</p>

<p>OTOH, if you're <em>only</em> doing graphical UI testing, then calling OS X's low-level Accessibility APIs via System Events.app via AppleScript via Python is arguably overkill anyway, and you may want to look around for existing Python GUI automation libraries that just wrap OS X's Accessibility APIs directly. e.g. <a href="https://gist.github.com/diyan/f3c24653e63c24c99137" rel="nofollow">Here's a useful-looking list</a>, although I can't vouch for its accuracy or completeness, or the quality of the libraries it links to.</p>

python applescript