vinniecent
10/11/2019 - 2:09 PM

My Hammerspoon configuration

My Hammerspoon configuration

hs.loadSpoon('KSheet')

hyper = {"cmd", "ctrl"}
shift_hyper = {"cmd", "ctrl", "alt"}


-- maximize the window
hs.hotkey.bind(hyper, "Down", function()
  local win = hs.window.focusedWindow()
  local f = win:frame()
  local screen = win:screen()
  local max = screen:frame()

  f.x = max.x
  f.y = max.y
  f.w = max.w
  f.h = max.h
  win:setFrame(f)
end)


-- move the focused window to the monitor on the right
hs.hotkey.bind(hyper, "right", function()
  local win = hs.window.focusedWindow()
  win:moveOneScreenEast()
end)


-- move the focused window to the monitor on the left
hs.hotkey.bind(hyper, "left", function()
  local win = hs.window.focusedWindow()
  win:moveOneScreenWest()
end)


ksheet_shown = false
-- show application shortcuts cheatsheet
hs.hotkey.bind(hyper, "/", function()

	if ksheet_shown then
		spoon.KSheet:hide()
		ksheet_shown = false
	else
		spoon.KSheet:show()
		ksheet_shown = true
	end

end)


function moveToScreen(screenPos)
  window = hs.window.focusedWindow()
  screen = hs.screen.find({x=screenPos, y=0})
  window:moveToScreen(screen)
end




-- reload config on change
function reloadConfig(files)
    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
myWatcher = hs.pathwatcher.new(os.getenv("HOME") .. "/.hammerspoon/", reloadConfig):start()
hs.alert.show("Config loaded")


-- quick jump to important applications
hs.hotkey.bind(hyper, 'pad0', function () hs.application.launchOrFocus("iTerm") end)
hs.hotkey.bind(hyper, 'pad1', function () hs.application.launchOrFocus("Slack") end)
hs.hotkey.bind(hyper, 'pad2', function () hs.application.launchOrFocus("Spotify") end)
hs.hotkey.bind(hyper, 'pad3', function () hs.application.launchOrFocus("WhatsApp") end)

hs.hotkey.bind(hyper, 'pad4', function () hs.application.launchOrFocus("Webstorm") end)
hs.hotkey.bind(hyper, 'pad5', function () hs.application.launchOrFocus("Tower") end)
hs.hotkey.bind(hyper, 'pad6', function () hs.application.launchOrFocus("Path Finder") end)

hs.hotkey.bind(hyper, 'pad7', function () hs.application.launchOrFocus("Google Chrome") end)
hs.hotkey.bind(hyper, 'pad8', function () hs.application.launchOrFocus("Microsoft Outlook") end)

local leftScreen = hs.screen'-1,0'
local centerScreen = hs.screen'0,0'
local rightScreen = hs.screen'1,0'

local developLayout = {
 {"Spotify", nil, rightScreen, hs.layout.maximized, nil, nil},
 {"iTerm2", nil, rightScreen, hs.layout.maximized, nil, nil},
 {"Slack", nil, rightScreen, hs.layout.maximized, nil, nil},
 {"WhatsApp", nil, rightScreen, hs.layout.maximized, nil, nil},

 {"WebStorm", nil, centerScreen, hs.layout.maximized, nil, nil},

 {"Google Chrome", nil, leftScreen, hs.layout.maximized, nil, nil},
 {"Tower", nil, leftScreen, hs.layout.maximized, nil, nil},
}



hs.hotkey.bind(hyper, 'f19', function ()
	hs.layout.apply(developLayout)
	hs.alert.show("Development layout")
end)


hs.hotkey.bind(shift_hyper, 'f19', function ()
	hs.application.launchOrFocus("Spotify")
	hs.application.launchOrFocus("iTerm")
	hs.application.launchOrFocus("Slack")
	hs.application.launchOrFocus("WhatsApp")

	hs.application.launchOrFocus("Webstorm")

	hs.application.launchOrFocus("Google Chrome")
	hs.application.launchOrFocus("Tower")

	hs.layout.apply(developLayout)
	hs.alert.show("Development layout")
end)


function chrome_active_tab_with_name(name, url)
    return function()
        hs.osascript.javascript([[
            // below is javascript code
            var chrome = Application('Google Chrome');
            chrome.activate();
            var wins = chrome.windows;

            // loop tabs to find a web page with a title of <name>
            var tabFound = false;

            for (var i = 0; i < wins.length; i++) {
                var win = wins.at(i);
                var tabs = win.tabs;
                for (var j = 0; j < tabs.length; j++) {
                var tab = tabs.at(j);
                tab.title(); j;
                if (tab.title().indexOf(']] .. name .. [[') > -1) {
                        win.activeTabIndex = j + 1;
                        tabFound = true;
                    }
                }

            }

            if (!tabFound) {
              newTab = chrome.Tab();
              chrome.windows[0].tabs.push(newTab);
              newTab.url = ']] .. url .. [[';
            }

            // end of javascript
        ]])
    end
end

hs.hotkey.bind(shift_hyper, 'pad1', chrome_active_tab_with_name("Google Agenda", "https://calendar.google.com/calendar/r"))
hs.hotkey.bind(shift_hyper, 'pad2', chrome_active_tab_with_name("GitLab", "https://gitlab.beequip.nl/"))
hs.hotkey.bind(shift_hyper, 'pad3', chrome_active_tab_with_name("Hacker News", "https://news.ycombinator.com/"))