Caffeinate via Hammerspoon

This post is over a year old. Content may have changed, moved or may be wrong in it's entirety. The views that I express here probably have changed. If you feel this is in error, or something I should correct or clarify, please feel free to email me using the link below the post. Thank you.

caffeine = hs.menubar.new()

function setCaffeineDisplay(state)
	if state then
		caffeine:setTitle("☕️")
		caffeine:setTooltip("Device is being kept awake")
	else
		caffeine:setTitle("😴")
		caffeine:setTooltip("Device is allowed to sleep")
	end
end

function caffeineClicked(modifiers)
	if modifiers.ctrl then -- quit on ctrl-click
		caffeine:delete()
	else
		setCaffeineDisplay(hs.caffeinate.toggle('displayIdle'))
	end
end

if caffeine then
	caffeine:setClickCallback(caffeineClicked)
	setCaffeineDisplay(hs.caffeinate.get("displayIdle"))
end

A quick modification of the Hammerspoon example for making a caffeinate clone in Lua. It was actually easier to understand than I imagined, and it seems like there is so much more that it can do. I just need to get up to speed on Lua.

Respond via email.