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
Post a Comment