Created
March 29, 2017 22:57
-
-
Save pudquick/eebc4d569100c8e3039bf3eae56bee4c to your computer and use it in GitHub Desktop.
Getting the list of visible apps (think: Force Quit) in macOS via python and pyobjc
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from Foundation import NSBundle | |
import objc | |
CoreServices = NSBundle.bundleWithIdentifier_('com.apple.CoreServices') | |
functions = [ | |
('_LSCopyRunningApplicationArray', '@I'), | |
('_LSCopyApplicationInformation', '@I@@'), | |
] | |
constants = [ | |
('_kLSApplicationTypeKey', '@'), | |
('_kLSApplicationForegroundTypeKey', '@'), | |
('_kLSDisplayNameKey', '@') | |
] | |
objc.loadBundleFunctions(CoreServices, globals(), functions) | |
objc.loadBundleVariables(CoreServices, globals(), constants) | |
apps = _LSCopyRunningApplicationArray(0xfffffffe) | |
app_infos = [_LSCopyApplicationInformation(0xffffffff, x, None) for x in apps] | |
visible_app_infos = [x for x in app_infos if x.get(_kLSApplicationTypeKey, None) == _kLSApplicationForegroundTypeKey] | |
visible_apps = sorted([x.get(_kLSDisplayNameKey) for x in visible_app_infos]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I figured out a few things about these APIs that can come in handy.
_LSCopyRunningApplicationArray
I don't have a source for this, but I'm fairly certain the prototype is:
LSSessionID
is a C enum with one known values (as seen in the WebKit source):The
-2
is equivalent to0xfffffffe
. The example above also uses0xffffffff
, which is probably another case,kLSCurrentSessionID = -1
, but I haven't been able to verify that. It's mentioned in manpage oflsappinfo
, but not its value, or how its different.The returned result is an array of
LSASNRef
s, described below._LSCopyApplicationInformation
Here's the prototype from darling:
I think the type is actually more like:
Where:
LSSessionID
is the same as aboveLSASNRef
is defined by WebKit as:A common value is the result of
_LSGetCurrentApplicationASN()
, or as in this example, the elements returned by_LSCopyRunningApplicationArray()
.struct __LSASN"
itself is some opaque structure. I don't know anything about its fields/layout.CFString key
is an optional key that picks which property to retrieve (all other properties would be null). It's probably quicker to fetch one field if you know what you need, but I haven't benchmarked it.If null, all properties are retrieved.
Here's a new-and-improved version, which gives name to the LSSessionID magic numbers, and updates the script for Python 3: https://gist.github.com/amomchilov/096ce5ceb9f4fca942ae0dd37066bc11