diff --git a/.hammerspoon/Spoons/Quitter.spoon/init.lua b/.hammerspoon/Spoons/Quitter.spoon/init.lua index a315ba8..43db7b8 100644 --- a/.hammerspoon/Spoons/Quitter.spoon/init.lua +++ b/.hammerspoon/Spoons/Quitter.spoon/init.lua @@ -15,16 +15,13 @@ obj.license = "MIT - https://opensource.org/licenses/MIT" obj.logger = hs.logger.new("quitter", "debug") obj.lastFocused = {} obj.windowFocusedFn = function(window, appName) - local bundleID = window:application():bundleID() - if not obj.quitAppsAfter[bundleID] then return end - obj.lastFocused[bundleID] = os.time() + local name = window:application():name() + obj.lastFocused[name] = os.time() end --- Quitter.quitAppsAfter --- Variable ---- Table of application bundle IDs to quit when unused. Use `mdls -name ---- --kMDItemCFBundleIdentifier -r /path/to/app` to find unknown bundle IDs. - +--- Table of applications to quit when unused. obj.quitAppsAfter = {} --- Quitter:start() @@ -43,7 +40,11 @@ function obj:start() end) -- Set last focused time for relevant apps - self.windowFilter = hs.window.filter.default:subscribe(hs.window.filter.windowFocused, self.windowFocusedFn) + self.windowFilter = hs.window.filter.new(false) + for app, _ in pairs(self.quitAppsAfter) do + self.windowFilter:allowApp(app) + end + self.windowFilter:subscribe(hs.window.filter.windowFocused, self.windowFocusedFn) self.timer = hs.timer.doEvery(60, function() self:reap() @@ -60,34 +61,36 @@ end --- * None function obj:stop() self.watcher:stop() - self.windowFilter:unsubscribe(hs.window.filter.windowFocused, self.windowFocusedFn) + self.windowFilter:unsubscribe(hs.window.filter.windowFocused) self.timer:stop() end function obj:reset() hs.fnutils.ieach(hs.application.runningApplications(), function(app) - local bundleID = app:bundleID() + local name = app:name() - local duration = self.quitAppsAfter[bundleID] + local duration = self.quitAppsAfter[name] if not duration then return false end - self.lastFocused[bundleID] = os.time() + self.lastFocused[name] = os.time() end) end function obj:reap() self.logger.d("reaping") local appsToQuit = hs.fnutils.ifilter(hs.application.runningApplications(), function(app) + local appName = app:name() + -- Don't quit the currently focused app if app:isFrontmost() then return false end - local duration = self.quitAppsAfter[app:bundleID()] + local duration = self.quitAppsAfter[appName] if not duration then return false end - local lastFocused = self.lastFocused[app:bundleID()] + local lastFocused = self.lastFocused[appName] if not lastFocused then return false end - self.logger.d("app: " .. app:name() .. " last focused at " .. lastFocused) + self.logger.d("app: " .. appName .. " last focused at " .. lastFocused) return (os.time() - lastFocused) > duration end) diff --git a/.hammerspoon/init.fnl b/.hammerspoon/init.fnl index 29a5a90..5c1a053 100644 --- a/.hammerspoon/init.fnl +++ b/.hammerspoon/init.fnl @@ -35,9 +35,21 @@ (hs.urlevent.openURLWithBundle fullURL "us.zoom.xos") (hs.urlevent.openURLWithBundle fullURL "org.mozilla.firefoxdeveloperedition"))))) +;; Spoons + (hs.loadSpoon "ReloadConfiguration") (spoon.ReloadConfiguration:start) +(hs.loadSpoon "Quitter") +(set spoon.Quitter.quitAppsAfter { + :Discord 300 + :MailMate 300 + :Messages 300 + :Slack 300 + :Twitterrific 300 +}) +(spoon.Quitter:start) + (let [n (hs.notify.new { :title "Hammerspoon" :informativeText "Config loaded" diff --git a/.hammerspoon/init.lua b/.hammerspoon/init.lua index 26dd6c5..19b0bbf 100644 --- a/.hammerspoon/init.lua +++ b/.hammerspoon/init.lua @@ -1,3 +1,4 @@ +-- Handle mismatch between homebrew's version of Lua and hammerspoon's version local lVer = _VERSION:match("Lua (.+)$") -- local luarocks = hs.execute("which luarocks"):gsub("\n", "") local luarocks = "/usr/local/bin/luarocks" @@ -14,16 +15,3 @@ fennel = require "fennel" table.insert(package.loaders or package.searchers, fennel.searcher) fennel.dofile("init.fnl", { allowedGlobals = false }) - --- Spoons -- - -hs.loadSpoon("Quitter") -spoon.Quitter.quitAppsAfter = { - -- mdls -name kMDItemCFBundleIdentifier -r /path/to/app - ["com.apple.iChat"] = 600, - ["com.freron.MailMate"] = 600, - ["com.kapeli.dashdoc"] = 600, - ["com.reederapp.macOS"] = 600, - ["com.tinyspeck.slackmacgap"] = 600, -} --- spoon.Quitter:start()