1
votes

My end goal is to determine the most-recently-used web browser. I.e., a user may have both Safari and Firefox running, but which were they using most recently?

And so, I'm trying to enumerate the open apps, ordered by their window / visibility.

Does Cocoa / Applescript offer a way to retrieve this information?

1

1 Answers

1
votes

You can use NSWorkspace's runningApplications method and NSRunningApplication's launchDate property to get you close to what you need.

Check out what the output if this code bit looks like in your console:

NSArray * arrayOfRunningApps = [[NSWorkspace sharedWorkspace] runningApplications];
if(arrayOfRunningApps)
{
    for(NSRunningApplication * aUserApp in arrayOfRunningApps) 
    {
        NSLog( @"%@ launch time is %@", [aUserApp localizedName], [aUserApp.launchDate description] );
    }
}

As for sorting on some criteria, that's really up to your implementation to do.

A couple caveats: these api's were introduced as of Snow Leopard (10.6) and they only work on apps that the user can see (in the dock), not UNIX processes or admin/root daemons or whatever.

And I'm not certain about Applescript yet, but I'm hoping you'll be able to use this information to get to where you need to go.