【发布时间】:2020-03-21 01:57:22
【问题描述】:
我编写了一个命令行工具来从 IMDB 获取电影信息。它在 JavaScript 中,称为 movinfo。我在 AppleScript 中运行它:
tell application "System Events"
set movT to "Back-to-the-future"
set exportPath to "export PATH=$PATH:/usr/local/bin:;"
set oriInfo to do shell script exportPath & "movinfo " & quoted form of movT
return oriInfo
end tell
效果很好。但有时 movinfo 需要很长时间才能从 Internet 获取信息。所以我想添加一个函数来检查获取是否完成。我首先尝试“忽略...结束忽略”结构:
tell application "System Events"
ignoring application responses
set movT to "Back-to-the-future"
set exportPath to "export PATH=$PATH:/usr/local/bin:;"
set oriInfo to do shell script exportPath & "movinfo " & quoted form of movT
end ignoring
end tell
tell application "System Events"
repeat 30 times
try
return oriInfo
exit repeat
on error
delay 1
end try
end repeat
do shell script "killall System\\ Events"
end tell
但这不起作用。也许我可以使用命令行工具来完成这项工作。但是我真的不太了解 JavaScript 和 CLI。我想在 AppleScript 中执行此操作。 希望有人能告诉我代码有什么问题,或者如何在 AS 中执行此操作?
【问题讨论】:
-
您的整个系统事件代码毫无意义。
do shell script属于标准(脚本)添加并且不发送 Apple 事件。 AppleScript 的基本设计是同步的,因为常规编译的脚本没有运行循环。而且do shell script应该是同步运行的。显然 javascript 是异步的,所以可以管理时间。 -
@vadian,谢谢,我在谷歌上搜索了一些关于同步和异步之间的差异。它们对我来说是新知识。
-
同步意味着代码在单个线程上串行执行。异步意味着代码在多个线程上并发执行。
标签: applescript