-[NSRunningApplication activateWithOptions:] not working

走远了吗. 提交于 2020-01-03 03:26:08

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/31839290/nsrunningapplication-activatewithoptions-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!