Skip to content

Instantly share code, notes, and snippets.

@technovangelist
Created August 21, 2017 12:53
Show Gist options
  • Save technovangelist/eaa55d8d86740a23d6550cb37d9219d5 to your computer and use it in GitHub Desktop.
Save technovangelist/eaa55d8d86740a23d6550cb37d9219d5 to your computer and use it in GitHub Desktop.
current (2017-08-21) hammerspoon config
local appfinder = require "hs.appfinder"
local app = require "hs.application"
local alive1 = hs.image.imageFromPath("~/bin/alive1.png")
local alive2 = hs.image.imageFromPath("~/bin/alive2.png")
alive = hs.menubar.new()
if alive then
alive:setIcon(alive2,false)
end
function updateAlive()
if alive:icon() == alive2 then
alive:setIcon(alive1,false)
else
alive:setIcon(alive2,false)
-- if alive:title() == "-" then
-- alive:setTitle("--")
-- alive:setIcon(alive1, false)
-- else
-- hs.timer.doAfter(.1,alive:setIcon(alive2,false))
-- alive:setTitle("-")
end
end
function reloadConfig(files)
updateAlive()
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
function keyValueExists(tbl, key, value)
for k,v in pairs(tbl) do
if value == v[key] then
return true
end
end
return false
end
function setUSLayout( )
hs.execute("keyboardSwitcher select \"U.S.\"", true)
end
function setWorkmanLayout( )
hs.execute("keyboardSwitcher select \"Workman-Dead\"", true)
end
function setKeyboard()
local devs = hs.usb.attachedDevices()
if keyValueExists(devs, "productName", "ErgoDox EZ") then
setUSLayout()
else
setWorkmanLayout()
end
end
function sleepCallback( eventType )
updateAlive()
hs.timer.doAfter(1,hs.reload)
end
function usbDeviceCallback(data)
updateAlive()
if string.match(data["productName"], "ErgoDox EZ") then
setKeyboard()
end
end
function launchRDM()
app.open("/Applications/RDM.app")
end
function screenCallback()
updateAlive()
rdm = app.find("RDM")
if rdm == nil then
launchRDM()
else
rdm:kill()
hs.timer.doAfter(1,launchRDM)
end
end
function powerCallback( )
updateAlive()
if hs.battery.isCharging() or hs.battery.isCharged() then
print('charging')
hs.brightness.set(95)
powerSwitch(lowPriorityApps, true)
powerSwitch(hiPriorityApps, true)
else
if hs.battery.percentage() < 50 then
hs.brightness.set(80)
powerSwitch(lowPriorityApps, false)
powerSwitch(hiPriorityApps, false)
elseif hs.battery.percentage() < 90 then
-- hs.alert.show('below 90')
hs.brightness.set(90)
powerSwitch( lowPriorityApps, false )
powerSwitch( hiPriorityApps, true )
else
hs.brightness.set(100)
powerSwitch( lowPriorityApps, true )
powerSwitch( hiPriorityApps, true )
end
end
end
function powerSwitch( appTable, powerOn )
updateAlive()
for i, application in ipairs(appTable) do
thisApp = app.get(application)
if thisApp == nil then -- couldn't find the app
-- print('Cant find ' .. application)
if powerOn == true then
-- print('Launched ' .. application)
app.open(application)
end
else -- the app is running
-- print(thisApp:name() .. ' is running')
if powerOn == false then
-- print('Killed ' .. thisApp:name())
thisApp:kill()
end
end
end
end
lowPriorityApps = {
"Google Drive",
"Dropbox",
"Amazon Drive",
"Creative Cloud",
"Arq Agent"
}
hiPriorityApps = {
"Keyboard Maestro Engine"
}
fcpSupportApps = {
"CTRL+Console",
"mInstaller"
}
function appCallback(appname, eventtype, theapp)
updateAlive()
-- hs.alert.show(appname .. eventtype)
if appname == 'Final Cut Pro'then
if eventtype == hs.application.watcher.launched then
print('started fcp')
powerSwitch(fcpSupportApps, true)
elseif eventtype == hs.application.watcher.terminated then
print('killed fcp')
powerSwitch(fcpSupportApps, false)
end
end
end
hs.caffeinate.watcher.new(sleepCallback):start()
hs.pathwatcher.new(os.getenv('HOME') .. '/.hammerspoon/', reloadConfig):start()
hs.alert.show('Config loaded')
hs.battery.watcher.new(powerCallback):start()
-- hs.audiodevice.watcher.setCallback(audioCallback):start()
hs.usb.watcher.new(usbDeviceCallback):start()
hs.screen.watcher.new(screenCallback):start()
hs.application.watcher.new(appCallback):start()
-- hs.hotkey.bind({"cmd", "ctrl", "alt", "shift"}, "Q", screenCallback)
setKeyboard()
screenCallback()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment