【问题标题】:osascript / syntax error: Expected end of line but found command name. (-2741)osascript / 语法错误:预期的行尾,但找到了命令名称。 (-2741)
【发布时间】:2011-10-21 07:48:06
【问题描述】:

我在使用一小部分 Applescript 的 shell 脚本时遇到了问题。当我用 Applescript 编辑器编译它时,它可以工作。它不在 shell 脚本中。

44:49:语法错误:预期的行尾,但找到了命令名称。 (-2741) 23:28:语法错误:预期的行尾,但在“之后”找到。 (-2741)

这是shell代码:

osascript -e 'tell application "System Events" -e 'activate'

osascript -e 'tell process "Application 10.5" -e 'set frontmost to true' -e 'end tell'

osascript -e 'delay 1' -e 'keystroke return' -e 'delay 1' -e 'keystroke return'

end tell

Applescript(有效):

tell application "System Events"
activate
tell process "Application 10.5"
    set frontmost to true
end tell

delay 1
keystroke return
delay 1
keystroke return

end tell

[更新]/[已解决]

这解决了我在尝试修改 applescript 以在 shell 脚本中工作时遇到的任何问题:

## shell script code

echo "shell script code"
echo "shell script code"

## applescript code

osascript <<EOF
tell application "Scriptable Text Editor"
    make new window
    activate
    set contents of window 1 to "Hello World!" & return
end tell
EOF

## resume shell script...

能够将纯applescript 直接放入shell 脚本中,这非常酷。 ;-)

【问题讨论】:

    标签: syntax applescript syntax-error osascript


    【解决方案1】:

    每个osascript(1) 命令都是完全独立的进程,因此是一个完全独立的脚本,因此您不能在它们之间使用状态(例如变量)。您可以使用多个-e 选项在osascript 中构建多行脚本——它们都与它们之间的换行符连接起来以形成脚本。对于足够长的脚本,您在最终解决方案中使用的单独文件或“此处文档”是一个不错的选择。

    另外,如果您的脚本大部分(或完全!)AppleScript,您可以使用调用osascript 的shebang 文件制作一个简单的AppleScript 的“shell”脚本:

    #!/usr/bin/osascript
    display dialog "hello world"
    

    ...然后根据需要使用do shell script

    【讨论】:

      【解决方案2】:

      您可以简单地将您的 AppleScript 代码存储在一个小文本文件中并调用

      ,而不是使用 -e 标志
      osascript /path/to/script
      

      另外,如果你告诉应用程序或进程只做一件事,你可以这样写:

      tell process "MyProcess" to perform action.
      

      现在我考虑了一下,使用 -e 标志单独运行每一行可能行不通,因为我不认为所有行都会连接并作为一个程序运行。例如,我刚刚测试了使用 osascript -e 设置变量。然后我使用单独的 osascript -e 读取变量,但它不能。

      [*]

      【讨论】:

      • 感谢您的解释。实际上,我想出了一个很好的方法,可以将 applescript 直接放入 shell 脚本,而无需完成所有修改它的工作。 (见上文)-
      猜你喜欢
      • 1970-01-01
      • 2023-03-31
      • 2021-12-09
      • 2019-04-05
      • 2018-06-28
      • 2019-11-12
      • 2020-04-18
      • 2022-01-15
      • 2020-10-03
      相关资源
      最近更新 更多