Java - 预期为“”但发现未知令牌

发布于 2024-09-10 02:09:42 字数 791 浏览 3 评论 0原文

当我尝试从我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

弃爱 2024-09-17 02:09:42

基于经验的猜测......也许路径名中的转义空格是你的表演障碍。

尝试从路径没有空格的位置调用脚本,或者尝试“双重转义”转义的空格,如下所示:

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

奇怪错误的常见原因是路径名中的空格。所以这是我的第一个猜测,这会给你的脚本带来麻烦。然后我想起有时我们必须“转义转义的反斜杠”。 本文没有解释为什么它能准确解决你的问题,但它显示可能需要多少个反斜杠......

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...

烟酉 2024-09-17 02:09:42

您需要“双重转义”路径中的空格:

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

You need to 'double-escape' the space in the path:

        vvvv
...\Menu\\\\ Extras\...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文