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

发布于 2024-11-28 08:22:11 字数 1180 浏览 0 评论 0原文

我遇到了使用 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 (有效):

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

I'm running into problems with a shell script that utilizes a small portion of Applescript. When I compile it with Applescript editor it works. It does not though within a shell script.

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)

Here is the shell code:

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 (that works):

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

[updated] / [solved]

This took care of any kind of problems I was having trying to modify the applescript to work within a shell script:

## 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...

It's very cool that you're able to put pure applescript directly into a shell script. ;-)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

蘸点软妹酱 2024-12-05 08:22:11

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

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

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

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

Each osascript(1) command is completely separate process, and therefore a completely separate script, so you can’t use state (such as variables) between them. You can build a multi-line script in osascript using multiple -e options -- they all get concatenated with line breaks between them to form the script. For a sufficiently long script, a separate file or a “here document”, as you used in your eventual solution, is a good way to go.

Also, if your script is mostly (or entirely!) AppleScript, you can make a “shell” script that simply is AppleScript using a shebang file that invokes osascript:

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

...and then use do shell script as necessary.

月光色 2024-12-05 08:22:11

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

osascript /path/to/script

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

tell process "MyProcess" to perform action.

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

[*]

Instead of using the -e flag, you can simply store your applescript code in a small text file and call

osascript /path/to/script

Also, if you're telling an application or process to do just one thing, you can write it like this:

tell process "MyProcess" to perform action.

Now that I think about it, running each line separately with the -e flag probably won't work because I don't think all the lines will connect and run as one program. For example, I just tested using osascript -e to set a variable. I then used a separate osascript -e to read the variable, and it couldn't.

[*]

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文