objective c - How can I run a terminal command in my swift menu bar app? -


i have made menu bar app , want command run on press

rm -rf ~/.trash/* 

the code have this:

@ibaction func toggleclicked(sender: nsmenuitem) {      let task = nstask()     task.launchpath = "/bin/sh"     task.arguments = ["rm", "-rf", "~/.trash/*"]     task.launch()     task.waituntilexit()  } 

but when run this, following error:

/bin/rm: /bin/rm: cannot execute binary file 

i dont understand why i'm getting error since can open terminal , run /bin/sh, enter in rm -rf ~/.trash/* , works expected.

edit

i have tried change commands nothing happens:

    task.launchpath = "/bin/rm"     task.arguments = ["-rf", "~/.trash/*"] 

to make /bin/sh read command line string need pass -c argument.

your code needs changed follows:

    let task = nstask()     task.launchpath = "/bin/sh"     task.arguments = ["-c", "rm -rf ~/.trash/*"]     task.launch()     task.waituntilexit() 

Comments

Popular posts from this blog

java - Date formats difference between yyyy-MM-dd'T'HH:mm:ss and yyyy-MM-dd'T'HH:mm:ssXXX -

c# - Get rid of xmlns attribute when adding node to existing xml -