- [NSRunningApplication activateWithOptions:]不工作 [英] -[NSRunningApplication activateWithOptions:] not working

查看:216
本文介绍了 - [NSRunningApplication activateWithOptions:]不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个程序,将某个应用程序(如果它启动)。这是我的代码:

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?

推荐答案

只需使用NSApplicationActivateIgnoringOtherApps修改器来激活。

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

另外,activateWithOptions:方法具有以下注释:

Additionally activateWithOptions: method has the following note:


如果应用程序已退出,或不是可以激活的
类型的应用程序
,则此方法将返回NO。

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

这篇关于 - [NSRunningApplication activateWithOptions:]不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆