Fighting muscle memory is an uphill battle, especially when using a web app that replaces a native app.

After accidentally closing my active Cloud9 tab one too many times (while intending to close the active document) I decided to look for a way to save me from myself.

Luckily, it turned out to be surprisingly easy with macOS.

Automator screenshot 1

The first step is to launch the Automator app that came with your Mac. Select Service when prompted to choose a type for your document.

Automator screenshot 2

At the top of the rightmost panel, change text to no input in the first dropdown and any application to Google in the second dropdown.

Drag the Run AppleScript action from the center panel to the rightmost panel.

Replace (* Your script goes here *) with the following:

tell application "Google Chrome"
        set currentTab to active tab of first window
        set currentURL to URL of currentTab
        if currentURL does not start with "" then close currentTab
end tell

Save the service as something memorable. I chose Cloud9PreventClose for this example.

Automator screenshot 3

The next step is to launch System Preferences and click on the Keyboard icon. Select the Shortcuts tab and choose App Shortcuts from the list on the left.

You’ll need to create 2 shortcuts; one to override the “Close Tab” shortcut and one to trigger the service.

  1. Click the + button to add the first shortcut.
  • Select Google as the application and type “Close Tab” for the menu title. Choose a keyboard shortcut other than ⌘W (I chose ^⌘W).
  1. Click the + button again to add the second shortcut.
  • Select Google as the application and type the service name (in this case “Cloud9PreventClose”) for the menu title. Use ⌘W as the keyboard shortcut.
System Preferences screenshot

If you switch back to Chrome, you should see your custom key command next to File > Close and ⌘W next to Chrome > Services > Cloud9PreventClose.