26

I've got a background process that makes a transparent window appear when a hotkey is pressed:

[window makeKeyAndOrderFront:nil];
[[content animator] setAlphaValue:1.0]; // alpha was 0.0

... the window shows up fine & in front of the other windows (as I want it to), however until I manually click the window whatever application was active when the window appears remains active. I was expecting the 'makeKeyAndOrderFront' to make the application active as well, however adding a NSLog line to my -applicationWillBecomeActive shows it's not getting any active notification until the mouse click is performed.

Does anyone know how I can set my application active @ the same time I issue the -makeKeyAndOrderFront ? I need it active so that it can begin accepting keyboard input - any assistance needed :-)

Dave Carpeneto
  • 1,042
  • 2
  • 12
  • 23

3 Answers3

54

Look at [[NSApplication sharedApplication] activateIgnoringOtherApps : YES];.

  • 2
    *THANK YOU !!!* - I was pulling my hair out trying stuff with NSRunningApplication's -activateWithOptions , but everything I tried resulted in a beach ball... – Dave Carpeneto Dec 15 '09 at 01:52
12

For 10.6 or greater

[[NSRunningApplication currentApplication] activateWithOptions:(NSApplicationActivateAllWindows | NSApplicationActivateIgnoringOtherApps)];
Arvin
  • 2,516
  • 27
  • 30
6

Swift 4.2+ of @Arvin answer

NSRunningApplication.current.activate(options: [.activateIgnoringOtherApps, .activateAllWindows])
Ahmadreza
  • 6,950
  • 5
  • 50
  • 69
  • 2
    This helped me to find what I needed in Xamarin MacOS `NSRunningApplication.CurrentApplication.Activate(NSApplicationActivationOptions.ActivateIgnoringOtherWindows);` – Felipe Jan 17 '20 at 06:29