-2

I have this line of code in a method:

system("osascript -e 'tell app "System Events" to restart'");

As you can see, I've got two quotation marks, and since these terminal commands have to be so specific, I need to know another way to run a system command from ObjC. I've already tried using '' and the / method but that didn't work out.

jscs
  • 63,694
  • 13
  • 151
  • 195
cyw
  • 520
  • 1
  • 4
  • 14

1 Answers1

5

You need to "escape" the quote characters to tell the compiler that the should be part of the string rather than delimiters of the string. You say you "tried using … the / method", but you got the wrong character. You escape characters using the backslash, not the forward slash:

-(IBAction)reboot:(id)sender{
    system("osascript -e 'tell app \"System Events\" to restart'");
}
Ken Thomases
  • 88,520
  • 7
  • 116
  • 154