我想把我的应用程序作为菜单栏应用程序,我已经做到了.而且我还想跟踪正在运行的应用程序.
NSRunningApplication方法返回所有runnng应用程序.但我想检测现在激活的唯一应用程序.(用鼠标点击或命令+标签...)我怎样才能找到它?
我在下面做了代码:
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
[NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(performTimerBasedUpdate) userInfo:nil repeats:YES];
}
- (void)performTimerBasedUpdate {
nowRunning = [NSRunningApplication currentApplication];
nowRunningName = [nowRunning localizedName];
}
Run Code Online (Sandbox Code Playgroud)
但是,它返回我创建的应用程序名称(self).
我终于找到了答案:谢谢你我.我可以使用过滤器获取激活的应用程序.我不知道isActive属性!
runningApplications_ = [[NSWorkspace sharedWorkspace] runningApplications];
nowRunning = [[runningApplications_ filteredArrayUsingPredicate:isActive] objectAtIndex:0];
bundleIdentifier_ = [nowRunning bundleIdentifier];
localizedName = [nowRunning localizedName];
Run Code Online (Sandbox Code Playgroud)