Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Restarting OSX app programmatically

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

like image 882
qwerty_so Avatar asked Apr 24 '15 12:04

qwerty_so


1 Answers

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)
like image 160
qwerty_so Avatar answered Nov 18 '22 22:11

qwerty_so