Syntax osascript/语法错误:应为行尾,但找到命令名。(-2741)

Syntax osascript/语法错误:应为行尾,但找到命令名。(-2741),syntax,applescript,syntax-error,osascript,Syntax,Applescript,Syntax Error,Osascript,我在使用Applescript的一小部分的shell脚本时遇到了问题。当我用Applescript编辑器编译它时,它可以工作。但它不在shell脚本中 44:49:语法错误:应为行尾,但找到命令名。(-2741) 23:28:语法错误:应为行尾,但找到“after”。(-2741) 以下是shell代码: osascript -e 'tell application "System Events" -e 'activate' osascript -e 'tell process "Applic

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

44:49:语法错误:应为行尾,但找到命令名。(-2741) 23:28:语法错误:应为行尾,但找到“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(有效):

[更新]/[解决]

这解决了我试图修改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...
##shell脚本代码
echo“shell脚本代码”
echo“shell脚本代码”
##applescript代码

osascript不使用-e标志,只需将applescript代码存储在一个小文本文件中并调用

osascript /path/to/script
此外,如果您告诉应用程序或流程只做一件事,您可以这样编写:

tell process "MyProcess" to perform action.
现在我考虑一下,使用-e标志分别运行每一行可能不起作用,因为我不认为所有的行都会作为一个程序连接和运行。例如,我刚刚测试了使用osascript-e设置一个变量。然后,我使用一个单独的osascript-e来读取变量,但它无法读取


[*]

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

此外,如果您的脚本大部分(或全部)是AppleScript,则可以使用调用
osascript
的shebang文件创建一个“shell”脚本,该脚本就是AppleScript:

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

…然后根据需要使用
执行shell脚本。

谢谢您的解释。实际上,我找到了一个很好的方法,可以将applescript直接放入shell脚本中,而不必经过所有的修改工作。(见上文)——
#!/usr/bin/osascript
display dialog "hello world"