15

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

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

这是外壳代码:

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 脚本中,这非常酷。;-)

4

2 回答 2

6

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

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

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

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

于 2013-04-05T17:27:29.253 回答
2

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

osascript /path/to/script

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

tell process "MyProcess" to perform action.

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

[ * ]

于 2011-08-06T03:24:49.220 回答