Over at the Applescript Blog there’s a nice little post on how to quit all running applications. I thought I’d put up the python equivalent.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
#!/usr/bin/python import sys from appscript import * def quitall(): whitelist = ["Finder"] # get list of all apps app(u'System Events').processes.visible.set(True) apps = app(u'System Events').processes[its.visible == True].name.get() for a in apps: if a in whitelist: continue app(a).quit() if __name__ == '__main__': quitall() # change to 0 for success, 1 for (partial) failure sys.exit(0)
It’s fairly simple code. You first set all the applications to visible and then get the name of all the visible apps. This provides the list of all the running applications without including processes you wouldn’t want included. You have a whitelist of application names to exclude (typically just the Finder). You then just iterate through all the names sending the Quit Applescript command.