0

我想创建一个 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
4

2 回答 2

0

请尝试这种更简单的语法,它只使用Finder术语

tell application "Finder"
    if frontmost then
        set visible of process "Finder" to false
    else
        if (count windows) is 0 then reveal home
        activate
    end if
end tell

编辑:

要运行 Keyboard Maestro 宏,请打开 Keyboard Maestro Editor,选择宏,然后从 Edit 菜单中选择 Copy as > Copy UUID。

然后在 AppleScript 中写

tell application "Keyboard Maestro Engine" to do script "<Script-UUID>"

替换<Script-UUID>为复制的真实 UUID

于 2020-06-10T13:00:56.823 回答
0

最终我需要在运行 count windows 命令之前激活 Finder,否则我会得到不一致的窗口计数。有时即使已经打开了一个窗口,它也会为 0。到目前为止,这段代码对我来说效果很好。

tell application "Finder"
    if frontmost then
        set visible of process "Finder" to false
    else
        activate
        if (count windows) is 0 then
            open home
            tell application "Keyboard Maestro Engine" to do script "<Script-UUID>"
        end if
    end if
end tell
于 2020-06-10T21:52:04.110 回答