Scripting Bridge vs PyObjC vs py2app
Asked Answered
M

1

6

I am just starting to learn about integrating Python and Mac OS apps. (I want to call some methods from Cocoa to Python.) I've ran into these terminologies -- Scripting Bridge, PyObjC, and py2app. What's the difference? Is PyObjC an example of a scripting bridge? And when does py2app come into play?

Micropaleontology answered 5/6, 2012 at 18:21 Comment(0)
A
8

The short version: PyObjC is the way you call Mac OS X APIs, Scripting Bridge is the way you talk to other apps' scripting interfaces. In more detail:

PyObjC is a bridge between the Python language and the Objective C runtime (and the set of Cocoa wrappers built trivially on top of that bridge, and some nice convenience stuff). If you want to call Cocoa methods, you use PyObjC, typically by importing either Cocoa or Foundation.

Scripting Bridge is a bridge between the Python language and the Apple Event-based scripting system. If you want to call another app's scripting interface, you use Scripting Bridge. (In most cases, if you're using Scripting Bridge, you'll also want to import Foundation, because Scripting Bridge deals with things like NSArrays, etc.)

So, PyObjC is not an example of a scripting bridge. An example of a scripting bridge is, well, Scripting Bridge, or Appscript (which is better, but not from Apple, and no longer maintained).

py2app has nothing much to do with either of these; it's a way to wrap up a Python application, together with all of the extension modules it requires, and as much of the Python interpreter as necessary, into a single .app bundle that you can distribute to users so they can just double-click to run it. Of course most such apps will have GUIs, and many of them will use PyObjC to create those GUIs directly in Cocoa (rather than using, e.g., PyQt or wxPython), but beyond that, there's no real connection.

Abutilon answered 5/6, 2012 at 19:14 Comment(4)
Also, depending which apps you are trying to integrate with, you may find py-appscript a better choice than Scripting Bridge. Don't be put off by the developer's deprecation disclaimer, it's still the best Apple Event scripting interface out there, particularly to apps with funky scripting interfaces. pypi.python.org/pypi/appscriptFrasquito
As I mentioned above, Appscript is no longer maintained. In fact, has isn't even accepting patches from other people anymore. Eventually, it's going to stop working, unless someone else takes it over. That being said, I'm still using it in multiple apps, both Python and ObjC, and I'll keep patching it myself rather than switch to Scripting Bridge and try to work around the problems with iTunes and Finder… But I always feel worried about recommending it to new users.Abutilon
Sorry. I overlooked your mention of Appscript. I agree with your assessment.Frasquito
Another alternative is to write in a combination of Python (with PyObjC) and AppleScript (with AppleScriptObjC). It's annoying to be forced to use two languages talking through different bridges to a runtime that's foreign to both… but on the other hand, it's still supported by Apple, and doesn't have all the horrible problems of ScriptingBridge.Abutilon

© 2022 - 2024 — McMap. All rights reserved.