Java - expected "" but unknown token found

I get this error when trying to execute applescript from my java application. The code is as follows:

String script = "tell application \"Terminal\" to do shell script \"/System/Library/CoreServices/Menu\\ Extras/user.menu/Contents/Resources/CGSession -suspend\" "; ScriptEngineManager mgr = new ScriptEngineManager(); ScriptEngine engine = mgr.getEngineByName("AppleScript"); engine.eval(script); 

I get the following error:

 Exception in thread "main" javax.script.ScriptException: Expected """ but found unknown token. at apple.applescript.AppleScriptEngine.evalScript(Native Method) at apple.applescript.AppleScriptEngine.eval(AppleScriptEngine.java:342) at apple.applescript.AppleScriptEngine.eval(AppleScriptEngine.java:313) at myTestApp.Main.main(Main.java:25) 

Thank you for your attention.

+6
java applescript
source share
2 answers

A guess based on experience ... Perhaps the screened space in the path name is your show stop.

Try calling the script from a location where the path has no spaces, or try doubling the screen space, for example:

 "tell application \"Terminal\" to do shell script \"/System/Library/CoreServices/Menu\\\\ Extras/user.menu/Contents/Resources/CGSession -suspend\" " 

A common cause of strange errors is pathname spaces. So this was my first suggestion that this is causing problems in your script. Then I remembered that sometimes we have to "avoid runaway slashes." This article does not explain why it solved your problem, but it shows how much backslash can be ...

+7
source share

You need to "double" the gap in the path:

  vvvv ...\Menu\\\\ Extras\... 
+6
source share

All Articles