Reset Your Workspace at the Push of a Button

A simple script to clean up the windows in your current workspace.

I’ve dabbled with desktop workspace tools over the years, but nothing seems to stick. Early Mac windowing utilities, Linux’s virtual desktops, Spaces on the modern day Mac — and at one point even a Windows box with two monitors and a whole lot of manual window snapping…

In everyday use there’s often a bit of an impedance mismatch with these kinds of utlities, where sometimes you wish the system would be more aggressive about keeping things where they are, and other times you wish it was easier to move something to another region or otherwise reorganise. No doubt, the big dream is an superintelligent window resizing bot! But for now I’ve fallen into the pattern of lots of overlapping windows on a single large screen.

This works pretty well on macOS, with its single menubar, compact window chrome, and fast show-all-windows and show-desktop shortcuts. It might seem a bit dirty at first, but I prefer seeing chunks of quite a few background windows and a sliver of Desktop at once, and not having to decide exactly what goes where all the time.

The annoying thing is the cleanup act required when switching to a new project or task. But with a few lines of AppleScript, we can automate it.

Getting Started

One approach would be to close windows for all open apps. We could use System Events to get a list of all open user apps and work from there:

tell application "System Events"
  set apps to name of every process where background only is false
end tell

This doesn’t really work for me though, as aside from the main work apps I want to clear up I often have one or two other apps open with unsaved changes (such as a Pages document, or a bunch of temporary notes in unsaved TextMate documents). I don’t want to see close confirmation dialogs for those, and I certainly don’t want to force them to close and risk losing data.

In the end I decided on a whitelist solution, simply hard coding the work apps I want cleaned up, along with a few others that are often open but never have unsaved documents (such as iTunes, Messages, and Notes). If anything else is still open after that I can review it manually.

set apps to {"Safari", "Google Chrome", "Firefox", "Mail", "Terminal", "Xcode", "Atom", "Sketch", "System Preferences", "Slack", "Messages", "Notes", "iTunes", "Finder"}

It’s then straightforward to loop through the list and tell each to close all of its windows. Some other options here are to use close every window without saving, or even just quit. (If you usually have an ongoing process running in Terminal, at this point it will ask to confirm to close — but you can actually set Terminal windows to close instantly).

repeat with this in apps
  if application this is running then
    tell application this
      close every window
    end tell
  end if
end repeat

Once all the windows are cleaned up, I’d like a fresh Terminal window to get started with my next project. This is easy with a final few lines:

tell application "Terminal"
  do script ""
  activate
end tell

The Complete Script

Here’s our final product. You can test it works as intended by pasting into the Script Editor application and choosing Run.

on run
  set apps to {"Safari", "Google Chrome", "Firefox", "Mail", "Terminal", "Xcode", "Atom", "Sketch", "System Preferences", "Slack", "Messages", "Notes", "iTunes", "Finder"}

  repeat with this in apps
    if application this is running then
      tell application this
        close every window
      end tell
    end if
  end repeat

  tell application "Terminal"
    do script ""
    activate
  end tell
end run

Setting a Keyboard Shortcut

For the script to be useful, we’ll need to be able to run it from the keyboard. We can do this easily using Automator to create a new macOS service.

First, open Automator and create a new document with the Service type.

Creating a new service with Automator.
Creating a new service with Automator.

Next, search for the Run AppleScript action and double-click to add it. Under the Service receives option, choose no input, and then paste in the script. You can test it by choosing Run from the toolbar.

Adding a new AppleScript action to the Automator service.
Adding a new AppleScript action to the Automator service.

Choose a name for the script and save it. You should then see it immediately under the Services item in any application menu.

The new service shows in the Finder’s services menu.
The new service shows in the Finder’s services menu.

Finally, we can add a keyboard shortcut for the service. Open System Preferences, choose Keyboard, and then Shortcuts from the tab bar. You should find your new service under Services in the list. Hit Add Shortcut, enter a keyboard shortcut, and that’s it!

Adding a keyboard shortcut for the service in System Preferences.
Adding a keyboard shortcut for the service in System Preferences.

Tags

Next

Dan Halliday Launches New Agency Swagger