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.
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.
At the top of the rightmost panel, change text to no input in the first dropdown and any application to Google Chrome.app in the second dropdown.
Drag the Run AppleScript action from the center panel to the rightmost panel.
(* 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 "https://console.aws.amazon.com/cloud9/ide" then close currentTab end tell
Save the service as something memorable. I chose Cloud9PreventClose for this example.
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.
- Click the + button to add the first shortcut.
- Select Google Chrome.app as the application and type “Close Tab” for the menu title. Choose a keyboard shortcut other than ⌘W (I chose ^⌘W).
- Click the + button again to add the second shortcut.
- Select Google Chrome.app as the application and type the service name (in this case “Cloud9PreventClose”) for the menu title. Use ⌘W as the keyboard shortcut.
If you switch back to Chrome, you should see your custom key command next to File > Close and ⌘W next to Chrome > Services > Cloud9PreventClose.