显示Finder,并通过AppleScript创建新的窗口,如果没有的话。

问题描述 投票:0回答:1

我想创建一个AppleScript,通过Keyboard Maestro中的按键命令触发,让我可以直接切换显示或隐藏Finder窗口。如果在切换显示Finder时,如果没有现有的窗口,则创建一个窗口并将其打开到我的主目录。

下面的AppleScript可以工作。然而,在激活Finder和检测是否有任何打开的窗口之间,似乎有一个竞赛条件。if not (window 1 exists)因此 delay 0.5.

这个问题(我认为是检测现有Finder窗口存在的一个竞赛条件)导致这个脚本经常在已经存在一个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
applescript finder keyboard-maestro
1个回答
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编辑器,选择该宏,然后从编辑菜单中选择复制为> 复制UUID。

然后在AppleScript中写道

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

取代 <Script-UUID> 与复制的真实UUID


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
© www.soinside.com 2019 - 2024. All rights reserved.