My Mac service disables the foreground application

I wrote a Cocoa application in a Mac application. When I enter another application and start the service, even if the service code is completely empty:

- (void)doMyService:(NSPasteboard*)pasteboard userData:(NSString*)userData error:(NSString**)error { } 

... this application window is deactivated, and mine is forward. This is true even if my application is configured to run in non-dock mode (LSUIElement = false). The foreground application window is turned off even if my application does not have its own window!

I tried to disable my application in the service handler; bad luck. Hiding my application from the service handler seems to work, but only if the user starts the service in the context menu, and not in the key combo, and only if we do not work in the mode without the docking station (I want my application was without a dock).

How to prevent the deactivation of the foreground application, or at least reactivate it?

+4
source share
1 answer

OK this question is 6 months old, so you probably solved your problem by now, but in case it is useful ...

I solved a similar problem with the application I'm working on, watching for application deactivation through NSWorkspace.

You may have a property:

 NSRunningApplication* _previousRunningApplication; 

And update this property by watching NSWorkspaceDidDeactivateApplicationNotification

 [[[NSWorkspace sharedWorkspace] notificationCenter] addObserver:self selector:@selector(anyApplicationDeactivated:) name:NSWorkspaceDidDeactivateApplicationNotification object:nil]; // Called when any running application deactivates -(void)anyApplicationDeactivated:(NSNotification*) notification { NSRunningApplication* app = [notification.userInfo valueForKey:NSWorkspaceApplicationKey]; _previousRunningApplication = app; } 

Then, when your application completes execution, simply activate the active application using:

 // Activate the previously active application (if available) [_previousRunningApplication activateWithOptions:0]; 

If there is a possibility that the last line may be called while your application is inactive, then first you need to check if it uses [NSApp isActive] (or you can cause some amazing and annoying behavior for users ...)

If you find a simpler solution, I would be interested to know about it.

In case this code is useful to everyone, I created a separate class to handle all this and checked it on Github:

http://github.com/mjrit/MJRAppReactivation

+4
source

Source: https://habr.com/ru/post/1385451/


All Articles