Skip to content

Instantly share code, notes, and snippets.

@zapkub
Last active April 2, 2020 08:00
Show Gist options
  • Save zapkub/cde255f7cafc087d64749a00d1d9a3c5 to your computer and use it in GitHub Desktop.
Save zapkub/cde255f7cafc087d64749a00d1d9a3c5 to your computer and use it in GitHub Desktop.
Bring back your frontmost application in macOS after NoMAD try to steal it🤖 ( make sure you do chmod +x before execute )
#!/usr/bin/python
try:
from AppKit import NSWorkspace, NSApplicationActivateIgnoringOtherApps
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
def getActiveApplicationName():
return NSWorkspace.sharedWorkspace().activeApplication()['NSApplicationName']
last_active_app_name = None
runningApplications = NSWorkspace.sharedWorkspace().runningApplications()
while True:
try:
active_app = None
for app in runningApplications:
if getActiveApplicationName() == app.localizedName():
active_app = app
if active_app.localizedName() == 'NoMAD' or active_app.localizedName() == 'SystemUIServer':
# bring last_active_app if NoMAD Active
print "NoMAD is triggered!, stealing back my windows..... %s" % last_active_app_name
last_active_app = None
for app in runningApplications:
if app.localizedName() == last_active_app_name:
last_active_app = app
if last_active_app == None:
print "Cannot find %s make sure you are running this application before this script is started" % last_active_app_name
else:
last_active_app.activateWithOptions_(NSApplicationActivateIgnoringOtherApps)
else:
last_active_app_name = active_app.localizedName()
except:
continue
sleep(0.5)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment