Inspect and control the displays attached to the system.
Obtaining screens
constall = hs.screen.all(); // [HSScreen, ...] constmain = hs.screen.main(); // screen containing the focused window constprimary = hs.screen.primary(); // screen with the global menu bar
Navigation
constright = hs.screen.main().toEast(); if (right) console.log("Screen to the right:", right.name);
Each screen has its own ordered list of spaces (Mission Control's desktops and fullscreen
apps), and shows one of them at a time: its current space. Every space belongs to exactly
one screen. The focused space is the current space of whichever screen has keyboard focus.
So a screen has spaces and a currentSpace, every HSSpace knows its screen, and a
window knows its spaces and can moveToSpace().
This relies on "Displays have separate Spaces" being turned on in System Settings → Desktop &
Dock, which is macOS's default. If it's off, macOS treats all your screens as one, so every
screen reports the same spaces, which belong to the primary screen.
Spaces are identified by numeric IDs, which last as long as the space does. Each space also
has a UUID, which macOS keeps across logins, so to find the same space in a later session,
save its uuid and look it up with hs.screen.spaceForUUID().
Warning: macOS has no public API for Spaces. Hammerspoon uses private functions from
Apple's SkyLight framework, which Apple can change or remove in any macOS update without
notice. If that happens, Spaces features may stop working or return wrong results.
hs.screen.spacesAvailable() reports whether the private functions could be found at all,
but it can't detect one whose behaviour has changed.
Adding, removing, and switching spaces works by briefly opening Mission Control and clicking
its buttons through the Accessibility API, since only macOS itself can make those changes.
These need Accessibility permission, return Promises, and visibly open Mission Control. They
depend on Mission Control's undocumented internal UI, so they can break in a macOS update too.
Moving a window to another space doesn't need Mission Control.
// Switch the main screen to its next space constspaces = hs.screen.main().spaces constindex = spaces.findIndex(s=>s.isCurrent) spaces[(index + 1) % spaces.length].goto()
// Add a desktop, then remove it again hs.screen.main().addSpace() .then(space=>space.remove()) .catch(err=>console.error(err))
// Send the focused window to the main screen's last desktop constdesktops = hs.screen.main().spaces.filter(s=>s.type === "desktop") hs.window.focusedWindow().moveToSpace(desktops[desktops.length - 1])
// Watch for screens switching space hs.screen.on('spaceChange', ({ screen, from, to }) => { console.log(screen.name + " switched to space " + to.id) })
Inspect and control the displays attached to the system.
Obtaining screens
Navigation
Display modes
Screenshots
Watching for display changes
Spaces
Each screen has its own ordered list of spaces (Mission Control's desktops and fullscreen apps), and shows one of them at a time: its current space. Every space belongs to exactly one screen. The focused space is the current space of whichever screen has keyboard focus. So a screen has
spacesand acurrentSpace, everyHSSpaceknows itsscreen, and a window knows itsspacesand canmoveToSpace().This relies on "Displays have separate Spaces" being turned on in System Settings → Desktop & Dock, which is macOS's default. If it's off, macOS treats all your screens as one, so every screen reports the same spaces, which belong to the primary screen.
Spaces are identified by numeric IDs, which last as long as the space does. Each space also has a UUID, which macOS keeps across logins, so to find the same space in a later session, save its
uuidand look it up withhs.screen.spaceForUUID().Adding, removing, and switching spaces works by briefly opening Mission Control and clicking its buttons through the Accessibility API, since only macOS itself can make those changes. These need Accessibility permission, return Promises, and visibly open Mission Control. They depend on Mission Control's undocumented internal UI, so they can break in a macOS update too. Moving a window to another space doesn't need Mission Control.