0

I am making a NSTask which runs osascript to gracefully quit an application instead of a killall/kill command.

I have this:

let killtask = NSTask()
killtask.launchPath = "/usr/bin/killall"
killtask.launchPath = "/usr/bin/osascript"
killtask.arguments = ["-e","'quit app", ""Transmission"""'"]
killtask.launch()

The troublesome line is the arguments I'm trying to pass.

The command in terminal is like this:

/usr/bin/osascript -e 'quit app "Notes"'

Where am I going wrong? How can I format the arguments so it gets all the single and double quotation marks actually used in the command?

EDIT

Running

killtask.arguments = ["-e \'quit app \"Transmission\"\'"]

Gives me:

0:2: syntax error: A unknown token can’t go here. (-2740)
DanTdd
  • 342
  • 3
  • 10

2 Answers2

1

Here's the correct way to parameterize an AppleScript run via osascript:

let appName = "Transmission"

let killtask = NSTask()
killtask.launchPath = "/usr/bin/osascript"
killtask.arguments = ["-e", "on run {appName}", 
                      "-e", "  quit app appName", 
                      "-e", "end run",
                      appName]
killtask.launch()
foo
  • 3,171
  • 17
  • 18
0

Try to delimit the quotes, and make sure your arguments are correct.

let killtask = NSTask()
killtask.launchPath = "/usr/bin/osascript"
killtask.arguments = ["-e","'quit app \"Transmission\"'"]
killtask.launch()

If the arguments do not work, try putting a backslash in front of the single quotes (') too.

Schemetrical
  • 5,506
  • 2
  • 26
  • 43
  • I'm getting `0:1: syntax error: A unknown token can’t go here. (-2740)` with both `killtask.arguments = ["-e","'quit app \"Transmission\"'"]` and `killtask.arguments = ["-e","\'quit app \"Transmission\"\'"]` – DanTdd May 07 '15 at 03:12
  • My output for the argument is this: `-e, 'quit app "Transmission"'` which seems almost right besides the , – DanTdd May 07 '15 at 03:14
  • One thing is, I believe its `'tell application "AppName" to quit'`, so the arguments should be `killtask.arguments = ["-e","'tell application \"Transmission\" to quit'"]` – Schemetrical May 07 '15 at 03:16
  • The comma is there because the output prints the array of arguments (which prints a comma between every array element). – Schemetrical May 07 '15 at 03:18
  • Hmm still not working :( The command I am trying to do is intact `/usr/bin/osascript -e 'quit app "Notes"'`. If you run this in terminal you will see it work. – DanTdd May 07 '15 at 03:22
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/77142/discussion-between-schemetrical-and-dantdd). – Schemetrical May 07 '15 at 03:29