views:

162

answers:

3

I want to find which applications are running, particularly I want to know which one has a window that has focus. So at any given time I want the app to know which application the user is actively working with. I can't figure out how to determine which app is selected, I can only see which window they are using in my app using keyedWindow. If they are not in my app it just becomes 'null'

What would be nice is if I could get the application name, and the title of the window (if possible)

+1  A: 

Have a look at GetFrontProcess and CopyProcessName (or GetProcessInformation).

Or have a look at NSWorkspace::activeApplication.

EDIT: the reference documentation says

It is strongly suggested that you use the NSRunningApplication methods currentApplication or active to retrieve this information in post Mac OS X v10.6 targeted applications.

Gregory Pakosz
Thank you, I will investigate this and see if it provides more info than the activeApplication from the sharedWorkspace
Jameson
A: 

Apologies, I found what I needed. If anyone is looking to do this same thing, this snippet should help:

NSDictionary *activeApp = [[NSWorkspace sharedWorkspace] activeApplication]; NSLog(@"Active application is: %@", (NSString *)[activeApp objectForKey:@"NSApplicationName"] );

Jameson
hehe you beat me to it :)
Gregory Pakosz
+1  A: 

To get a list of running applications:

NSArray *appNames = [[NSWorkspace sharedWorkspace] runningApplications];

To get the active application:

NSRunningApplication *currentApp = [NSRunningApplication currentApplication];

Here's a sample project from Apple:

http://developer.apple.com/mac/library/samplecode/AppList/index.html#//apple%5Fref/doc/uid/DTS40008859

These all require 10.6, according to the documentation.

Preston