我想创建一个 AppleScript 以通过 Keyboard Maestro 中的键盘命令触发,它允许我直接切换显示或隐藏 Finder 窗口。如果在切换显示 Finder 时,如果没有现有窗口,请创建一个并将其打开到我的主目录。
以下 AppleScript 有效。但是,在激活 finder 和检测是否有任何打开的窗口之间似乎存在竞争条件if not (window 1 exists)
,因此delay 0.5
.
问题(我认为这是检测现有 Finder 窗口存在的竞争条件)导致此脚本经常在已经存在的情况下创建新的 Finder 窗口。if not (window 1 exists)
并不总是正确的。
任何想法,调整或肯定这就是它的方式将不胜感激!
tell application "System Events"
set activeApp to name of application processes whose frontmost is true
if ((activeApp as string) is equal to "Finder") then
set visible of process "Finder" to false
else
tell application "Finder"
activate
delay 0.5
if not (window 1 exists) then
make new Finder window
set thePath to POSIX file "/Users/jon"
set the target of the front Finder window to folder thePath
end if
end tell
end if
end tell