Java - 预期为“”但发现未知令牌
当我尝试从我的 java 应用程序执行 applescript 时,我收到此错误。代码如下:
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);
我收到以下错误:
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)
感谢您的考虑。
I am receiving 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)
Thanks for your consideration.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
基于经验的猜测......也许路径名中的转义空格是你的表演障碍。
尝试从路径没有空格的位置调用脚本,或者尝试“双重转义”转义的空格,如下所示:
奇怪错误的常见原因是路径名中的空格。所以这是我的第一个猜测,这会给你的脚本带来麻烦。然后我想起有时我们必须“转义转义的反斜杠”。 本文没有解释为什么它能准确解决你的问题,但它显示可能需要多少个反斜杠......
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:
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: