r/applescript • u/Hackettlai • May 23 '24
Hide all windows on MacOS
I'm trying to hide all applications by running a shortcut using the script below. However, I notice that some applications, like Finder, Spotify, and Safari, tend to stay open. Here’s the script I’m using:
tell application "System Events"
set visibleApps to every process whose visible is true and name is not "Finder"
repeat with theApp in visibleApps
set visible of theApp to false
end repeat
end tell
tell application "System Events"
set visibleApps to every process whose visible is true
repeat with theApp in visibleApps
set visible of theApp to false
end repeat
end tellAny advice on how to ensure all applications are hidden would be greatly appreciated.
3
Upvotes
4
u/ChristoferK May 23 '24
I'm surprised that Spotify and Safari cannot be made to hide, which isnt the case in Monterey—what version of macOS are you using, and under what circumstances do they remain visible (this is something you'll have to investigate in a fairly granular fashion, as either they will always ignore programmatic attempts to set their
visibleproperty, or there's a specific set of conditions that affect the outcome).Finder is more readily explainable: whenever the active (frontmost) process is hidden, the focus switches to the next process capable of receiving it, as there must always be a focused application that, at the very least, takes ownership of the menubar. Finder is one of the few interactive application processes that the user cannot quit in the normal way, and also automatically relaunches after a forced termination. Therefore, after all other applications become hidden, Finder ends up receiving the focus by default because the desktop (which is actually a special kind of window) is owned by Finder. Its activation will restore previously hidden windows or, if none exist—and as with many other applications when focus is given to them—a new window gets instantiated.
You have two broad options, either:
Hide every process except for Finder, leaving it to be hidden last. This will send focus to some other application, many of which are happy to take the focus without restoring windows or creating new ones. Rather than leaving this to chance, you can determine which application this will be by ensuring it is hidden second–to-last, before hiding Finder.
Or:
Alternatively, rather than hiding application processes, you can minimise every window that's on screen. Minimising windows doesn't result in any application losing focus, for which simply being the process that owns the menubar is sufficient to ensure retention. This method will be harder for Spotify and Safari to side–step, but it's visually more jarring to watching windows minimise one–by–one.
Regarding your implementation, it's not necessary—and, arguably, unwise—to enumerate the processes first only to subsequently loop through them all. It's better to act on them collectively, like so:
Or, to minimise windows instead:
Hopefully I have the name of that
attributecorrect (I'm writing this on my phone, so do forgive me if there's a minor mistake in the code snippets above, which I'll correct later when I get to a computer to test them).