Created
October 27, 2015 11:02
-
-
Save johnste/e22d1c2657f630c15271 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local utils = require 'utils' | |
hs.hotkey.bind(utils.hyper, "Left", utils.getScreen(function(win, frame, screen, screenFrame) | |
local margin = 10 | |
local myFrame = hs.fnutils.copy(frame) | |
myFrame.x = screenFrame.x + margin | |
myFrame.y = screenFrame.y + margin | |
myFrame.w = screenFrame.w/2 - margin*2 | |
myFrame.h = screenFrame.h - margin*2 | |
if utils.rectEquals(myFrame, frame) then | |
utils.throwNext(win, screen) | |
else | |
win:setFrame(myFrame) | |
end | |
end)) | |
hs.hotkey.bind(utils.hyper, "Right", utils.getScreen(function(win, frame, screen, screenFrame) | |
local margin = 10 | |
local myFrame = hs.fnutils.copy(frame) | |
myFrame.x = screenFrame.x + screenFrame.w/2 + margin | |
myFrame.y = screenFrame.y + margin | |
myFrame.w = screenFrame.w/2 - margin*2 | |
myFrame.h = screenFrame.h - margin*2 | |
if utils.rectEquals(myFrame, frame) then | |
utils.throwNext(win, screen) | |
else | |
win:setFrame(myFrame) | |
end | |
end)) | |
hs.hotkey.bind(utils.hyper, "Up", utils.getScreen(function(win, frame, screen, screenFrame) | |
local margin = 10 | |
local myFrame = hs.fnutils.copy(frame) | |
myFrame.x = screenFrame.x + margin | |
myFrame.y = screenFrame.y + margin | |
myFrame.w = screenFrame.w - margin*2 | |
myFrame.h = screenFrame.h - margin*2 | |
if utils.rectEquals(myFrame, frame) then | |
utils.throwNext(win, screen) | |
else | |
win:setFrame(myFrame) | |
end | |
end)) | |
hs.hotkey.bind(utils.hyper, "1", function() | |
hs.notify.new({ | |
title="Spotify", | |
informativeText=hs.spotify.getCurrentArtist() .. " - \"" .. hs.spotify.getCurrentTrack() .. "\"" | |
}):send() | |
end) | |
local pasteTimer | |
hs.hotkey.bind(utils.hyper, "v", | |
function() | |
pasteTimer = hs.timer.secondsSinceEpoch() | |
hs.eventtap.keyStrokes(utils.makeString(10)) | |
end, | |
function() | |
local diff = hs.timer.secondsSinceEpoch() - pasteTimer | |
if diff > 0.2 then | |
hs.eventtap.keyStrokes("@example.com") | |
end | |
end | |
) | |
apps = { | |
chrome = {key="A", bundleId = 'com.google.Chrome'}, | |
chromeCanary = {key="X", bundleId = 'com.google.Chrome.canary'}, | |
finder = {key="F", name="Finder"}, | |
helium = {key="H", name="Helium"}, | |
hipchat = {key="Q", name="HipChat"}, | |
iTerm = {key="D", name="iTerm"}, | |
slack = {key="W", name="Slack"}, | |
spotify = {key="E", name="Spotify"}, | |
sublimeText = {key="S", name="Sublime Text"}, | |
twitter = {key="Z", name="Twitter"}, | |
} | |
for key, val in pairs(apps) do -- Table iteration. | |
local focusFn | |
if val.bundleId then | |
focusFn = utils.focusAppByBundleId(val.bundleId) | |
else | |
focusFn = utils.focusApp(val.name) | |
end | |
hs.hotkey.bind(utils.hyper, val.key, focusFn) | |
end | |
hs.alert.show("Config loaded") | |
hs.window.animationDuration = 0.05 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local utils = {} | |
utils.hyper = { "cmd", "alt", "shift", "ctrl" } | |
function utils.getScreen(callback) | |
return function() | |
local win = hs.window.focusedWindow() | |
local frame = win:frame() | |
local screen = win:screen() | |
local screenFrame = screen:frame() | |
if win then | |
callback(win, frame, screen, screenFrame) | |
end | |
end | |
end | |
function utils.focusApp(name) | |
return function() | |
return hs.application.launchOrFocus(name) | |
end | |
end | |
function utils.focusAppByBundleId(bundleId) | |
return function() | |
return hs.application.launchOrFocusByBundleID(bundleId) | |
end | |
end | |
function utils.rectEquals(frame1, frame2) | |
return ( | |
frame1.x == frame2.x and | |
frame1.y == frame2.y and | |
frame1.width == frame2.width and | |
frame1.height == frame2.height | |
) | |
end | |
function utils.throwNext(win, screen) | |
local toScreen = screen:toEast() | |
if not toScreen then | |
toScreen = screen:toWest() | |
end | |
if toScreen then | |
win:moveToScreen(toScreen) | |
else | |
hs.notify.new({ | |
title="Hammerspoon", | |
informativeText="No screen to east or west" | |
}):send() | |
end | |
end | |
function utils.makeString(length) | |
length = length or 1 | |
if length < 1 then return nil end | |
local array = {} | |
for i = 1, length do | |
local rand = math.random(48, 122) | |
while (rand >= 57 and rand < 65) or (rand >= 91 and rand < 97) do | |
rand = math.random(48, 122) | |
end | |
array[i] = string.char(rand) | |
end | |
return table.concat(array) | |
end | |
function utils.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 | |
hs.pathwatcher.new(os.getenv("HOME") .. "/.hammerspoon/", utils.reloadConfig):start() | |
return utils |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment