Getting the list of running applications ordered by last use
Asked Answered
I

4

16

I'd like to get the list of running applications in the same order they appear when doing ⌘ + ⇥

I.e. if I use TextEdit, then Preview, then iCal, the order is

  1. iCal
  2. Preview
  3. TextEdit

Using [[NSWorkspace sharedWorkspace] launchedApplications] does not work as applications are sorted by launch date/process id. Enumerating with GetNextProcess does not work either as it is also ordered by pid.

Registering for notifications and maintaining a list myself is not an option as I must know the list right after the application launches. Well, the first element of the list would be enough actually, but I think it is pretty much the same question.

Is there some API available to get this information?

Informed answered 3/6, 2009 at 14:18 Comment(0)
A
19

Maybe something like this:

cd /System/Library/Frameworks/CoreServices.framework/Frameworks/LaunchServices.framework
nm LaunchServices | grep __LSCopyApplicationArrayInFrontToBackOrder
Advanced answered 4/8, 2009 at 11:10 Comment(3)
Great, it works exactly as expected. It's a pity that it does not come as a documented LaunchServices API.Informed
And here is how to use _LSCopyApplicationArrayInFrontToBackOrder safely: gist.github.com/163918Informed
Hey @Informed I am kinda noob how to execute that file. So far I've added that in my Xcode but how to call that.Recline
M
3

You need to register for notifications when the active application changes, as outlined here: http://www.unsanity.org/archives/000045.php

Once that is done it is easy to maintain an array of active applications sorted by last active time.

Monovalent answered 3/6, 2009 at 17:32 Comment(1)
Link not available anymore - would be nice if code on here... :-*(Albuminoid
B
3

It is impossible to get the list before your application launches. After you start the application though, you can just register for notifications and maintain your own array of applications.

The only solution is to start a background process at login using launchd that simply listens for applications.

Bombsight answered 3/6, 2009 at 19:56 Comment(0)
M
1

Try enumerating the list of windows with the Accessibility or CGWindowList APIs. Apps aren't windows on Mac OS X, as I'm sure you know, but the front-to-back order of applications should be determined by the front-to-back order of their frontmost windows.

You will, of course, need to ignore processes you've already seen windows from (that is, only consider their frontmost windows).

Malapropos answered 4/8, 2009 at 16:43 Comment(1)
Unfortunately, this solution will not work because as you said, apps aren't window and an app without any window can't be found with this method. Also, the front to back order may be right if you only have one space, but does not work with multiple spaces.Informed

© 2022 - 2024 — McMap. All rights reserved.