macos 如何使用 Cocoa api 启动应用程序并将其置于前台?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2333078/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 06:47:23  来源:igfitidea点击:

How to launch application and bring it to front using Cocoa api?

cocoamacos

提问by dimsuz

I'm very new to a cocoa programming and I can't find the way to do the following:

我对可可编程很陌生,我找不到执行以下操作的方法:

  • Start a particular application by name
  • Do some work
  • Later bring that application I've started to be the front process
  • 按名称启动特定应用程序
  • 做一些工作
  • 后来把那个应用程序我开始成为前端进程

From what I've found in Carbon API it looks like the calls i should use are launchApplication() and setFrontProcess().

从我在 Carbon API 中发现的内容来看,我应该使用的调用是 launchApplication() 和 setFrontProcess()。

But how to do this in Cocoa? I.e. launch it, get PID, set that PID to be a front process. I tried to google for examples and find nothing...

但是如何在 Cocoa 中做到这一点?即启动它,获取 PID,将该 PID 设置为前端进程。我试图用谷歌搜索示例,但一无所获...

If any of you can provide a minimalistic sample that would be awesome :)

如果你们中的任何一个人可以提供一个非常棒的简约样本:)

Thanks in advance.

提前致谢。

回答by Nyx0uf

To launch an application :

启动应用程序:

[[NSWorkspace sharedWorkspace] launchApplication:@"/Applications/Safari.app"];

To activate an app :

激活应用程序:

NSRunningApplication* app = [NSRunningApplication
                             runningApplicationWithProcessIdentifier: PID];
[app activateWithOptions: NSApplicationActivateAllWindows];
// or
NSArray* apps = [NSRunningApplication
                 runningApplicationsWithBundleIdentifier:@"com.bla.blah"];
[(NSRunningApplication*)[apps objectAtIndex:0]
 activateWithOptions: NSApplicationActivateAllWindows];

回答by ashcatch

To start an application, use the NSWorkspaceclass: NSWorkspace Reference

要启动应用程序,请使用NSWorkspace类:NSWorkspace 参考

Specifically, the launchApplication:function.

具体来说,launchApplication:功能。

I don't know the answer of the activation part off my head. You can activate your own application with -[NSApplication activateIgnoringOtherApps:], but I don't know how to do it for other apps.

我不知道激活部分的答案。您可以使用 激活您自己的应用程序-[NSApplication activateIgnoringOtherApps:],但我不知道如何为其他应用程序执行此操作。

回答by catlan

Did you look into NSRunningApplication?

你有没有研究NSRunningApplication

回答by Kevin Grant

NSRunningApplication is available on Mac OS X 10.6 or later.

NSRunningApplication 在 Mac OS X 10.6 或更高版本上可用。

If you have to support earlier systems, this can be done with APIs such as GetCurrentProcess() and SetFrontProcess() and the old ProcessSerialNumber structure.

如果您必须支持较早的系统,则可以使用诸如 GetCurrentProcess() 和 SetFrontProcess() 之类的 API 以及旧的 ProcessSerialNumber 结构来完成。

回答by Vijay Singh Rana

For Swift2 version

对于 Swift2 版本

Launch App:

启动应用程序:

let task = NSTask()
task.launchPath = "/usr/bin/env"
task.arguments = ["/Applications/TextEdit.app/Contents/MacOS/TextEdit"]
task.launch()

To get the app using bundle Identifier:

要使用捆绑标识符获取应用程序:

    var apps: [AnyObject] = NSRunningApplication.runningApplicationsWithBundleIdentifier("com.apple.TextEdit")
    var MyApp: [NSRunningApplication] = apps as! [NSRunningApplication]        
            for app in MyApp        
            {
            }

I am still trying to find the way to know "active", "hide" etc state of app, But not succeed till now.

我仍在尝试找到了解应用程序“活动”、“隐藏”等状态的方法,但直到现在还没有成功。

回答by Jay Wong

In swift 4, you can use NSWorkspace.shared.launchApplication(appName:)to open an app. It also makes the launched app at front in my case.

在 swift 4 中,您可以使用NSWorkspace.shared.launchApplication(appName:)来打开一个应用程序。在我的情况下,它还使启动的应用程序位于前面。

You also can try:

你也可以试试:

do {
    try NSWorkspace.shared.launchApplication(at: yourAppURL,
                                             options: .andHideOthers,
                                             configuration: [:])
} catch {
    printError("Failed to launch the app.")
}

Option andHideOthers: Hide all apps except the newly launched one.

选项andHideOthers:隐藏除新启动的应用程序之外的所有应用程序。

回答by Creator

Open App in Mac in Objective C

使用 Objective C 在 Mac 中打开应用程序

#import <Cocoa/Cocoa.h>

int main(int argc, const char * argv[]) {

    @autoreleasepool {
        // insert code here...
        [[NSWorkspace sharedWorkspace] launchApplication:@"/Applications/Books.app"];
    }
    return 0;
}