Advertisement
Guest User

init.lua

a guest
Mar 1st, 2015
425
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.26 KB | None | 0 0
  1. --------------------------------------------------------------------------------
  2. -- Unsupported Spaces extension. Uses private APIs but works okay.
  3. -- (http://github.com/asmagill/hammerspoon_asm.undocumented)
  4. spaces = require("hs._asm.undocumented.spaces")
  5.  
  6. -- Get output of a bash command
  7. function os.capture(cmd)
  8. local f = assert(io.popen(cmd, 'r'))
  9. local s = assert(f:read('*a'))
  10. f:close()
  11. s = string.gsub(s, '^%s+', '')
  12. s = string.gsub(s, '%s+$', '')
  13. s = string.gsub(s, '[\n\r]+', ' ')
  14. return s
  15. end
  16.  
  17. -- Update the fan and temp. Needs iStats CLI tool from homebrew.
  18. local function updateStats()
  19. fanSpeed = os.capture("iStats fan speed | cut -c14- | sed 's/\\..*//'")
  20. temp = os.capture("iStats cpu temp | cut -c11- | sed 's/\\..*//'")
  21. end
  22.  
  23. -- Makes (and updates) the topbar menu filled with the current Space, the
  24. -- temperature and the fan speed. The Space only updates if the space is changed
  25. -- with the Hammerspoon shortcut (option + arrows does not work).
  26. local function makeStatsMenu(calledFromWhere)
  27. if statsMenu == nil then
  28. statsMenu = hs.menubar.new()
  29. end
  30. if calledFromWhere == "spaceChange" then
  31. currentSpace = tostring(spaces.currentSpace())
  32. else
  33. updateStats()
  34. end
  35. statsMenu:setTitle("Space " .. currentSpace .. " | Fan: " .. fanSpeed .. " | Temp: " .. temp)
  36. end
  37.  
  38. -- Gets a list of windows and iterates until the window title is non-empty.
  39. -- This avoids focusing the hidden windows apparently placed on top of all
  40. -- Google Chrome windows. It also checks if the empty title belongs to Chrome,
  41. -- because some apps don't give any of their windows a title, and should still
  42. -- be focused.
  43. local function spaceChange()
  44. makeStatsMenu("spaceChange")
  45. visibleWindows = hs.window.orderedWindows()
  46. for i, window in ipairs(visibleWindows) do
  47. if window:application():title() == "Google Chrome" then
  48. if window:title() ~= "" then
  49. window:focus()
  50. break
  51. end
  52. else
  53. window:focus()
  54. break
  55. end
  56. end
  57. end
  58.  
  59. --------------------------------------------------------------------------------
  60. -- Options
  61.  
  62. -- How often to update Fan and Temp
  63. updateStatsInterval = 20
  64. statsMenuTimer = hs.timer.new(updateStatsInterval, makeStatsMenu)
  65. statsMenuTimer:start()
  66.  
  67. -- instant window resizing
  68. hs.window.animationDuration = 0
  69.  
  70. local mash = {"cmd", "alt", "ctrl"}
  71. local mashshift = {"cmd", "alt", "ctrl", "shift"}
  72.  
  73.  
  74. --------------------------------------------------------------------------------
  75. -- Applications launch
  76.  
  77. local function newChromeWindow()
  78. os.execute("/Applications/'Google Chrome.app'/Contents/MacOS/'Google Chrome' --new-window")
  79. visibleWindows = hs.window.orderedWindows()
  80. visibleWindows[2]:focus()
  81. end
  82.  
  83. hs.hotkey.bind(mash, "c", function()
  84. newChromeWindow()
  85. end)
  86.  
  87. local function moveWindowToSpace()
  88. -- zoomXY = hs.window.focusedWindow():zoomButtonRect()
  89. -- zoomXY.x = zoomXY.x + 15
  90. -- zoomXY.y = zoomXY.y + 5
  91. -- hs.eventtap.event.newMouseEvent(hs.eventtap.event.types.leftmousedown, zoomXY):post()
  92. local mods = {["ctrl"] = true}
  93. changeSpaceOSX = hs.eventtap.event.newKeyEvent(mods, "2", true)
  94. -- spaces.moveToSpace("2")
  95. -- hs.eventtap.event.newMouseEvent(hs.eventtap.event.types.leftmouseup, zoomXY):post()
  96. end
  97.  
  98. hs.hotkey.bind(mash, "v", function()
  99. hs.timer.doAfter(3, moveWindowToSpace):start()
  100. end)
  101.  
  102. function reload_config(files)
  103. hs.reload()
  104. end
  105. hs.pathwatcher.new(os.getenv("HOME") .. "/.hammerspoon/", reload_config):start()
  106. hs.alert.show("Config loaded")
  107.  
  108. --------------------------------------------------------------------------------
  109. -- snap windows to edges
  110. -- (and make them half-width or half-height)
  111.  
  112. hs.hotkey.bind(mash, "y", function()
  113. local win = hs.window.focusedWindow()
  114. local f = win:fullFrame()
  115. local screen = win:screen()
  116. local max = screen:fullFrame()
  117.  
  118. f.x = max.x
  119. f.y = max.y
  120. f.w = max.w / 2
  121. f.h = max.h
  122. win:setFrame(f)
  123. end)
  124.  
  125. hs.hotkey.bind(mash, "u", function()
  126. local win = hs.window.focusedWindow()
  127. local f = win:frame()
  128. local screen = win:screen()
  129. local max = screen:frame()
  130.  
  131. f.x = max.x
  132. f.y = max.y + (max.h / 2)
  133. f.w = max.w
  134. f.h = max.h / 2
  135. win:setFrame(f)
  136. end)
  137.  
  138. hs.hotkey.bind(mash, "i", function()
  139. local win = hs.window.focusedWindow()
  140. local f = win:frame()
  141. local screen = win:screen()
  142. local max = screen:frame()
  143.  
  144. f.x = max.x
  145. f.y = max.y
  146. f.w = max.w
  147. f.h = max.h / 2
  148. win:setFrame(f)
  149. end)
  150.  
  151. hs.hotkey.bind(mash, "o", function()
  152. local win = hs.window.focusedWindow()
  153. local f = win:frame()
  154. local screen = win:screen()
  155. local max = screen:frame()
  156.  
  157. f.x = max.x + (max.w / 2)
  158. f.y = max.y
  159. f.w = max.w / 2
  160. f.h = max.h
  161. win:setFrame(f)
  162. end)
  163.  
  164. --------------------------------------------------------------------------------
  165. -- Manual window moving and resizing
  166. -- Credit to GitHub user: ztomer
  167.  
  168. hs.grid.MARGINX = 0
  169. hs.grid.MARGINY = 0
  170. hs.grid.GRIDHEIGHT = 18
  171. hs.grid.GRIDWIDTH = 18
  172.  
  173. --Alter gridsize
  174. hs.hotkey.bind(mashshift, '=', function() hs.grid.adjustHeight( 1) end)
  175. hs.hotkey.bind(mashshift, '-', function() hs.grid.adjustHeight(-1) end)
  176. hs.hotkey.bind(mash, '=', function() hs.grid.adjustWidth( 1) end)
  177. hs.hotkey.bind(mash, '-', function() hs.grid.adjustWidth(-1) end)
  178.  
  179. --Snap windows
  180. hs.hotkey.bind(mash, ';', function() hs.grid.snap(hs.window.focusedWindow()) end)
  181. hs.hotkey.bind(mash, "'", function() hs.fnutils.map(hs.window.visibleWindows(), hs.grid.snap) end)
  182.  
  183. --Move windows
  184. hs.hotkey.bind(mash, 'j', hs.grid.pushWindowDown)
  185. hs.hotkey.bind(mash, 'k', hs.grid.pushWindowUp)
  186. hs.hotkey.bind(mash, 'h', hs.grid.pushWindowLeft)
  187. hs.hotkey.bind(mash, 'l', hs.grid.pushWindowRight)
  188.  
  189. --resize windows
  190. hs.hotkey.bind(mashshift, 'k', hs.grid.resizeWindowShorter)
  191. hs.hotkey.bind(mashshift, 'j', hs.grid.resizeWindowTaller)
  192. hs.hotkey.bind(mashshift, 'l', hs.grid.resizeWindowWider)
  193. hs.hotkey.bind(mashshift, 'h', hs.grid.resizeWindowThinner)
  194.  
  195. -- toggle window zoom (acts like Alt+Shift+GreenPlusButton)
  196. hs.hotkey.bind(mash, "m", function()
  197. local win = hs.window.focusedWindow()
  198. local frame = win:frame()
  199. local id = win:id()
  200.  
  201. -- init table to save window state
  202. savedwin = savedwin or {}
  203. savedwin[id] = savedwin[id] or {}
  204.  
  205. if (savedwin[id].maximized == nil or savedwin[id].maximized == false) then
  206. savedwin[id].frame = frame
  207. savedwin[id].maximized = true
  208. win:maximize()
  209. else
  210. savedwin[id].maximized = false
  211. win:setFrame(savedwin[id].frame)
  212. savedwin[id] = nil
  213. end
  214. end)
  215.  
  216. --------------------------------------------------------------------------------
  217. -- switch Spaces
  218. hs.hotkey.bind(mash, '1', function()
  219. spaces.moveToSpace("1")
  220. spaceChange()
  221. end)
  222. hs.hotkey.bind(mash, '2', function()
  223. spaces.moveToSpace("2")
  224. spaceChange()
  225. end)
  226. hs.hotkey.bind(mash, '3', function()
  227. spaces.moveToSpace("3")
  228. spaceChange()
  229. end)
  230. hs.hotkey.bind(mash, '4', function()
  231. spaces.moveToSpace("4")
  232. spaceChange()
  233. end)
  234. hs.hotkey.bind(mash, '5', function()
  235. spaces.moveToSpace("5")
  236. spaceChange()
  237. end)
  238. hs.hotkey.bind(mash, '6', function()
  239. spaces.moveToSpace("6")
  240. spaceChange()
  241. end)
  242.  
  243.  
  244. --------------------------------------------------------------------------------
  245. -- Initialize
  246. currentSpace = tostring(spaces.currentSpace())
  247. updateStats()
  248. makeStatsMenu()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement