Skip to content

Instantly share code, notes, and snippets.

@dropmeaword
Last active November 14, 2021 15:58
Show Gist options
  • Save dropmeaword/cbf03189897733cbef6267dfbf36861d to your computer and use it in GitHub Desktop.
Save dropmeaword/cbf03189897733cbef6267dfbf36861d 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