以编程方式重启OSX应用 [英] Restarting OSX app programmatically

查看:93
本文介绍了以编程方式重启OSX应用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要重新启动我的应用程序,以防我重新加载某些内容需要从头开始.我尝试过了

I need to restart my app in case I reload something that will require a start from the very beginning. I tried this

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

但是我在open

无法访问启动路径

launch path not accessible

推荐答案

尽管问题本身是微不足道的(忘记了路径),但在其他人需要相同功能的情况下,我会进行问答.

Though the problem itself was trivial (forgot the path) I leave question and answer in case someone else needs the same functionality.

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也适用):

Edit (daily Swift syntax change for Sw3; works also for 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)

这篇关于以编程方式重启OSX应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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