如何在苹果脚本中隐藏特定窗口

时间:2021-04-15 21:24:59

标签: applescript

我想要一个脚本来显示和隐藏带有特定文件的 VIM 窗口和键盘快捷键(将通过 BetterTouchTool 完成)。我的脚本中的所有内容都可以正常工作,希望隐藏窗口。

我试图找到 AXRaise 的反面,但没有找到关于此的一些文档。

toogle-journal.scpt

if application "MacVim" is running then
    log "macvim running"
    tell application "System Events"
        log "tell app system events"
        tell application process "MacVim"
            log "- tell app process macvim"
            if exists (first window whose name contains "journal") then
                log "-- window contains journal"
                # @todo have to toggle between visible and not visible
                if (get frontmost) then
                    log "true: " & (get frontmost)
                    # How to hide the window ??
                else
                    log "false: " & (get frontmost)
                    # set visible to true
                    # set frontmost to true
                    perform action "AXRaise" of (first window whose name contains "journal")
                end if
            else
                log "- no window with journal"
                do shell script "/Users/foobar/bin/jo > /dev/null 2>&1 &"
            end if
        end tell
    end tell
else
    log "no macvim running"
    do shell script "/Users/foobar/bin/jo > /dev/null 2>&1 &"
end if

1 个答案:

答案 0 :(得分:1)

basic vanilla AppleScript的上下文中,应用程序窗口有两种方式隐藏

  • 如果 miniaturizable propertytrue,将其 miniaturized property 设置为:true
  • 将其 visible property 设置为:false
    • 注意:当将 windowvisible property 设置为 false 时,它不再出现在下面显示的打开窗口列表中其 UI 中的 窗口 菜单,并将保持这种状态,直到它重置为 true,而应用程序 仍在运行。
    • 关闭 MacVim,通常情况下,窗口 仍然以这种方式隐藏会导致 退出而不保存? 有修改的缓冲区,如果您现在退出所有更改都将丢失。还是退出? [取消] [退出] 对话框

示例 AppleScript 代码

  • 如果 MacVim 是最活跃的应用程序,则:

通过最小化隐藏

tell application "MacVim" to ¬
    set miniaturized of ¬
        (first window whose name contains "journal") to true

miniaturized 设置为:true

取消隐藏
tell application "MacVim" to ¬
    set miniaturized of ¬
        (first window whose name contains "journal") to false

或者:

tell application "MacVim" to ¬
    set visible of ¬
        (first window whose name contains "journal") to true

隐藏,将 visible 设置为:false

tell application "MacVim" to ¬
    set visible of ¬
        (first window whose name contains "journal") to false

取消隐藏,将 visible 设置为:true

tell application "MacVim" to ¬
    set visible of ¬
        (first window whose name contains "journal") to true

  • 如果MacVim不是最活跃的应用程序,则在系统事件下执行上述任一操作,例如:
tell application "System Events" to ¬
    tell application "MacVim" to ¬
        set miniaturized of ¬
            (first window whose name contains "journal") to true

注意:示例 AppleScript 代码就是这样,没有任何包含的错误处理不包含任何额外的错误处理。用户有责任根据需要添加任何错误处理。查看 try 中的 error statementAppleScript Language Guide statement。另见Working with Errors。此外,在适当的情况下,可能需要在事件之间使用 delay command,例如delay 0.5,适当设置 delayvalue