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...

undefinedChar
  • 537
  • 1
  • 5
  • 18

2 Answers2

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/

ipaleka
  • 3,745
  • 2
  • 13
  • 33
  • If you wouldn’t mind, could you show how to execute a function like NSWorkspace.shared.openFile() with pyobjc? – undefinedChar Aug 12 '19 at 12:07
  • I retrieve [the list of running apps](https://github.com/ipaleka/arrangeit/blob/master/arrangeit/darwin/collector.py) 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. – ipaleka Aug 12 '19 at 12:44
  • 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... – undefinedChar Aug 12 '19 at 12:50
  • Dunno, I've managed to install it and use it, chek this [Travis-CI log](https://travis-ci.org/ipaleka/arrangeit/jobs/569815918) from line 91-102. That installation went fine and all the tests passed. – ipaleka Aug 12 '19 at 13:00
  • @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. – Ted Wrigley Aug 12 '19 at 14:47
  • Really don't know that, probably, or maybe with custom Python install. – ipaleka Aug 12 '19 at 14:54
  • I still don't understand how to get AppKit to work, I can't figure out how to install the module... – undefinedChar Aug 12 '19 at 15:30
  • Just that it can't find the module itself, so Im guessing Im somehow not installing it correctly... – undefinedChar Aug 12 '19 at 15:33
  • You should install cairo, try with `pip install pycairo` or check [this](https://scoobygalletas.blogspot.com/2011/08/installing-cairo-and-pycairo-in-mac-os.html). – ipaleka Aug 12 '19 at 15:55
  • On the install command and build command for ./waf it gives an error. This is the [output](https://psty.io/r?q=fd15) – undefinedChar Aug 12 '19 at 16:09
  • That's from install from source, what errors you've got with pip install (`pip install pycairo`)? – ipaleka Aug 12 '19 at 16:12
  • Just read the errors. In this case you need `libffi`. After googling, [this](https://stackoverflow.com/a/25854749/11703358) came up. – ipaleka Aug 12 '19 at 16:19
  • You should try with Python3 then. https://arrangeit.readthedocs.io/en/latest/development.html#mac-os-x – ipaleka Aug 12 '19 at 16:46
  • This seems like it is way too complicated, is there no easier method? – undefinedChar Aug 12 '19 at 19:52
  • As you can see from that Travis-Ci log, for Python3 it's simple as `pip pyobjc-framework-Cocoa`. I suppose you have problems because of Python 2. – ipaleka Aug 12 '19 at 20:06
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.

benwiggy
  • 1,440
  • 17
  • 35