Skip to content

Instantly share code, notes, and snippets.

@mutewinter
Created April 9, 2018 16:51
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mutewinter/62a01d75fac3b3d8539297a32aefbac3 to your computer and use it in GitHub Desktop.
Save mutewinter/62a01d75fac3b3d8539297a32aefbac3 to your computer and use it in GitHub Desktop.
Hammerspoon App Focus Alert
-- --------------------------
-- Alert When App Not Focused
-- --------------------------
local alertInSeconds = 60 * 20
local alertAppName = 'App You Want to Focus Here'
local alertTimer
local function stopTimerAlertTimer()
if alertTimer then
alertTimer.stop()
alertTimer = nil
end
end
local function notificationClicked()
hs.application.launchOrFocus(alertAppName)
end
local function sendNotification()
local notification = hs.notify.new(notificationClicked,
{
title = 'Time to focus ' .. alertAppName,
informativeText = 'Click to return',
soundName = 'Glass.aiff'
})
notification:send()
end
local function appFocused(appName, eventType)
if appName ~= alertAppName then
return
end
if eventType == hs.application.watcher.activated then
stopTimerAlertTimer()
hs.alert.show('Welcome back to ' .. alertAppName)
elseif eventType == hs.application.watcher.deactivated then
stopTimerAlertTimer()
alertTimer = hs.timer.delayed.new(alertInSeconds, sendNotification)
alertTimer:start()
hs.alert.show(alertInSeconds / 60 .. ' minute return timer started for ' .. alertAppName)
end
end
if alertAppName then
local watcher = hs.application.watcher.new(appFocused)
watcher:start()
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment