Miniaturise all windows with AppleScript

tell application "System Events"
    set mywindows to windows of (processes where background only is false)
    repeat with theItem in windows of (processes where background only is false)
        set miniaturized of theItem to true
    end repeat
end tell

I get

error "System Events got an error: Can’t set miniaturized of UI element to any." number -10006 from miniaturized of UI element to any


I answered a similar question over on Stack Overflow, around the time I was still working mostly in High Sierra. Testing it today, it still works on Catalina.

Here's the salient part of that answer:

tell application id "com.apple.systemevents" to set the value of ¬
        attribute "AXMinimized" of every window of every process ¬
        to true

Unfortunately, it's not going to minimise them all simultaneously, which would be lovely. It does them one-by-one, not necessary starting with the one that's right in front of you. Therefore, if you have many windows open, it can appear at first that it's not doing anything, but in fact it's minimising windows obscured by the frontmost one), and they'll minimise in turn, which is fun to see the first time, but irksome every other time when you just want it done now.

Another point that @user3439894 kindly reminded me of is that System Events is only aware of objects that exist on the currently active desktop. Therefore, this method won't be to minimise windows that are on other desktops/spaces are off-screen.

However, if you aren't specifically looking to minimise windows, but rather just get them out of sight, then you can elect to hide the processes instead:

tell application id "com.apple.systemevents" to ¬
        set visible of every process to false

The major annoyance here is that Finder remains visible, and will need to have an extra line of code after to get rid of its windows, which I suggest should be:

tell application id "com.apple.Finder" to set ¬
        miniaturized of every window to true

The benefits, however, are that all the windows across all desktops/screens will be gone, not just the ones on your current desktop; and this takes place very quickly, almost instantaneously (apart from Finder). Activating the application by clicking on its dock icon for example, makes the process visible again; typically, this means that the application and all of its windows that were previously visible will re-appear en masse. Whether that's a positive or negative will depend on your needs.


The reason your code doesn't work is because miniaturised is not a property of the window object belonging to the processes suite of System Events' AppleScript dictionary. It is a property of window objects belonging to the standard suite of any scriptable application's AppleScript dictionary (including SystemEvents, although it doesn't have any windows of its own); despite sharing a common name, these two window class objects are distinct and unrelated objects in AppleScript.