Guest User

Untitled

a guest
Dec 17th, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.57 KB | None | 0 0
  1. -- -----------------
  2. -- Setup environment
  3. -- -----------------
  4.  
  5. -- Animation off, mofo
  6. hs.window.animationDuration = 0
  7.  
  8. -- Get list of screens and refresh that list whenever screens are (un)plugged
  9. local screens = hs.screen.allScreens()
  10. local screenwatcher = hs.screen.watcher.new(function()
  11. screens = hs.screen.allScreens()
  12. end)
  13. screenwatcher:start()
  14.  
  15. -- Modifier shortcuts
  16. local hyper = {"⌥", "⇧"}
  17. local pushkey = {"⌃", "⌘"}
  18.  
  19. -- Tracking variables
  20. local pressed = {
  21. up = false,
  22. down = false,
  23. left = false,
  24. right = false
  25. }
  26.  
  27. -- Full screen sizes
  28. local fullScreenSizeIndex = 0
  29. local fullScreenSizes = {
  30. { x = 0.00, y = 0.00, w = 1.0, h = 1.0 },
  31. { x = 0.05, y = 0.05, w = 0.9, h = 0.9 },
  32. { x = 0.10, y = 0.10, w = 0.8, h = 0.8 },
  33. { x = 0.15, y = 0.15, w = 0.7, h = 0.7 },
  34. { x = 0.20, y = 0.20, w = 0.6, h = 0.6 },
  35. { x = 0.25, y = 0.25, w = 0.5, h = 0.5 }
  36. }
  37.  
  38.  
  39. -- ----------------
  40. -- Helper functions
  41. -- ----------------
  42.  
  43. -- Resize window for chunk of screen.
  44. -- For x and y: use 0 to expand fully in that dimension, 0.5 to expand halfway
  45. -- For w and h: use 1 for full, 0.5 for half
  46. function push(x, y, w, h)
  47. local win = hs.window.focusedWindow()
  48. local f = win:frame()
  49. local screen = win:screen()
  50. local max = screen:frame()
  51.  
  52. f.x = max.x + (max.w*x)
  53. f.y = max.y + (max.h*y)
  54. f.w = max.w*w
  55. f.h = max.h*h
  56. win:setFrame(f)
  57. end
  58.  
  59. -- Shortcuts for pushing to top, bottom, left, right in every combination
  60. function pushT() push(0.0, 0.0, 1.0, 0.5) end
  61. function pushB() push(0.0, 0.5, 1.0, 0.5) end
  62. function pushL() push(0.0, 0.0, 0.5, 1.0) end
  63. function pushR() push(0.5, 0.0, 0.5, 1.0) end
  64. function pushTL() push(0.0, 0.0, 0.5, 0.5) end
  65. function pushTR() push(0.5, 0.0, 0.5, 0.5) end
  66. function pushBL() push(0.0, 0.5, 0.5, 0.5) end
  67. function pushBR() push(0.5, 0.5, 0.5, 0.5) end
  68.  
  69.  
  70. -- Move to monitor x. Checks to make sure monitor exists, if not moves to last monitor that exists
  71. function moveToMonitor(x)
  72. local win = hs.window.focusedWindow()
  73. local newScreen = nil
  74. while not newScreen do
  75. newScreen = screens[x]
  76. x = x-1
  77. end
  78. win:moveToScreen(newScreen)
  79. end
  80.  
  81. -- -----------------
  82. -- Window management
  83. -- -----------------
  84.  
  85. -- Push to left (including top left and bottom left)
  86. hs.hotkey.bind(pushkey, "left", function()
  87. pressed.left = true
  88. if pressed.up then
  89. pushTL()
  90. elseif pressed.down then
  91. pushBL()
  92. else
  93. pushL()
  94. end
  95. end, function ()
  96. pressed.left = false
  97. end)
  98.  
  99. -- Push to right (including top right and bottom right)
  100. hs.hotkey.bind(pushkey, "right", function()
  101. pressed.right = true
  102. if pressed.up then
  103. pushTR()
  104. elseif pressed.down then
  105. pushBR()
  106. else
  107. pushR()
  108. end
  109. end, function()
  110. pressed.right = false
  111. end)
  112.  
  113. -- Push to top (including top right and top left)
  114. hs.hotkey.bind(pushkey, "up", function()
  115. pressed.up = true
  116. if pressed.left then
  117. pushTL()
  118. elseif pressed.right then
  119. pushTR()
  120. else
  121. pushT()
  122. end
  123. end, function()
  124. pressed.up = false
  125. end)
  126.  
  127. -- Push to bottom (including bottom left and bottom right)
  128. hs.hotkey.bind(pushkey, "down", function()
  129. pressed.down = true
  130. if pressed.left then
  131. pushBL()
  132. elseif pressed.right then
  133. pushBR()
  134. else
  135. pushB()
  136. end
  137. end, function()
  138. pressed.down = false
  139. end)
  140.  
  141. -- Center window with some room to see the desktop
  142. hs.hotkey.bind(pushkey, "m", function() push(0.05,0.05,0.9,0.9) end)
  143.  
  144. -- Move a window between monitors
  145. hs.hotkey.bind(pushkey, "1", function() moveToMonitor(1) end) -- Move to first monitor
  146. hs.hotkey.bind(pushkey, "2", function() moveToMonitor(2) end) -- Move to second monitor
  147.  
  148. -- Tile windows on current screen
  149. hs.hotkey.bind({'cmd', 'ctrl'}, 't', function()
  150. local wins = hs.window.filter.new():setCurrentSpace(true):getWindows()
  151. local screen = hs.screen.mainScreen():currentMode()
  152. local rect = hs.geometry(0, 0, screen['w'], screen['h'])
  153. hs.window.tiling.tileWindows(wins, rect)
  154. end)
  155.  
  156.  
  157. -- -------------
  158. -- "Full" screen
  159. -- -------------
  160.  
  161. -- Cycle through "full" screen sizes
  162. hs.hotkey.bind(pushkey, "f", function()
  163. fullScreenSizeIndex = fullScreenSizeIndex + 1
  164. if fullScreenSizeIndex > #fullScreenSizes then fullScreenSizeIndex = 1 end
  165. size = fullScreenSizes[fullScreenSizeIndex]
  166. push(size.x, size.y, size.w, size.h)
  167. end)
  168.  
  169. -- Watch for focus changes to reset the full screen size cycling
  170. allwindows = hs.window.filter.new(nil)
  171. allwindows:subscribe(hs.window.filter.windowUnfocused, function () fullScreenSizeIndex = 0 end)
  172.  
  173.  
  174. -- ---------------------
  175. -- Application shortcuts
  176. -- ---------------------
  177.  
  178. appmode = hs.hotkey.modal.new({"cmd"}, "E")
  179. appmode:bind({}, "B", function() hs.application.launchOrFocus("Safari") appmode:exit() end)
  180. appmode:bind({}, "T", function() hs.application.launchOrFocus("iTerm") appmode:exit() end)
  181. appmode:bind({}, "M", function() hs.application.launchOrFocus("Mail") appmode:exit() end)
  182. appmode:bind({}, "S", function() hs.application.launchOrFocus("Slack") appmode:exit() end)
  183. appmode:bind({}, "R", function() hs.application.launchOrFocus("Tweetbot") appmode:exit() end)
  184. appmode:bind({}, "I", function() hs.application.launchOrFocus("Messages") appmode:exit() end)
  185.  
  186. -- -----------------------
  187. -- Configuration reloading
  188. -- -----------------------
  189.  
  190. function reloadConfig(files)
  191. doReload = false
  192. for _,file in pairs(files) do
  193. if file:sub(-4) == ".lua" then
  194. doReload = true
  195. end
  196. end
  197. if doReload then
  198. hs.reload()
  199. end
  200. end
  201.  
  202. hs.pathwatcher.new(os.getenv("HOME") .. "/.hammerspoon/", reloadConfig):start()
  203. hs.alert.show("Config loaded")
Add Comment
Please, Sign In to add comment