打开另一个 Mac 应用 [英] Open another Mac app

查看:17
本文介绍了打开另一个 Mac 应用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

推荐答案

Xcode 8.3.2 • Swift 3.1

import Cocoa

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

openPhotos()

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

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

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 及更高版本

使用 NSWorkspace.shared 而不是 NSWorkspace.shared()

这篇关于打开另一个 Mac 应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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