Hammerspoon for Mac - Automate me completely
Macos / / December 26, 2019
Many actions in OS X can be automated with the help of a staff Automator application. Hammerspoon free utility does the same thing, but more deeply integrated with the system, allowing you to create hot keys tied to the actions and even small programs.
Such an unusual name creators must have wanted to emphasize the versatility and flexibility Hammerspoon. They can hardly be reproached, because thanks to the support of Lua-scripts opening up before us is really a tremendous opportunity.
Immediately it should say that Hammerspoon we'll have to tinker with the code a little bit. But do not worry, be able to write it yourself is not necessary (although it is very easy in the case of Lua), on site contains many ready examples based on extensions that can be used or modified.
After installing Hammerspoon need to enable universal access to it, and then go to the menu Open Config in a text editor to add the code of our first script window.
Traditionally, you can start with Hello World, or go directly to more complex things: developers have prepared
a detailed guide. We are with you will not waste your time on trifles and make shortcuts for moving windows. To do this, insert into our init.lua following code, save the document and choose to Hammerspoon Reload Config menu.hs.hotkey.bind ({ "cmd", "alt", "ctrl"}, "Y", function () local win = hs.window.focusedWindow () local f = win: frame () fx = fx - 10 fy = fy - 10 win: setFrame (f) end) hs.hotkey.bind ({ "cmd", "alt", "ctrl"}, "K", function () local win = hs.window.focusedWindow () local f = win: frame () fy = fy - 10 win: setFrame (f) end) hs.hotkey.bind ({ "cmd", "alt", "ctrl"}, "U", function () local win = hs.window.focusedWindow () local f = win: frame () fx = fx 10 + fy = fy - 10 win: setFrame (f) end) hs.hotkey.bind ({ "cmd", "alt", "ctrl"}, "H", function () local win = hs.window.focusedWindow () local f = win: frame () fx = fx - 10 win: setFrame (f) end) hs.hotkey.bind ({ "cmd", "alt", "ctrl"}, "L", function () local win = hs.window.focusedWindow () local f = win: frame () fx = fx + 10 win: setFrame (f) end) hs.hotkey.bind ({ "cmd", "alt", "ctrl"}, "B", function () local win = hs.window.focusedWindow () local f = win: frame () fx = fx - 10 fy = fy + 10 win: setFrame (f) end) hs.hotkey.bind ({ "cmd", "alt", "ctrl"}, "J", function () local win = hs.window.focusedWindow () local f = win: frame () fy = fy + 10 win: setFrame (f) end) hs.hotkey.bind ({ "cmd", "alt", "ctrl"}, "N", function () local win = hs.window.focusedWindow () local f = win: frame () fx = fx 10 + fy = fy + 10 win: setFrame (f) end)
If you look closely at the code, you can see the parameters indicating the number of pixels that the window is moved, its position and the key combinations that are responsible for movement. Check: hold ⌃⌥⌘, We press on the H / L key to move left and right and the J / K to move up and down. Everything works.
As a mini-programs can cite the example of the implementation of the popular utility analogue Caffeine, prevents the Mac transition to sleep. The relevant script only a dozen lines of code.
local caffeine = hs.menubar.new () function setCaffeineDisplay (state) if state then caffeine: setTitle ( "AWAKE") else caffeine: setTitle ( "SLEEPY") end. end function caffeineClicked () setCaffeineDisplay (hs.caffeinate.toggle ( "displayIdle")) end if caffeine then caffeine: setClickCallback (caffeineClicked) setCaffeineDisplay (hs.caffeinate.get ( "displayIdle")) end
After saving SLEEPY simple button appears in the menu bar, changing to AWAKE, if the ban goes to sleep.
In the script, you can add some code fragments with various functions, they will work independently of each other. Just Hammerspoon available about hundreds of APIThat allow to automate a variety of actions from moving windows, interaction with clipboard and run AppleScript to all sorts of processes based on events, such as network connectivity, connect USB-devices and many others.
The utility is very useful, and if you're not afraid to tinker with the code, it is no exaggeration to be able to help her move mountains.