AppleScript to create a “ShowDesktop” shortcut
When I’m on Os X I often miss the “show desktop” shortcut icon you can easily get on Windows.
Yes, there is the expose version but it doesn’t really work the same. My Google-fu showed few solutions (like clicking a visible piece of your desktop while holding option+command down) but none that I liked so here is my input.
Before loading additional software on my computer I like to try and find a way to use what’s shipped-in. This took me directly to AppleScript a pretty simple scripting language for Apple environments (doh!).
Once in AppleScript I found a way to have it working via GUI scripting and “System Events” calls. Of course if I knew Cocoa I’d probably come with a much snappier result through the NSEvent library, but I don’t...
Anyway here is the code, if anybody reading this needs more info just use the comments.
--
tell application "Finder"
activate
end tell
tell application "System Events"
set quit delay to 0
keystroke "h" using {command down, option down}
keystroke "m" using {command down, option down}
end tell
Notes
-Supposedly for GUI scripting you need to “Enable access for assistive devices” (System Preferences>Universal Access): In my case while running 10.6.2 it works without, not sure why...
-keystroke "h" using {command down, option down} can be replaced by [set visible of every process whose visible = true and name ≠ "Finder" to false] but don’t get fooled it still is a GUI command going through the “Systems Events” app.
-keystroke "m" using {command down, option down} can be replaced by [set the collapsed of windows to true] but the script behaves differently, i.e. on my system it minimizes windows one by one instead of all at once.
-Although I didn’t try very hard I couldn’t find out how to send a “visible = false” or “keystroke h using command down” to the finder after all other applications have been hidden hence my “minimize all” command.
Here are some references and tools for Apple Scripts.
AppleScript @ macautomatation.com
KeyCode (can be useful for GUI scripting if you use “keycode”)
Apple Training Series: Apple Script 1-2-3


