osascript /语法错误:预期行结束但找到命令名称.(-2741)

dr.*_*ull 14 syntax applescript syntax-error osascript

我遇到了一个使用一小部分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
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

[更新]/[已解决]

这解决了我试图修改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...
Run Code Online (Sandbox Code Playgroud)

您可以将纯Applecript直接放入shell脚本中,这非常酷.;-)

Chr*_*s N 6

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

此外,如果您的脚本主要(或完全!)AppleScript,您可以使用调用的shebang文件创建一个简单的AppleScript的"shell"脚本osascript:

#!/usr/bin/osascript
display dialog "hello world"
Run Code Online (Sandbox Code Playgroud)

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


小智 3

您可以简单地将 applescript 代码存储在一个小文本文件中并调用,而不是使用 -e 标志

osascript /path/to/script
Run Code Online (Sandbox Code Playgroud)

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

tell process "MyProcess" to perform action.
Run Code Online (Sandbox Code Playgroud)

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

[ * ]