syntax - osascript/语法错误 : Expected end of line but found command name. (-2741)

标签 syntax applescript syntax-error osascript

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

44:49: syntax error: Expected end of line but found command name. (-2741) 23:28: syntax error: Expected end of line but found “after”. (-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 脚本中是非常酷的。 ;-)

最佳答案

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

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

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

...然后使用 do shell script有必要的。

关于syntax - osascript/语法错误 : Expected end of line but found command name. (-2741),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6964486/

相关文章:

sql - 使用Mono.Data.SqliteClient库的SQLite语法

class - Delphi : Error 2008 Incompatible types for a class property

ruby - "string_name = string_name[3..-1]"下面这行 ruby​​ 代码是什么意思?

syntax - rust 编译器 "error: can' t 捕获 fn 项中的动态环境是什么;使用|| { ... } closure form instead"的意思,如何解决呢?

properties - 我可以找出 AppleScript 对象是否具有特定属性吗?

python - 每n步执行一次while循环

识别出 Javascript 语法错误 'for' 关键字

matlab - MATLAB 中的级联 if/elseif/else 构造是否有简洁的替代方案?

php - 执行多个查询时 PHP 出现 MySQL 语法错误

macos - 如何判断菜单项是否为 'checked' ?