5

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 2

2

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

https://pypi.org/project/pyobjc-framework-Cocoa/

Sign up to request clarification or add additional context in comments.

15 Comments

If you wouldn’t mind, could you show how to execute a function like NSWorkspace.shared.openFile() with pyobjc?
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.
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...
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.
@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.
|
1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.