duncanm
duncanm

Reputation: 796

What is the 'correct' way to identify the currently active application in OSX 10.6+?

I'm trying to identify which OSX application is currently active. I understand that in OSX 10.5, this could be done with:

[[NSWorkspace sharedWorkspace] activeApplication]

however, this has been deprecated in 10.6+.

The apple developers documentation states that this should be done through the 'active' property of the NSRunningApplication object. I thought one way to approach this might be to get the list of all running applications through

[[NSWorkspace sharedWorkspace] runningApplications]

and then loop through, checking the 'active' property of each application. However, the following test code doesn't behave as I expected: when compiled and run from Terminal.app, only the "terminal" app is ever marked as active, regardless of whether I select a different application.

#import <Foundation/Foundation.h>
#import <AppKit/NSRunningApplication.h>
#import <AppKit/NSWorkspace.h>

int main(int argc, char *argv[]) {
  while(1){
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

    NSString *currApp;
    NSArray *runningApps;
    runningApps = [[NSWorkspace sharedWorkspace] runningApplications];
    for (id currApp in runningApps) {
      if ([currApp isActive])
          NSLog(@"* %@", [currApp localizedName]);
      else
          NSLog(@"  %@", [currApp localizedName]);
    }
    sleep(1);
    [pool release];
  }

  return 0;
}

What am I doing wrong? Have I misunderstood how the "active" property works?

(Also, please feel free to criticise my Objective C code --- this is my first attempt at objective C, so I know it's likely to be hideously ugly to the trained eye! Please forgive me! :) Any suggestions welcome.)

Upvotes: 24

Views: 12709

Answers (5)

LukeChairWalker
LukeChairWalker

Reputation: 1

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {

    NSNotificationCenter *allApplicationsNotificationCenter;

    allApplicationsNotificationCenter = [[NSWorkspace sharedWorkspace] notificationCenter];

    [allApplicationsNotificationCenter addObserver:self selector:@selector(applicationActivated:) name:NSWorkspaceDidActivateApplicationNotification object:nil];
}

- (void)applicationActivated:(NSNotification *)aNotification {

    NSLog(@"%@",[[[NSWorkspace sharedWorkspace] menuBarOwningApplication] localizedName]);

}

Upvotes: 0

codingFriend1
codingFriend1

Reputation: 6807

As of OS X 10.7 NSWorkspace also has the convenient method:

- (NSRunningApplication *)frontmostApplication;

Also you could now use Grand Central dispatch calls to make repetitive calls with timeout.

Something like this:

- (void) checkFrontmostApp {

    double delayInSeconds = 2.0;
    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
    dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
        NSRunningApplication* runningApp = [[NSWorkspace sharedWorkspace] frontmostApplication];
        //do something
        NSLog(@"frontmost app: %@", runningApp.bundleIdentifier);
        [self checkFrontmostApp]; //'recursive' call
    }); 
}

Upvotes: 13

NSGod
NSGod

Reputation: 22948

Polling every second or so to find out the current application is inefficient, and is the wrong way to go about this. A much better approach it to simply set your process up to receive the NSWorkspaceDidActivateApplicationNotification notification.

@interface MDAppController : NSObject <NSApplicationDelegate> {
    NSRunningApplication    *currentApp;
}
@property (retain) NSRunningApplication *currentApp;
@end

@implementation MDAppController 
@synthesize currentApp;

- (id)init {
    if ((self = [super init])) {
        [[[NSWorkspace sharedWorkspace] notificationCenter] addObserver:self
                      selector:@selector(activeAppDidChange:)
               name:NSWorkspaceDidActivateApplicationNotification object:nil];
    }
    return self;
}
- (void)dealloc {
    [[[NSWorkspace sharedWorkspace] notificationCenter] removeObserver:self];
    [super dealloc];
}
- (void)activeAppDidChange:(NSNotification *)notification {
    self.currentApp = [[notification userInfo] objectForKey:NSWorkspaceApplicationKey];
    NSLog(@"currentApp == %@", currentApp);
}
@end

int main(int argc, const char * argv[]) {
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    [NSApplication sharedApplication];
    MDAppController *appController = [[MDAppController alloc] init];
    [NSApp setDelegate:appController];
    [NSApp run];
    [pool release];
    return 0;
}

Upvotes: 21

Michael Dautermann
Michael Dautermann

Reputation: 89509

The notes for NSWorkspace's activeApplication say:

Special Considerations

It is strongly suggested that you use the NSRunningApplication classes’ currentApplication or activemethods to retrieve this information in applications targeted for Mac OS X v10.6 and later.

You should probably do a 10.6 & newer set of code and a 10.5.X and older set of code there.

B.T.W., the NSWorkspace method was only marked deprecated as of 10.7, but NSRunningApplication came in as of 10.6.

Oh, here's an alternative that is 64-bit compatible if you include the Application Services framework:

int main (int argc, const char * argv[])
{
    // insert code here...
    CFShow(CFSTR("Hello, World!\n"));
    ProcessSerialNumber psn;

    OSErr err = GetFrontProcess(&psn);
    if(err == noErr)
    {
        ProcessInfoRec info;
        StringPtr processName = malloc(64);

        if(processName)
        {
            bzero(processName, 64);
            info.processInfoLength = sizeof(ProcessInfoRec);
            info.processName = processName;
            err = GetProcessInformation( &psn, &info);
            if(err == noErr)
            {
                fprintf(stdout, "front most process name is %s", processName+1 );
            }
            free(processName);
        }
    }
    return 0;
}

Upvotes: 1

Lily Ballard
Lily Ballard

Reputation: 185661

Your problem is that your application is unable to receive any events from the system informing it that the current application has changed, and therefore it never updates the active property on the NSRunningApplication instances. If I use the exact same code, but another app is active when I start running the code, it reports that application instead.

If instead you change your code to run the main thread's NSRunLoop and use a 1-second timer, it should work.

Here's a quick example:

#import <Foundation/Foundation.h>
#import <AppKit/AppKit.h>

@interface Foo : NSObject
- (void)run;
@end

@implementation Foo
- (void)run {
    for (NSRunningApplication *currApp in [[NSWorkspace sharedWorkspace] runningApplications]) {
        if ([currApp isActive]) {
            NSLog(@"* %@", [currApp localizedName]);
        } else {
            NSLog(@"  %@", [currApp localizedName]);
        }
    }
    NSLog(@"---");
}
@end

int main(int argc, char *argv[]) {
    NSAutoreleasePool *p = [NSAutoreleasePool new];

    Foo *foo = [[Foo new] autorelease];
    NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:1.0f
                                                      target:foo
                                                    selector:@selector(run)
                                                    userInfo:nil
                                                     repeats:YES];
    [[NSRunLoop mainRunLoop] run];

    [p release];
}

Upvotes: 19

Related Questions