xcode 打开另一个 Mac 应用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27505022/
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-09-15 06:22:28  来源:igfitidea点击:

Open another Mac app

swiftxcodemacosswift3external

提问by Tom Coomer

In my app I would like to open another app that is installed on the User's Mac (such as iPhoto). I am not sure what I should be looking for in the documentation. What is this called and how should I do it? Thank you

在我的应用程序中,我想打开安装在用户 Mac 上的另一个应用程序(例如 iPhoto)。我不确定我应该在文档中寻找什么。这叫什么,我该怎么做?谢谢

回答by Leo Dabus

Xcode 8.3.2 ? Swift 3.1

Xcode 8.3.2 ? 斯威夫特 3.1

import Cocoa

func openPhotos() {
    NSWorkspace.shared().open(URL(fileURLWithPath: "/Applications/Photos.app"))
}

openPhotos()

Or using launchApplication with the app name parameter in the method:

或者在方法中使用带有应用名称参数的launchApplication:

import Cocoa

func openApp(_ named: String) -> Bool {
    return NSWorkspace.shared().launchApplication(named)
}

// Usage

if openApp("Photos") {
    print(true)
}

// Or

print(openApp("Photos") ? true : false)


Swift 4 and later

斯威夫特 4 及更高版本

Use NSWorkspace.sharedinstead of NSWorkspace.shared()

使用NSWorkspace.shared代替NSWorkspace.shared()

回答by vookimedlo

XCode 11 ? MacOS Catalina 10.15 ? Swift 5

代码 11 ? MacOS Catalina 10.15 ? 斯威夫特 5

NSWorkspace.shared.launchApplicationis deprecated and starting from the MacOS 10.15 the new function NSWorkspace.shared.openApplicationshall be used.

NSWorkspace.shared.launchApplication已弃用,从 MacOS 10.15 开始,应使用新功能NSWorkspace.shared.openApplication



Example - open terminal application by its bundle id

示例 - 通过捆绑 ID 打开终端应用程序

guard let url = NSWorkspace.shared.urlForApplication(withBundleIdentifier: "com.apple.Terminal") else { return }

let path = "/bin"
let configuration = NSWorkspace.OpenConfiguration()
configuration.arguments = [path]
NSWorkspace.shared.openApplication(at: url,
                                   configuration: configuration,
                                   completionHandler: nil)


Example - open terminal application by its path

示例 - 按路径打开终端应用程序

let url = NSURL(fileURLWithPath: "/System/Applications/Utilities/Terminal.app", isDirectory: true) as URL

let path = "/bin"
let configuration = NSWorkspace.OpenConfiguration()
configuration.arguments = [path]
NSWorkspace.shared.openApplication(at: url,
                                   configuration: configuration,
                                   completionHandler: nil)

回答by qwerty_so

let task = NSTask.launchedTaskWithLaunchPath(<#path: String#>, arguments: <#[AnyObject]#>)will probably do what you want

let task = NSTask.launchedTaskWithLaunchPath(<#path: String#>, arguments: <#[AnyObject]#>)可能会做你想做的

回答by mabi99

I second the answer by vookimedlo- unfortunately, I cannot yet comment (silly reputation limit) so I post this as an extra answer.

我支持vookimedlo的答案- 不幸的是,我还不能发表评论(愚蠢的声誉限制),所以我将此作为额外的答案发布。

This is just one caveat, which might not affect too many: while launchApplication()accepted a path to an executable (e.g. "MyApp.app/Contents/MacOS/MyApp"), this will result in an error (lacking privileges) with openApplication(::). You have to supply the path to the app bundle ("MyApp.app") instead.

这只是一个警告,可能不会影响太多:虽然launchApplication()接受了可执行文件的路径(例如“MyApp.app/Contents/MacOS/MyApp”),但这将导致openApplication(::). 您必须提供应用程序包(“MyApp.app”)的路径。

Of particular interest when you try to make a helper ("launcher") to add as a login item. See the following and keep my comment in mind:

当您尝试将帮助程序(“启动程序”)添加为登录项时特别感兴趣。请参阅以下内容并记住我的评论:

https://theswiftdev.com/2017/10/27/how-to-launch-a-macos-app-at-login/(GREAT article by Tibor B?decs)

https://theswiftdev.com/2017/10/27/how-to-launch-a-macos-app-at-login/(TiborB?decs 的精彩文章)

BTW, as for vookimedlo's code: in my experience, you don't need to specify the OpenContext.argumentswith [path], you can simply pass a default NSWorkspace.OpenContext()...

顺便说一句,至于vookimedlo的代码:根据我的经验,您不需要指定OpenContext.argumentswith [path],您只需传递一个默认值NSWorkspace.OpenContext()...

回答by Mike Crawford

There are different ways to do that. The most efficient is to use fvork and execve - see man vforkand man execve.

有不同的方法可以做到这一点。最有效的是使用 fvork 和 execve-seeman vforkman execve

Less efficient but more flexible is to use the systemlibrary call. What that actually does is runs a shell - like bash - then passes the string you provide, to bash. So you can set up pipelines, redirection and such.

效率较低但更灵活的是使用system库调用。实际所做的是运行一个 shell——比如 bash——然后将你提供的字符串传递给 bash。因此,您可以设置管道、重定向等。

Or you can send an Apple Event to the Finder: "Tell Finder Open iPhoto".

或者,您可以向 Finder 发送 Apple 事件:“告诉 Finder 打开 iPhoto”。

In the first two cases you want to launch the executable inside the bundle, that is, /Applications/iPhoto.app/Contents/MacOS/iPhoto.

在前两种情况下,您要启动包内的可执行文件,即/Applications/iPhoto.app/Contents/MacOS/iPhoto.

Try the above from the command line, in the Terminal:

在终端中从命令行尝试上述操作:

$ /Applications/iPhoto.app/Contents/MacOS/iPhoto

You'll see the iPhoto App launch.

您将看到 iPhoto 应用程序启动。