The Menubar Widget
A proper on/off switch in your menubar.
Having to edit a config file every time you want to toggle the jiggler is no good. Let's add a menubar icon that cycles through three states when you click it:
| Icon | State |
|---|---|
| 💤 | Off — display will sleep normally |
| ☕ | Display awake — screen won't sleep, no mouse movement |
| ☕🐭 | Display awake + jiggler active |
The two-level design is deliberate. Sometimes you just want the screen to stay on (watching a video, reading something long). Sometimes you need the full jiggle — for tools that specifically watch for mouse input. Click to cycle, click again to go back.
Add this to your init.lua, below the jiggler code from the previous page:
-- ── Caffeinate + Jiggler Menubar ─────────────────────────────────────── ───────
-- Click cycles: 💤 off → ☕ display awake → ☕🐭 display awake + jiggler
local caffeine = hs.menubar.new()
local caffeineState = 0 -- 0: off, 1: display awake, 2: display awake + jiggler
local function refreshCaffeineIcon()
if caffeineState == 2 then
caffeine:setTitle("☕🐭")
elseif caffeineState == 1 then
caffeine:setTitle("☕")
else
caffeine:setTitle("💤")
end
end
local function cycleCaffeine()
if caffeineState == 0 then
hs.caffeinate.set("displayIdle", true)
caffeineState = 1
elseif caffeineState == 1 then
startJiggler()
caffeineState = 2
else
stopJiggler()
caffeineState = 0
end
refreshCaffeineIcon()
hs.alert.show(
caffeineState == 2 and "Awake + Jiggler" or
caffeineState == 1 and "Awake" or "Off", 1)
end
-- Re-apply settings after the system wakes from sleep
local caffeineWatcher = hs.caffeinate.watcher.new(function(event)
if event == hs.caffeinate.watcher.systemDidWake then
if caffeineState == 1 or caffeineState == 2 then
hs.caffeinate.set("displayIdle", true)
end
if caffeineState == 2 then
startJiggler()
end
end
end):start()
caffeine:setClickCallback(cycleCaffeine)
refreshCaffeineIcon()
Save and reload Hammerspoon. You should see 💤 appear in your menubar. Click it once — it becomes ☕ and your display will stop sleeping. Click again — ☕🐭, jiggler is now active. Click a third time — back to 💤, everything off, normal sleep behaviour restored.
The caffeineWatcher block at the bottom handles a subtle edge case: if your Mac sleeps anyway (lid closed, for example) and then wakes up, the caffeinate state is reset by macOS. The watcher detects the wake event and re-applies your settings automatically so you don't come back to a sleeping display after opening the lid.