Xcode – How to detect when the current active application changes in OS X?

Try to write an application for OS X that triggers behavior based on the current application. It does not need to interact with it. It only needs to know when to change and what.

Can anyone recommend which APIs can be used for this? My guess will be ancillary services, because most applications such as BetterTouchTool and similar need to interact with the current application. However, those applications directly manipulate those windows, and I am just looking for read-only access, so I am not sure if I need it .

Any similar guidance or reference links would be greatly appreciated.

(PS is sorry for the tags. I have not tried “active application”, “current application”, ” “Focus on the application” attracts white space.)

You can use NSWorkspace to turn the application into an activity Status (NSWorkspaceDidActivateApplicationNotification) or re-activation (NSWorkspaceDidDeactivateApplicationNotification) to be notified. For details, please refer to the documentation on NSWorkspace.

In your controller class, you would do something like this:

- (id)init {
if ((self = [super init])) {
[[[NSWorkspace sharedWorkspace] notificationCenter] addObserver:self
selector:@selector(appDidActivate:)
name:NSWorkspaceDidActivateApplicationNotification
object:nil] ;
}
return self;
}

- (void)dealloc {
[[[NSWorkspace sharedWorkspace] notificationCenter] removeObserver:self];< br /> [ super dealloc];
}

- (void)appDidActivate:(NSNotification *)notification {
NSDictionary *userInfo = [notification userInfo];
NSLog(@" userInfo == %@", userInfo);

}

The key point is basically that you need to register to receive the notification shown in -init. You will repeat the code for you Add another observer for each additional notification name you want (e.g. NSWorkspaceDidDeactivateApplicationNotification).

Another important thing to remember is to remove yourself as an observer in -dealloc (or elsewhere) , So that NSWorkspace will not try to notify your controller object after releasing dealloc’d (and no longer valid).

In the specified -appDidActivate: method, use the information about the application to perform what you need Any operation.

Try to write an application for OS X that triggers behavior based on the current application. It does not need to interact with it. It only needs to know when to change , What.

Can anyone recommend which APIs can be used for this? My guess will be ancillary services, because most applications such as BetterTouchTool and similar need to interact with the current application. However, those applications directly manipulate those windows, and I am just looking for read-only access, so I am not sure if I need it .

Any similar guidance or reference links would be greatly appreciated.

(PS is sorry for the tags. I have not tried “active application”, “current application”, ” “Focus on the application” has attracted a blank.)

You can use NSWorkspace to activate the application (NSWorkspaceDidActivateApplicationNotification) or Receive a notification when reactivating (NSWorkspaceDidDeactivateApplicationNotification). For details, please refer to the documentation on NSWorkspace.

In your controller In the class, you would do something like this:

- (id)init {
if ((self = [super init])) {
[[[NSWorkspace sharedWorkspace] notificationCenter] addObserver:self
selector:@selector(appDidActivate:)
name:NSWorkspaceDidActivateApplicationNotification
object:nil];
}
return self ;
}

- (void)dealloc {
[[[NSWorkspace sharedWorkspace] notificationCenter] removeObserver:self];
[super dealloc];
}

- (void)appDidActivate:(N SNotification *)notification {
NSDictionary *userInfo = [notification userInfo];
NSLog(@"userInfo == %@", userInfo);

}

< p>The key point is basically that you need to register to receive the notifications shown in -init. You will repeat the code to add another observer for each additional notification name you want (e.g. NSWorkspaceDidDeactivateApplicationNotification).

In the specified -appDidActivate: method, use the information about the application to perform any operation you need.

WordPress database error: [Table 'yf99682.wp_s6mz6tyggq_comments' doesn't exist]
SELECT SQL_CALC_FOUND_ROWS wp_s6mz6tyggq_comments.comment_ID FROM wp_s6mz6tyggq_comments WHERE ( comment_approved = '1' ) AND comment_post_ID = 3933 ORDER BY wp_s6mz6tyggq_comments.comment_date_gmt ASC, wp_s6mz6tyggq_comments.comment_ID ASC

Leave a Comment

Your email address will not be published.