Applescript - 如何从终端 运行 单个 bash 命令并在继续之前等待响应?

Applescript - How to run a single bash command from terminal and wait for response before continue?

我有一个 modelinfo.sh 文件,如果我在终端中 运行 它 echos/saves 结果为 TXT 文件。

要从终端执行此文件,我使用命令:

./modelinfo.sh -s C8QH74G6DP11

使用此命令保存给定序列号的结果:C8QH74G6DP11

我需要获取 5000 个连续出版物的报告,所以我认为 AppleScript 可能对我有帮助?

我用 AppleScript 写了这段代码:

tell application "Terminal"
    do script ("./modelinfo.sh -s C8TJ14JWDP11") in window 1
    do script ("./modelinfo.sh -s C8QH74G6DP12") in window 1
    do script ("./modelinfo.sh -s C8QKGFWSDP13") in window 1
    do script ("./modelinfo.sh -s C8QKFR5FDP14") in window 1
end tell

使用上面的代码,我的 IP 被阻止,我只得到第一个序列号的报告。

我也试过:

on delay duration
    set endTime to (current date) + duration
    repeat while (current date) is less than endTime
        tell AppleScript to delay duration
    end repeat
end delay
tell application "Terminal"
    do script ("./modelinfo.sh -s C8TJ14JWDP11") in window 1
    delay 20
    do script ("./modelinfo.sh -s C8QH74G6DP12") in window 1
    delay 20
    do script ("./modelinfo.sh -s C8QKGFWSDP13") in window 1
    delay 20
    do script ("./modelinfo.sh -s C8QKFR5FDP14") in window 1
end tell

但此代码也无济于事..

上次尝试:

tell application "Terminal"
    do script ("./modelinfo.sh -s C8TJ14JWDP11") in window 1
end tell

我可以 运行 多次使用最后一个脚本,而且我总是能在不阻止我的 IP 的情况下获得报告。

看起来 Applescript 运行 即使我的 IP 被屏蔽了,也能一次完成所有 4 个连续剧? 因为我能够 运行 多次单次检查而不会被阻止。

谁能帮我指出正确的方向?

Applescript 可以做到这一点吗? 或者我可以制作一个新的 bash 文件,其中 运行 将我所有的 5000 个命令 1 接 1 地存储起来吗?

谢谢

好吧,这是 Applescript 中使用您正在尝试的方法的常用方法。

property serialList: {"C8TJ14JWDP11", "C8QH74G6DP12", "C8QKGFWSDP13", "C8QKFR5FDP14"}

tell application "Terminal"
repeat with aSerial in serialList
    do script ("./modelinfo.sh -s " & aSerial) in window 1
    delay 20
end

这应该有效。但是,终端的状态 window 需要完成该过程并为下一次调用做好准备才能正常工作,因此延迟 20 可能过于简单。将上面的代码放在try块中,看看有没有错误。

try
-- above code goes here
on error err
display dialog err
end try

然而,另一种方法是直接在 Applescript 中包含 shell 命令,而无需通过终端应用程序,使用

do shell script

您必须 post 您的 modelinfo.sh 文件的内容才能了解这种可能性。

更新答案

如果您想计算行数并指示进度,请将下面的代码替换为:

#!/bin/bash
declare -i total
total=$(wc -l <sn.txt)     # count the lines in sn.txt
i=1
while read sn; do
   echo "Fetching $sn ($i of $total)"
   ./modelinfo.sh -s "$sn"
   ((i++))
done < sn.txt

原答案

不知道为什么有人会为此使用 Applescript - 这显然是一个从终端到 运行 的简单 bash 脚本。

假设您的序列号保存在一个名为 sn.txt 的文件中,如下所示:

C8QH74G6DP11
C8TJ14JWDP11
C8QH74G6DP12
C8QKGFWSDP13

然后将以下内容保存在 HOME 目录中名为 fetch 的文件中。它一次读取一个序列号并提取它们。

#!/bin/bash
while read sn; do
   echo Fetching $sn...
   ./modelinfo.sh -s "$sn"
done < sn.txt

然后您将进入终端并键入以下内容以使其可执行:

chmod +x fetch

然后您可以 运行 输入

./fetch

您可以通过按住 Command 并点击空格键然后键入 Ter 来启动终端,Spotlight 会猜测您指的是终端,然后您只需按 Enter 即可实际启动它。