Show desktop when button clicked (macOS)

I'm making a macOS app using SwiftUI and I would like to minimise (hide) all open windows when I click a button.

Is this possible with SwiftUI and if so, how would I go about implementing this? Thanks

Answered by DTS Engineer in 844332022

To minimize windows of your App currently, you could dip into AppKit and iterate through NSApplication.shared.windows to minimize each one. For example:

private func minimizeWindows() {
    for window in NSApplication.shared.windows {
        if window.isMiniaturizable {
            window.miniaturize(nil)
        }
    }
}
Accepted Answer

To minimize windows of your App currently, you could dip into AppKit and iterate through NSApplication.shared.windows to minimize each one. For example:

private func minimizeWindows() {
    for window in NSApplication.shared.windows {
        if window.isMiniaturizable {
            window.miniaturize(nil)
        }
    }
}
Show desktop when button clicked (macOS)
 
 
Q