Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save confluencepoint/f43513ef78fcb71fffc2ccc3f6d459a2 to your computer and use it in GitHub Desktop.

Select an option

Save confluencepoint/f43513ef78fcb71fffc2ccc3f6d459a2 to your computer and use it in GitHub Desktop.
This tiny script goes into your Hammerspoon configuration and it hooks on the lid open/close event
function sleepWatch(eventType)
local action = "unknown"
if (eventType == hs.caffeinate.watcher.systemWillSleep) then
hs.alert.show("Going to sleep!")
host = hs.host.localizedName()
action = "sleep"
elseif (eventType == hs.caffeinate.watcher.systemDidWake) then
hs.alert.show("Waking up!")
action = "awake"
end
-- write current state of laptop lid to file
if (action ~= "unknown") then
filename = string.format("%s/mylaptoplid.txt", os.getenv( "HOME" ))
local f = assert(io.open(filename, "a+"))
local tstamp = os.date("%d/%m/%y %H:%M:%S")
local s = string.format("%s %s\n", tstamp, action)
f:write( s )
f:flush()
f:close()
end
end
local sleepWatcher = hs.caffeinate.watcher.new(sleepWatch)
sleepWatcher:start()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment