MacOS 12.3 - Python - Missing NSWorkspace from AppKit

In the past I used this script as groundwork for a little helper which swaps the Modifier Keys (CMD, OPT, CTRL) for me, when a specific application getting focused and unfocused.

With MacOS 12.3 Apple finally removed Python2

Not a big deal, if you reinstall it via Homebrew and update all your Python scripts. But what is about Apple related modules like NSWorkspace from AppKit?

Code:

try:
    from AppKit import NSWorkspace
except ImportError:
    print "Can't import AppKit -- maybe you're running python from brew?"
    print "Try running with Apple's /usr/bin/python instead."
    exit(1)

This code returns reasonably the print-message.

Question

Is this not available anymore? Because the homebrew version can't find this and Python3 seems also not to know it.

Is NSWorkspace for Python now deprecated?

Replies

The AppKit module is part of pyobjc.

Create a virtual environment and install pyobjc:

/usr/bin/python3 -m venv ~/myproject
cd ~/myproject
source bin/activate
pip install pyobjc

Did you ever get this to work @novek? For the life of me I cannot. I don't know how, nor want to create a virtual env...I just want to run a simple py script for a bit to see what the heck is taking focus away from my active window. Any help is really appreciated. Thanks.