这是一个applescript方式。如您所见,您不能依赖特定的延迟时间。因此,我们通过检查它是否在正在运行的进程列表中来手动等待 Finder 退出。当它不再在列表中时,我们知道它已经退出,我们可以再次激活它。
您还会注意到,由于重复循环,我在脚本中添加了时间检查。万一出现问题,我们不希望重复循环永远运行。因此,如果它运行超过 10 秒,我们会自动退出重复循环。
tell application "Finder" to quit
set inTime to current date
repeat
tell application "System Events"
if "Finder" is not in (get name of processes) then exit repeat
end tell
if (current date) - inTime is greater than 10 then exit repeat
delay 0.2
end repeat
tell application "Finder" to activate
这是该代码的 osascript 版本。
/usr/bin/osascript -e 'tell application "Finder" to quit' -e 'set inTime to current date' -e 'repeat' -e 'tell application "System Events"' -e 'if "Finder" is not in (get name of processes) then exit repeat' -e 'end tell' -e 'if (current date) - inTime is greater than 10 then exit repeat' -e 'delay 0.2' -e 'end repeat' -e 'tell application "Finder" to activate'