Inspect and control the displays attached to the system.
Obtaining screens
const all = hs.screen.all(); // [HSScreen, ...]
const main = hs.screen.main(); // screen containing the focused window
const primary = hs.screen.primary(); // screen with the global menu bar
Navigation
const right = 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
const spaces = hs.screen.main().spaces
const index = 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
const desktops = 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)
})
An event name for hs.screen.on(), hs.screen.off() and hs.screen.once()
Event
Description
"change"
The display configuration changed: a display was connected or disconnected, its resolution or arrangement changed, or the menu bar moved to another display
Whether the private macOS functions Spaces support relies on could be found. If this is
false, every Spaces property and function returns an empty or failure result.
A true result does not guarantee every function works - it only means none of them
have been removed. Apple can still change how they behave.
Declaration
hs.screen.spacesAvailable() -> boolean
Returns
boolean
true if the private SkyLight functions are available, otherwise false
Example
if (!hs.screen.spacesAvailable()) {
console.error("Spaces aren't supported on this version of macOS")
}
Whether each screen has its own spaces ("Displays have separate Spaces" in System
Settings → Desktop & Dock). Hammerspoon expects it to be on, which is macOS's default. If
it's off, all screens share one set of spaces.
Declaration
hs.screen.screensHaveSeparateSpaces() -> boolean
Returns
boolean
true if each screen has its own spaces
Example
if (!hs.screen.screensHaveSeparateSpaces()) {
console.warn("Turn on 'Displays have separate Spaces' for per-screen spaces")
}
Every space on every screen, screen by screen, each in Mission Control's order. Screens
are in the order macOS lists them, which is also the order Mission Control numbers
desktops in.
The names Mission Control shows for each space, such as "Desktop 2" or "Safari". This
opens Mission Control briefly to read them, and needs Accessibility permission. For a
desktop's number without opening Mission Control, use HSSpace.desktopNumber.
Toggles Mission Control, like pressing its key. This returns straight away; use
openMissionControl() or closeMissionControl() to know when it has finished.
"change" fires whenever the display configuration changes — monitors connected/disconnected, resolution or arrangement changes, or the menu bar moving to a different display. The listener gets no arguments; call all()/main()/primary() inside it to inspect the new configuration.
"spaceChange" fires whenever a screen switches to a different space. If several screens switch at once, it fires once for each. The listener gets an object with the screen that switched (null if it has just been disconnected), the space it was showing before (from, null for a screen that has just been connected), and the space it's showing now (to).
Called when the event happens; see above for its argument
Event Names
Event
Description
"change"
The display configuration changed: a display was connected or disconnected, its resolution or arrangement changed, or the menu bar moved to another display
"spaceChange"
A screen switched to a different Space
Returns
None
Throws
Throws an Error on failure; wrap calls in try/catch to handle it.
Example
try {
hs.screen.on('change', () => console.log("Screens changed, now: " + hs.screen.all().length))
hs.screen.on('spaceChange', ({ screen, to }) => console.log(screen.name + " is now showing space " + to.id))
} catch (err) {
console.error(err.message)
}
The display configuration changed: a display was connected or disconnected, its resolution or arrangement changed, or the menu bar moved to another display
The display configuration changed: a display was connected or disconnected, its resolution or arrangement changed, or the menu bar moved to another display
"spaceChange"
A screen switched to a different Space
Returns
None
Throws
Throws an Error on failure; wrap calls in try/catch to handle it.