Making MacOS Even Greater

By mcs94, Tue 16 April 2019, modified Tue 16 April 2019, in category Macos

automation, macos

Table of Contents

Making MacOS Even Greater

In the last post we explored a few things we can do with Hammerspoon to improve the experience of using MacOS.

This post will explore a few more cool things we can do with Hammerspoon.

Window Management

First up, window management. There are a few Spoons we can use for this, but I opted for Miro's Window Management. This one really does feel like magic.

Window Management

Once you've grabbed the spoon and dumped it into ~/.hammerspoon/Spoons/ you may call the Spoon in your init.lua file as below:

hs.loadSpoon("MiroWindowsManager")

hs.window.animationDuration = 0.1
spoon.MiroWindowsManager:bindHotkeys({
    up = {hyper, "up"},
    right = {hyper, "right"},
    down = {hyper, "down"},
    left = {hyper, "left"},
    fullscreen = {hyper, "f"}
})

Boiled Egg Timer

This is pretty cool. Hammerspoon functions may be triggered by using URLs beginning with hammerspoon://.

For instance, we have a function which notifies us after 6 and a half minutes:

function startEggTimer()
    hs.timer.doAfter(6.5 * 60, function ()
        hs.notify.new({
            title="Egg Timer!",
            informativeText="Your Egg is ready!"
        }):send()
    end)
    hs.alert(" Egg timer started! ")
    end

We can bind this function to a URL using this line:

hs.urlevent.bind("eggtimer", startEggTimer)
-- Visit this url to trigger the timer:
-- hammerspoon://eggtimer

Integrating with Home Assistant

Home Assistant is a open source home automation platform. I have a basic setup for controlling a few lights.

With a little work we can detect the state of a mac:

data = '{"entity_id": "switch.macbook_power"}'

headers = {}
headers["x-ha-access"] = "your_password"

local sleepWatcher = hs.caffeinate.watcher.new(function (eventType)
    if (eventType == hs.caffeinate.watcher.systemDidWake) then
        hs.notify.show("Waking up", "", "")
        hs.http.post("https://192.168.0.10:8123/api/services/switch/turn_on", data, headers)
    elseif (eventType == hs.caffeinate.watcher.systemWillSleep) then
            hs.notify.show("Ciao", "", "")
            hs.http.post("https://192.168.0.10:8123/api/services/switch/turn_off", data, headers)
    end
end)
sleepWatcher:start()

You'll need a corresponding switch in your home assistant configuration.

Defeating Paste Blockers

Okay, this one isn't really for defeating paste blockers (although it'll work fine for those). It's actually for dealing with the pain of pasting text into Confluence.

hs.hotkey.bind(hyper, "V", function()
    hs.eventtap.keyStrokes(hs.pasteboard.getContents())
end)

Text Expander

This Github Gist by Amit Kotlovski will get you a text expander. Good enough for my needs.