Created
July 23, 2016 13:57
-
-
Save johnkorzhuk/77c8e3ad5c5842b1e1f3fef777fb01e7 to your computer and use it in GitHub Desktop.
hammerspoon init file.
This file contains 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
-------------------------------------------------------------------------------- | |
-- rtoshiro - https://github.com/rtoshiro | |
-- You should see: http://www.hammerspoon.org/docs/index.html | |
-------------------------------------------------------------------------------- | |
------------------- | |
-- John's Config -- | |
------------------- | |
-------------------------------------------------------------------------------- | |
-- CONSTANTS | |
-------------------------------------------------------------------------------- | |
local cmd_alt = { "cmd", "alt" } | |
local cmd_alt_ctrl = { "cmd", "alt", "ctrl" } | |
-------------------------------------------------------------------------------- | |
-- CONFIGURATIONS | |
-------------------------------------------------------------------------------- | |
hs.window.animationDuration = 0 | |
-------------------------------------------------------------------------------- | |
-- LAYOUTS | |
-- SINTAX: | |
-- { | |
-- name = "App name" ou { "App name", "App name" } | |
-- func = function(index, win) | |
-- COMMANDS | |
-- end | |
-- }, | |
-- | |
-- It searches for application "name" and call "func" for each window object | |
-------------------------------------------------------------------------------- | |
local layouts = { | |
{ | |
name = { "Skype" }, | |
func = function(index, win) | |
local frame = win:frame() | |
frame.x = -1920 | |
frame.y = 862 | |
frame.w = 719 | |
frame.h = 1177 | |
win:setFrame(frame) | |
end | |
}, | |
{ | |
name = { "Slack" }, | |
func = function(index, win) | |
local frame = win:frame() | |
frame.x = -1605 | |
frame.y = 1015 | |
frame.w = 793 | |
frame.h = 1036 | |
win:setFrame(frame) | |
end | |
}, | |
{ | |
name = { "Messages" }, | |
func = function(index, win) | |
local frame = win:frame() | |
frame.x = -1046 | |
frame.y = 1235 | |
frame.w = 766 | |
frame.h = 808 | |
win:setFrame(frame) | |
end | |
}, | |
{ | |
name = { "WebStorm", | |
"Atom", | |
"Brackets", | |
"Sublime Text", | |
"IntelliJ IDEA", | |
"Visual Studio Code" }, | |
func = function(index, win) | |
local frame = win:frame() | |
local app = win:application() | |
if (app:title() == 'Atom') then | |
app:selectMenuItem({"View", "Reset Font Size"}) | |
for i=0, 1, 1 | |
do | |
app:selectMenuItem({"View", "Increase Font Size"}) | |
end | |
end | |
frame.x = 1477 | |
frame.y = 635 | |
frame.w = 2363 | |
frame.h = 1459 | |
win:setFrame(frame) | |
end | |
}, | |
{ | |
name = { "Spotify" }, | |
func = function(index, win) | |
local frame = win:frame() | |
frame.x = 0 | |
frame.y = 1442 | |
frame.w = 1245 | |
frame.h = 716 | |
win:setFrame(frame) | |
-- print(frame.x) | |
-- print(frame.y) | |
-- print(frame.w) | |
-- print(frame.h) | |
end | |
}, | |
{ | |
name = { "SourceTree" }, | |
func = function(index, win) | |
local frame = win:frame() | |
frame.x = 0 | |
frame.y = 692 | |
frame.w = 1473 | |
frame.h = 1127 | |
win:setFrame(frame) | |
end | |
}, | |
{ | |
name = { "Terminal" }, | |
func = function(index, win) | |
local frame = win:frame() | |
local app = win:application() | |
app:selectMenuItem({"View", "Default Font Size"}) -- Resets the frontsize | |
for i=0, 3, 1 -- of the terminal and | |
do -- increments it 3 times | |
app:selectMenuItem({"View", "Bigger"}) | |
end | |
frame.x = 469 | |
frame.y = 1261 | |
frame.w = 1005 | |
frame.h = 827 | |
win:setFrame(frame) | |
end | |
}, | |
{ | |
name = { "Finder" }, | |
func = function(index, win) | |
local frame = win:frame() | |
local app = win:application() | |
os.execute("sleep " .. tonumber(1.5)) | |
app:selectMenuItem({"Window", "Merge All Windows"}) | |
os.execute("sleep " .. tonumber(1)) | |
frame.x = 1543 | |
frame.y = 23 | |
frame.w = 1464 | |
frame.h = 613 | |
win:setFrame(frame) | |
end | |
}, | |
} | |
local closeAll = { | |
"Calculator", | |
"Notes", | |
"System Preferences" | |
} | |
-- | |
--local openAll = { | |
-- "Skype", | |
-- "Messages" | |
--} | |
function config() | |
local geom | |
-- Move window to top left of main monitor | |
-- used for terminal | |
hs.hotkey.bind(cmd_alt, "pad*", function() | |
local win = hs.window.focusedWindow() | |
win:move(hs.geometry.rect(3007, 23, 833, 613)) | |
end) | |
-- Split focus preset: Left window large | |
-- expand window to main monitor to split focus preset | |
hs.hotkey.bind(cmd_alt, "pad1", function() | |
local win = hs.window.focusedWindow() | |
win:move(hs.geometry.rect(54, 337, 2065, 1549)) | |
end) | |
-- Split focus preset: right window small | |
-- expand window to main monitor to split focus preset | |
hs.hotkey.bind(cmd_alt, "pad2", function() | |
local win = hs.window.focusedWindow() | |
win:move(hs.geometry.rect(2126, 337, 1600, 1549)) | |
end) | |
-- top left-large of main monitor | |
hs.hotkey.bind(cmd_alt_ctrl, "pad4", function() | |
local win = hs.window.focusedWindow() | |
win:move(hs.geometry.rect(23, 38, 2985, 1838)) | |
end) | |
-- top left of main monitor | |
hs.hotkey.bind(cmd_alt, "pad4", function() | |
local win = hs.window.focusedWindow() | |
win:move(hs.geometry.rect(0, 23, 1544, 1354)) | |
end) | |
-- move window top-right of laptop monitor | |
hs.hotkey.bind(cmd_alt, "pad7", function() | |
local win = hs.window.focusedWindow() | |
win:move(hs.geometry.rect(-1200, 767, 1200, 600)) | |
end) | |
-- move window & enlarge to almost fullscrren laptop monitor | |
hs.hotkey.bind(cmd_alt, "pad8", function() | |
local win = hs.window.focusedWindow() | |
win:move(hs.geometry.rect(-1804, 767, 1804, 1041)) | |
end) | |
-- expand focused screen to center of main monitor | |
-- option_shift - to restore previous position | |
hs.hotkey.bind(cmd_alt, "pad+", function() | |
local win = hs.window.focusedWindow() | |
geom = hs.geometry.copy(win:frame()) | |
win:move(hs.geometry.rect(697, 511, 2279, 1346)) | |
end) | |
-- expand window to main monitor to almost fullscreen | |
-- option_shift - to restore previous position | |
hs.hotkey.bind(cmd_alt_ctrl, "pad+", function() | |
local win = hs.window.focusedWindow() | |
geom = hs.geometry.copy(win:frame()) | |
win:move(hs.geometry.rect(35, 50, 3763, 2004)) | |
end) | |
-- restores old position of frame; whichever screen is focused gets that position | |
hs.hotkey.bind(cmd_alt, "pad-", function() | |
local win = hs.window.focusedWindow() | |
win:move(hs.geometry.rect(geom)) | |
end) | |
-- Hint icons | |
hs.hotkey.bind(cmd_alt_ctrl, "padenter", function() | |
hs.hints.windowHints() | |
end) | |
-- Reload config | |
hs.hotkey.bind(cmd_alt_ctrl, "R", function() | |
hs.reload() | |
hs.alert.show("Config loaded") | |
end) | |
-- close all apps specified on line 164 | |
hs.hotkey.bind(cmd_alt_ctrl, "P", function() | |
hs.alert.show("Closing") | |
for i, v in ipairs(closeAll) do | |
local app = hs.application(v) | |
if (app) then | |
if (app.name) then | |
hs.alert.show(app:name()) | |
end | |
if (app.kill) then | |
app:kill() | |
end | |
end | |
end | |
end) | |
-- -- open all apps specified on line 165 | |
-- hs.hotkey.bind(cmd_alt_ctrl, "O", function() | |
-- hs.alert.show("Openning") | |
-- for i, v in ipairs(openAll) do | |
-- hs.alert.show(v) | |
-- hs.application.open(v) | |
-- end | |
-- end) | |
-- all apps to specific positions | |
hs.hotkey.bind(cmd_alt_ctrl, "pad3", function() | |
applyLayouts(layouts) | |
end) | |
-- focused app to specific position | |
hs.hotkey.bind(cmd_alt_ctrl, "pad2", function() | |
local focusedWindow = hs.window.focusedWindow() | |
local app = focusedWindow:application() | |
if (app) then | |
applyLayout(layouts, app) | |
end | |
end) | |
end | |
-------------------------------------------------------------------------------- | |
-- END CONFIGURATIONS | |
-------------------------------------------------------------------------------- | |
-------------------------------------------------------------------------------- | |
-- METHODS - BECAREFUL :) | |
-------------------------------------------------------------------------------- | |
-- Moves focused app to specified position | |
function applyLayout(layouts, app) | |
if (app) then | |
local appName = app:title() | |
for i, layout in ipairs(layouts) do | |
if (type(layout.name) == "table") then | |
for i, layAppName in ipairs(layout.name) do | |
if (layAppName == appName) then | |
local wins = app:allWindows() | |
local counter = 1 | |
for j, win in ipairs(wins) do | |
if (win:isVisible() and layout.func) then | |
layout.func(counter, win) | |
counter = counter + 1 | |
end | |
end | |
end | |
end | |
elseif (type(layout.name) == "string") then | |
if (layout.name == appName) then | |
local wins = app:allWindows() | |
local counter = 1 | |
for j, win in ipairs(wins) do | |
if (win:isVisible() and layout.func) then | |
layout.func(counter, win) | |
counter = counter + 1 | |
end | |
end | |
end | |
end | |
end | |
end | |
end | |
-- Moves all aps to specifies positions | |
function applyLayouts(layouts) | |
for i, layout in ipairs(layouts) do | |
if (type(layout.name) == "table") then | |
for i, appName in ipairs(layout.name) do | |
local app = hs.appfinder.appFromName(appName) | |
if (app) then | |
local wins = app:allWindows() | |
local counter = 1 | |
for j, win in ipairs(wins) do | |
if (win:isVisible() and layout.func) then | |
layout.func(counter, win) | |
counter = counter + 1 | |
end | |
end | |
end | |
end | |
elseif (type(layout.name) == "string") then | |
local app = hs.appfinder.appFromName(layout.name) | |
if (app) then | |
local wins = app:allWindows() | |
local counter = 1 | |
for j, win in ipairs(wins) do | |
if (win:isVisible() and layout.func) then | |
layout.func(counter, win) | |
counter = counter + 1 | |
end | |
end | |
end | |
end | |
end | |
end | |
function applicationWatcher(appName, eventType, appObject) | |
if (eventType == hs.application.watcher.activated) then | |
if (appName == "Finder") then | |
appObject:mainWindow():move(hs.geometry.rect(1543, 23, 2297, 613)) | |
end | |
end | |
if (eventType == hs.application.watcher.deactivated) then | |
if (appName == "Finder") then | |
os.execute("sleep " .. tonumber(2)) | |
appObject:mainWindow():move(hs.geometry.rect(1543, 23, 1464, 613)) | |
end | |
end | |
end | |
config() | |
hs.console.clearConsole() | |
local appWatcher = hs.application.watcher.new(applicationWatcher) | |
appWatcher:start() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment