Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling AppleScript from Python without using osascript or appscript?

Is there any way to execute (and obtain the results of) AppleScript code from python without using the osascript command-line utility or appscript (which I don't really want to use (I think?) because it's no longer developed/supported/recommended)?

Rationale: in another question I've just posted, I describe a strange/undesired behaviour I'm experiencing with running some AppleScript via osascript. As I'm actually calling it from a python script, I wondered if there was a way to route around osascript altogether, since that seems to be where the problem lies - but appscript (the obvious choice?) looks risky now...

like image 390
gimboland Avatar asked Apr 17 '13 16:04

gimboland


1 Answers

You can use the PyObjC bridge:

>>> from Foundation import *
>>> s = NSAppleScript.alloc().initWithSource_("tell app \"Finder\" to activate")
>>> s.executeAndReturnError_(None)
like image 74
Ken Thomases Avatar answered Sep 24 '22 02:09

Ken Thomases