HammerSpoon is an open source, free App for window management. Of course, there’s more to it than that

The purpose of this article is to show you how to configure window management functionality with HammerSpoon. There are SizeUp, Moom and Divvy for a fee, and Spectacle and Slate for free

SizeUp and Divvy have different features. SizeUp makes it easy to quickly resize a window, but not to quickly resize a window. Divvy does the opposite. It is possible to set quick Resize to a specified size in Divvy, but this is basic and cannot be adjusted in real time. The Spectacle and Slate are more like a combination of the two, and more powerful than either, but take some time to configure. The original Slate writer hasn’t been in charge of the project for three years, but the fork is maintained

HammerSpoon is recommended for its powerful window management capabilities. It’s also developer-friendly, with console Debug. Language using Lua, not difficult to get started. In addition, there are countless system apis that can be called at will. So HammerSpoon is much more than just window management software

If you are:

  1. Using MacOS
  2. Want to use the keyboard to adjust the size and position of the window
  3. You have multiple external screens and want to use shortcuts to throw Windows from one screen to the next

Well, read on

If you don’t want to see the configuration details or have already mastered the configuration method, you can directly jump to the download page, load my configuration file, just take a few minutes, you can say goodbye to these unscientific operations

use

Let’s take a look at the configuration and see what it does.

Following this tutorial configuration, you can use keyboard shortcuts to trigger the following actions:

  1. A window in the current screen full screen, applies to any screen and window
  2. Center window on current screen
  3. Resize the window to half the size of the screen (up, down, or so). Works with any screen and window
  4. Throw the current window onto another monitor and (optional) display it in full screen
  5. Adjust the current window size arbitrarily
  6. I didn’t include this feature for arbitrary moving Windows because I didn’t need it. If you need to implement this feature, read this blog post and you will

Download and install

The README file makes it clear, so let’s list it here (MacOS shortcuts) :

  • Move the window to another screen
    • Ctrl Alt + leftMove to the screen on the left
    • Ctrl Alt + rMove to the screen on the right
  • Full screen
  • Place the window in the center of the screen
  • Set the window size to half the screen size
    • Ctrl Alt -command + to the leftHalf the width, left
    • Ctrl Alt -command + rightHalf width, right side
    • Ctrl Alt -command +Half height, lean up
    • Under the Ctrl Alt -command +It’s half way down
  • Set window size based on left and top (meaning left and top margins are not moving)
    • Ctrl, Alt, Shift + leftMove the right side of the window to the left
    • Ctrl, Alt, Shift + rightMove the right side of the window
    • The Ctrl, Alt, Shift +The upper boundary of the window moves up
    • Under the Ctrl, Alt, Shift +The top edge of the window moves down
  • Set window size based on right and bottom
    • Alt Command - Shift + leftMove the left side of the window to the left
    • Alt Command - Shift + rightMove the left side of the window to the right
    • Alt Command - Shift +The bottom edge of the window moves up
    • Under the Alt Command - Shift +The lower edge of the window moves down

The shortcuts for center, full screen, moving the window to another screen, and setting the window to half the screen size are the same as SizeUp. As for window resizing, just remember that Ctrl-Alt-shift holds the left and bottom, and command-alt-shift holds the right and bottom. After all, Ctrl is to the left of Command

configuration

In terms of configuration, HammerSpoon offers a number of apis, both its own and those from the Mac OS. The full API documentation is available here

API

Here are the apis I used in my configuration file:

  1. hs.alert- Displays a prompt message. The location defaults to the center of the screen, with white text on a black background, and disappears after about three seconds. There are other similar functionshs.notifyNotification of the system
  2. Hs. grid – Used to set the grid. My configuration files, which implement window resizing and position adjustment, are based on this API
  3. hs.hotkey- Used to set shortcut keys. It’s called in my configuration filebindmethods
  4. Hs.screen – Screen-related API to get screen parameters and set Grid
  5. Hs.screen. watcher – A watcher used to monitor screen resolution, quantity changes, etc
  6. Hs.window – API associated with application Windows that provides methods for getting active Windows, moving Windows to other displays, moving Windows to the center of the screen, and so on

The basic principle of

First, divide the screen into grids based on its aspect ratio. A 16:9 screen, for example, is split into 8 by 4, which looks like this:

So, by setting how many cells the window occupies, you can achieve a quick layout

A little advice

  1. About Lua. If you have a foundation in Python or JavaScript, it is not too difficult to write the configuration. Please refer to the latest version (currently 5.3.3) of Lua’s official website. If you have a language problem, do a StackOverflow search first
  2. For questions about HammerSpoon, check out their Start Guide, which provides answers to some of the most basic questions
  3. In Lua, callfunctionThere are two ways to write it. One is thefoo.bar()And the other one isfoo:bar(). If you use the colon, you pass it inthis(Or in Pythonself) as the first argument to the function. As a result,foo:bar(baz)It’s the equivalent offoo.bar(foo, baz)
  4. It’s important to use the Console well. Once HammerSpoon is installed, click the HammerSpoon icon on the MenuBar, and then click Console to open the Console. HammerSpoon itself also provides an interface for Console, hs.console
  5. If you simply want to output something on the Console, you just need to write it in your codeprintThat’s it. Lua supports two typesprintOne way to write it isprint "foo", applies to strings. There’s another wayprint(foo), applies to variables
  6. In HammerSpoon’s documentation, some methods are used.Some are called with:The call. with.Almost all of the methods called can be executed directly on the Console. But with:The method called, if executed directly from the Console, will report an error. Like this one:hs.application:allWindows(). As the name suggests, this is the window used to get all the Windows of an Application. In Console, we just need to call it with an instance. For example, if we want to display all of Chrome’s Windows, the two notations are the same: <
    hs.application.find('chrome'):allWindows()
    hs.application.allWindows(hs.application.find('chrome'))
    Copy the code

Write in the last

As mentioned earlier, HammerSpoon can do a lot more than just manage Windows. I’ll talk about global controls for music players in my next blog post. Interested friends can take a look at the code I submitted, as well as the documentation

Feel free to leave a comment or comment below. If you liked this article, please give me a thumbs up at 👍