hammerspoon config
This commit is contained in:
parent
77d18a6e1f
commit
a08934a1e3
2 changed files with 200 additions and 0 deletions
92
.hammerspoon/Spoons/darkmode.lua
Normal file
92
.hammerspoon/Spoons/darkmode.lua
Normal file
|
@ -0,0 +1,92 @@
|
|||
-- --------------------------------------------------------------------------
|
||||
--
|
||||
-- Implementation of a dark mode library for detecting and setting dark
|
||||
-- mode in MacOS.
|
||||
--
|
||||
-- --------------------------------------------------------------------------
|
||||
--
|
||||
-- Example:
|
||||
--
|
||||
-- dm = require "darkmode"
|
||||
-- dm.addHandler(function(dm2) print('darkmode changed:',dm2) end)
|
||||
-- print('darkmode:',dm.getDarkMode())
|
||||
-- dm.setDarkMode(not dm.getDarkMode())
|
||||
-- print('darkmode:',dm.getDarkMode())
|
||||
-- dm.setDarkMode(not dm.getDarkMode())
|
||||
--
|
||||
-- --------------------------------------------------------------------------
|
||||
|
||||
-- --------------------------------------------------------------------------
|
||||
-- internal Data which should not be garbage collected
|
||||
-- --------------------------------------------------------------------------
|
||||
|
||||
local internalData = {
|
||||
darkmode = false,
|
||||
watcher = nil,
|
||||
handler = {}
|
||||
}
|
||||
|
||||
-- --------------------------------------------------------------------------
|
||||
-- General functions
|
||||
-- --------------------------------------------------------------------------
|
||||
|
||||
local function getDarkModeFromSystem()
|
||||
-- local _, darkmode = hs.osascript.applescript('tell application "System Events"\nreturn dark mode of appearance preferences\nend tell')
|
||||
local _, darkmode = hs.osascript.javascript("Application('System Events').appearancePreferences.darkMode.get()")
|
||||
return darkmode
|
||||
end
|
||||
|
||||
local function getDarkMode()
|
||||
return internalData.darkmode
|
||||
end
|
||||
|
||||
local function setDarkMode(state)
|
||||
hs.osascript.javascript(string.format("Application('System Events').appearancePreferences.darkMode.set(%s)",state))
|
||||
end
|
||||
|
||||
local function addHandler(fn)
|
||||
-- add it here...
|
||||
internalData.handler[#internalData.handler+1] = fn
|
||||
end
|
||||
|
||||
-- --------------------------------------------------------------------------
|
||||
-- Internal functions
|
||||
-- --------------------------------------------------------------------------
|
||||
|
||||
local function initialize()
|
||||
internalData.darkmode = getDarkModeFromSystem()
|
||||
end
|
||||
|
||||
local function initializeWatcher()
|
||||
-- exit if already watching
|
||||
if internalData.watcher ~= nil then return end
|
||||
|
||||
internalData.watcher = hs.distributednotifications.new(function(name, object, userInfo)
|
||||
local hasDarkMode = getDarkModeFromSystem()
|
||||
if hasDarkMode ~= internalData.darkmode then
|
||||
internalData.darkmode = hasDarkMode
|
||||
-- execute each handler with the darkmode as parameter (may change in future)
|
||||
for index, fn in ipairs(internalData.handler) do
|
||||
fn(hasDarkMode)
|
||||
end
|
||||
end
|
||||
end,'AppleInterfaceThemeChangedNotification')
|
||||
|
||||
internalData.watcher:start()
|
||||
end
|
||||
|
||||
-- --------------------------------------------------------------------------
|
||||
-- Initialization
|
||||
-- --------------------------------------------------------------------------
|
||||
|
||||
initialize()
|
||||
initializeWatcher()
|
||||
|
||||
local module = {
|
||||
_ = internalData,
|
||||
setDarkMode = setDarkMode,
|
||||
getDarkMode = getDarkMode,
|
||||
addHandler = addHandler
|
||||
}
|
||||
|
||||
return module
|
108
.hammerspoon/init.lua
Normal file
108
.hammerspoon/init.lua
Normal file
|
@ -0,0 +1,108 @@
|
|||
local darkmode = require("Spoons/darkmode")
|
||||
|
||||
-- Variables
|
||||
local hyperKey = {"alt", "shift", "ctrl", "cmd"}
|
||||
-- termApplication = "Alacritty"
|
||||
local termApplication = "WezTerm"
|
||||
|
||||
-- Highlight the currently focused window
|
||||
hs.window.highlight.start()
|
||||
|
||||
-- Enable spotlight to search applications by name
|
||||
hs.application.enableSpotlightForNameSearches(true)
|
||||
|
||||
-- Reload configuration automatically
|
||||
local function reloadConfig(files)
|
||||
local doReload = false
|
||||
for _, file in pairs(files) do
|
||||
if file:sub(-4) == ".lua" then
|
||||
doReload = true
|
||||
end
|
||||
end
|
||||
if doReload then
|
||||
hs.reload()
|
||||
end
|
||||
end
|
||||
|
||||
local myWatcher = hs.pathwatcher.new(os.getenv("HOME") .. "/.hammerspoon/", reloadConfig):start()
|
||||
hs.alert.show("hs.config updated")
|
||||
|
||||
-- Allow a scratchpad-like behavior for a certain app with a key combination
|
||||
local function scratchpadAppWithCombination(modifier, key, appName)
|
||||
hs.hotkey.bind(modifier, key, function()
|
||||
local app = hs.application.get(appName)
|
||||
if app then
|
||||
space = hs.spaces.activeSpaceOnScreen(hs.screen.mainScreen())
|
||||
if not app:mainWindow() then
|
||||
app:selectMenuItem({appName, "New OS window"})
|
||||
elseif app:isFrontmost() then
|
||||
app:hide()
|
||||
else
|
||||
app:unhide()
|
||||
hs.spaces.moveWindowToSpace(app:focusedWindow(), space)
|
||||
app:activate()
|
||||
end
|
||||
else
|
||||
hs.application.launchOrFocus(appName)
|
||||
end
|
||||
end)
|
||||
end
|
||||
|
||||
-- Focus a window with a combination
|
||||
LastFocusedWindow = nil
|
||||
local function focusAppWithCombination(modifier, key, appName)
|
||||
hs.hotkey.bind(modifier, key, function()
|
||||
local app = hs.application.get(appName)
|
||||
if app then
|
||||
if app:focusedWindow() == hs.window.focusedWindow() then
|
||||
LastFocusedWindow:focus()
|
||||
LastFocusedWindow = app:mainWindow()
|
||||
else
|
||||
LastFocusedWindow = hs.window.focusedWindow()
|
||||
-- hs.application.launchOrFocus(appName)
|
||||
app:mainWindow():focus()
|
||||
end
|
||||
else
|
||||
hs.alert.show("iTerm not running")
|
||||
end
|
||||
end)
|
||||
end
|
||||
|
||||
-- Setup
|
||||
scratchpadAppWithCombination({"cmd", "shift"}, "-", "Obsidian")
|
||||
-- scratchpadAppWithCombination(hyperKey, "-", "Notion")
|
||||
scratchpadAppWithCombination({"ctrl"}, "return", termApplication)
|
||||
|
||||
-- Dark mode toggles
|
||||
local function toggleMattermostTheme(dark)
|
||||
print('theme changed to dark:', dark)
|
||||
end
|
||||
darkmode.addHandler(toggleMattermostTheme)
|
||||
|
||||
-- Test
|
||||
|
||||
-- function safariMouseBack()
|
||||
|
||||
-- end
|
||||
|
||||
-- termBinds = {}
|
||||
|
||||
-- function enableBinds()
|
||||
-- -- hs.console.printStyledtext("term focused")
|
||||
-- for k, v in pairs(termBinds) do
|
||||
-- v:enable()
|
||||
-- end
|
||||
-- end
|
||||
|
||||
-- function disableBinds()
|
||||
-- -- hs.console.printStyledtext("term unfocused")
|
||||
-- for k, v in pairs(termBinds) do
|
||||
-- v:disable()
|
||||
-- end
|
||||
-- end
|
||||
|
||||
-- local wf = hs.window.filter
|
||||
|
||||
-- wf_safari = wf.new {'Safari'}
|
||||
-- wf_safari:subscribe(wf.windowFocused, enableBinds)
|
||||
-- wf_safari:subscribe(wf.windowUnfocused, disableBinds)
|
Loading…
Add table
Add a link
Reference in a new issue