Using Swift (or objective-C) I want to get the list of apps currently running on the macOS, in order of recent usage. That would be the order in which cmd-tab
shows app icons on the Mac.
The following gives me the apps, but not in the order I want.
let apps = NSWorkspace.shared.runningApplications
In the documentation for the .runningApps property
, it says:
The order of the array is unspecified, but it is stable, meaning that the relative order of particular apps will not change across multiple calls to runningApplications.
Is there a way to sort/get the apps in the desired order?
Edit:
The approach suggested by in the answer by 'brimstone' seems promising, but CGWindowListCopyWindowInfo
returns windows in the front-to-back order only when CGWindowListOption.optionOnScreenOnly
is specified. In that case, however, only windows in the current space are returned.
cmd-tab
however is able to list apps across all spaces.
Does anyone know of any other way? Shouldn't there be a more direct/easier way to do this?