当应用程序打开,退出和更改焦点时,OS X通知

时间:2013-07-01 18:03:41

标签: macos cocoa window

当其他应用程序打开或退出时,或者当不同的窗口(听力应用程序或其他应用程序)被带到最前端时,是否有任何方法可以通知cocoa应用程序?

1 个答案:

答案 0 :(得分:3)

您可以将观察者添加到共享工作区notificationCenter:

[[[NSWorkspace sharedWorkspace] notificationCenter] addObserverForName:NSWorkspaceDidActivateApplicationNotification object:nil queue:[NSOperationQueue currentQueue] usingBlock:^(NSNotification *note) {
    NSRunningApplication *frontmostApplication = [[note userInfo] objectForKey:NSWorkspaceApplicationKey];
}];

// from NSWorkspace.h
APPKIT_EXTERN NSString * NSWorkspaceWillLaunchApplicationNotification;  //  see above
APPKIT_EXTERN NSString * NSWorkspaceDidLaunchApplicationNotification;   //  see above
APPKIT_EXTERN NSString * NSWorkspaceDidTerminateApplicationNotification;    //  see above
APPKIT_EXTERN NSString * const NSWorkspaceDidHideApplicationNotification NS_AVAILABLE_MAC(10_6);
APPKIT_EXTERN NSString * const NSWorkspaceDidUnhideApplicationNotification NS_AVAILABLE_MAC(10_6);
APPKIT_EXTERN NSString * const NSWorkspaceDidActivateApplicationNotification NS_AVAILABLE_MAC(10_6);
APPKIT_EXTERN NSString * const NSWorkspaceDidDeactivateApplicationNotification NS_AVAILABLE_MAC(10_6);