-[NSRunningApplication activateWithOptions:] not working
Asked Answered
L

1

1

I'm trying to create a program that will focus a certain application (if it is launched). Here is my code:

#import <Cocoa/Cocoa.h>
#import <stdio.h>

int main() {
  // activate Firefox
  NSArray *apps = [NSRunningApplication runningApplicationsWithBundleIdentifier: @"org.mozilla.firefox"];

  if ([apps count] == 0) {
    printf("no matching app\n");
    return 1;
  }

  if (![apps[0] activateWithOptions: NSApplicationActivateAllWindows]) {
    printf("failed to activate\n");
    return 1;
  }

  return 0;
}

When I run this, it prints "failed to activate," and Firefox is not brought into focus. What am I doing wrong?

Lordinwaiting answered 5/8, 2015 at 17:40 Comment(0)
V
6

Just use NSApplicationActivateIgnoringOtherApps modifier to activate. It's work ok.

Additionally activateWithOptions: method has the following note:

This method will return NO if the application has quit, or is not a type of application than can be activated.

Vienne answered 5/8, 2015 at 18:41 Comment(2)
Adding NSApplicationActivateIgnoringOtherApps worked! Thank you!Lordinwaiting
Hi @Lordinwaiting and comrade, I am having an issue with with this, if the all the windows of the process are minimized, it is not un-minimizing the windows. Is there anyway to unminimize the windows then focus? Here is screencast of my issue, I am making a firefox profile manager - youtube.com/watch?v=sR5uf4eR8jsElectrode

© 2022 - 2024 — McMap. All rights reserved.