Restarting OSX app programmatically

倾然丶 夕夏残阳落幕 提交于 2019-12-05 02:29:33

问题


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)

but I get an error upon the open

launch path not accessible


回答1:


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)

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)


来源:https://stackoverflow.com/questions/29847611/restarting-osx-app-programmatically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!