I was wondering if it is possible to use Cocoa (Apple's API) with Python, so being able to run any code like in this link, so NSWorkspace functions and so on, this might be a super stupid question, but I was still wondering if it was possible...
2 Answers
Yes.
There are Python packages, kind of wrappers around Objective-C, install them like:
$ pip install pyobjc-core
$ pip pyobjc-framework-Cocoa
$ pip pyobjc-framework-Quartz
15 Comments
undefinedChar
If you wouldn’t mind, could you show how to execute a function like NSWorkspace.shared.openFile() with pyobjc?
ipaleka
I retrieve the list of running apps with
NSWorkspace.sharedWorkspace().runningApplications() after NSWorkspace is inported with from AppKit import NSWorkspace. Try with similar approach, I'm not near an OS X right now to check.undefinedChar
Can't seem to be able to install AppKit... Or at least python can't find the module even though I did as you did, and installed all the things you told me to install...
ipaleka
Dunno, I've managed to install it and use it, chek this Travis-CI log from line 91-102. That installation went fine and all the tests passed.
Ted Wrigley
@ipaleka - out of curiosity, is PyObjC installed with Xcode? I have it on my system — at least, I can access objc elements through python — but I don't recall ever installing it manually.
|
MacOS by default comes with python 2.7 and pyObjC 2.5, and has done for years. If you want a newer version of either python, such as python 3, or the newest version of pyObjC, then you have to install it yourself.
The latest version of pyObjC is 5.2.
However, on a default installation of MacOS from at least Snow Leopard onward, the following should work:
python
>>> import Appkit
>>> AppKit.NSWorkspace.sharedWorkspace().runningApplications()
Catalina is stated to be the last version to contain python bundled.