简体   繁体   English

Java - 期待“”“但发现了未知的令牌

[英]Java - Expected “"” but found unknown token

I am receiving this error when trying to execute applescript from my java application. 我尝试从我的java应用程序执行applescript时收到此错误。 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)

Thanks for your consideration. 谢谢你的考虑。

A guess based on experience... Maybe the escaped space in the pathname is your show stopper. 基于经验的猜测...也许路径名中的转义空间是你的节目限制。

Try to call a script from a location where the path has no spaces or try to 'double-escape' the escaped space, like so: 尝试从路径没有空格的位置调用脚本或尝试“转义”转义空间,如下所示:

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

A common reason for strange errors are those whitespaces in pathnames. 奇怪错误的一个常见原因是路径名中的空格。 So it was my first guess, that this causes trouble in your script. 所以这是我的第一个猜测,这会导致脚本出现问题。 Then I remembered that sometimes we have to 'escape escaped backslashes'. 然后我记得有时候我们必须“逃脱逃脱的反冲”。 This article doesn't explain, why it solved exactly your problem, but it shows how many backslashes may be needed... 这篇文章没有解释,为什么它解决了你的问题,但它显示了可能需要多少反斜杠......

You need to 'double-escape' the space in the path: 你需要“双重逃避”路径中的空间:

        vvvv
...\Menu\\\\ Extras\...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM