我遇到了使用一小部分 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/