Python3 OSX 12.4 Monterey

I have a script that works with python and OSX Sierra 10.12.6. This script will not work with Python3 OSX 12.4 Monterey (syntax error). Could someone fix the script to work with Python3 OSX 12.4 Monterey?

Here is the script:

#!/usr/bin/python


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)


from datetime import datetime

from time import sleep


last_active_name = None

while True:

active_app = NSWorkspace.sharedWorkspace().activeApplication()

if active_app['NSApplicationName'] != last_active_name:

last_active_name = active_app['NSApplicationName']

print '%s: %s [%s]' % (

datetime.now().strftime('%Y-%m-%d %H:%M:%S'),

active_app['NSApplicationName'],

active_app['NSApplicationPath']

)

sleep(1)

Mac mini, macOS 12.4

Posted on Jun 26, 2022 3:13 AM

Reply

Similar questions

2 replies

Jun 26, 2022 5:50 AM in response to darwinsafari

Apple removed Python 2.7.18 from macOS 12.3.1 which was the last Python that supported the Python scripting bridge and Objective-C code. So, there is no longer a /usr/bin/python. A Python3 porting service is out of scope for these communities.


The Python3 installed by Xcode and/or the Command Line Tools for Xcode does not support the scripting bridge, and no Python downloaded from Python.org does either.

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Python3 OSX 12.4 Monterey

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.