Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
566 views
in Technique[技术] by (71.8m points)

objective c - 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.)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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];
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...