Automate your macOS with Hammerspoon

I've been using macOS for about 8 years and as a dual monitor user, there was always a very annoying problem which I could never find a perfect solution for. Every time I connected or disconnected my second monitor from my MacBook, macOS messed up the window positions.

Then I discovered Hammerspoon and was not only able to fix this problem but also able to automate a lot of cool things and I was also able to replace HardwareGrowler with it.

My configuration is available at GitHub and provides the following features:

Hardware event notifications

It's always good to know what is going on with your computer. Using this script you will get detailed notifications whenever there is a change in one of the monitored system components. Currently, the following events are supported:

  • USB connections
  • Battery state
  • WiFi connections
  • Volume mount / unmount
Window management

You can define a layout for a specific application window. Whenever this application is started the window will be automatically moved and resized according to the configuration.

There are two different layout configurations. dualLayout will be used if a second monitor is connected. singleLayout will be used if only one monitor is connected.

The layout will be automatically changed when a monitor is connected or disconnected.

Misc

Bluetooth will be turned off when the second monitor is unplugged (blueutil must be installed). You can configure this feature with the manageBluetooth setting.

As a replacement for growlnotify you can use the following command to display a notification:

open 'hammerspoon://notify?title=This is the title&subtitle=A subtitle&info=The info text'

To open a new browser window in a special developer mode, use this command:

open 'hammerspoon://devopen?url=http://localhost:8080'

This configuration is work in progress and there will be more features in the future.

Installation

git clone https://github.com/code2k/.hammerspoon.git ~/.hammerspoon
cd ~/.hammerspoon
cp example-config.lua config.lua

Then edit config.lua as needed.