以编程方式重新启动OSX应用程序

时间:2015-04-24 12:30:18

标签: macos swift restart

我需要重启我的应用程序,以防我重新加载需要从头开始的东西。我试过这个

  let path = NSBundle.mainBundle().resourcePath!.stringByDeletingLastPathComponent.stringByDeletingLastPathComponent
  let task = NSTask()
  task.launchPath = "open"
  task.arguments = [path]
  task.launch()
  exit(0)

但我在open

上收到错误
  

无法访问启动路径

1 个答案:

答案 0 :(得分:3)

虽然问题本身是微不足道的(忘了路径)但我会留下问题和答案,以防其他人需要相同的功能。

let path = NSBundle.mainBundle().resourcePath!.stringByDeletingLastPathComponent.stringByDeletingLastPathComponent
let task = NSTask()
task.launchPath = "/usr/bin/open"
task.arguments = [path]
task.launch()
exit(0)

编辑(Sw3的每日Swift语法更改;也适用于Sw4):

let url = URL(fileURLWithPath: Bundle.main.resourcePath!)
let path = url.deletingLastPathComponent().deletingLastPathComponent().absoluteString
let task = Process()
task.launchPath = "/usr/bin/open"
task.arguments = [path]
task.launch()
exit(0)