hs.screen.primary() -> HSScreen
The primary display — the one that contains the global menu bar.
hs.screen.primary() -> HSScreen
null if no primary screen can be determined.const s = hs.screen.primary()
console.log(s && s.frame)
Inspect and control the displays attached to the system.
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
const right = hs.screen.main().toEast();
if (right) console.log("Screen to the right:", right.name);
const s = hs.screen.primary();
console.log(s.mode);
// → { width: 1440, height: 900, scale: 2, frequency: 60 }
s.setMode(1920, 1080, 1, 60);
hs.screen.main().snapshot()
.then(img => img.saveToFile("/tmp/screen.png"));
hs.screen.on('change', () => {
console.log("Display configuration changed:", hs.screen.all().length, "screens");
});
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)
})
This module provides the following types:
This module has no properties.
All connected screens.
hs.screen.all() -> HSScreen[]
const screens = hs.screen.all()
screens.forEach(s => console.log(s.name))
The screen that currently contains the focused window, or the screen with the keyboard focus if no window is focused.
hs.screen.main() -> HSScreen
null if no main screen can be determined.const main = hs.screen.main()
console.log(main && main.name)
The primary display — the one that contains the global menu bar.
hs.screen.primary() -> HSScreen
null if no primary screen can be determined.const s = hs.screen.primary()
console.log(s && s.frame)
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.
hs.screen.spacesAvailable() -> boolean
true if the private SkyLight functions are available, otherwise falseif (!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.
hs.screen.screensHaveSeparateSpaces() -> boolean
true if each screen has its own spacesif (!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.
hs.screen.allSpaces() -> HSSpace[]
const fullscreen = hs.screen.allSpaces().filter(s => s.type === "fullscreen")
console.log(fullscreen.length + " fullscreen apps")
The focused space: the space showing on the screen with keyboard focus
hs.screen.focusedSpace() -> HSSpace
null if it can't be foundconsole.log("Focused space: " + hs.screen.focusedSpace().id)
Finds a space by its ID
hs.screen.spaceForID(id) -> HSSpace
| Name | Type | Description |
|---|---|---|
| id | number | The space's ID |
null if there's no space with that IDconst space = hs.screen.spaceForID(3)
if (space) space.goto()
Finds a space by its UUID. Unlike IDs, macOS keeps space UUIDs across logins, so this is how to find a space you saved in an earlier session.
hs.screen.spaceForUUID(uuid) -> HSSpace
| Name | Type | Description |
|---|---|---|
| uuid | string | The space's UUID |
null if there's no space with that UUIDconst workUUID = hs.settings.get("workSpace")
const work = workUUID ? hs.screen.spaceForUUID(workUUID) : null
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.
hs.screen.missionControlNames() -> Promise<Record>
hs.screen.missionControlNames().then(names => {
for (const space of hs.screen.allSpaces()) {
console.log(space.id + " is " + names[space.id])
}
})
Opens Mission Control, and keeps it open until closeMissionControl(), even across other
Spaces operations that use it. Needs Accessibility permission.
hs.screen.openMissionControl() -> Promise<void>
hs.screen.openMissionControl()
.then(() => hs.timer.doAfter(2, () => hs.screen.closeMissionControl()))
Closes Mission Control, if it's open. Needs Accessibility permission.
hs.screen.closeMissionControl() -> Promise<void>
hs.screen.closeMissionControl()
Toggles Mission Control, like pressing its key. This returns straight away; use
openMissionControl() or closeMissionControl() to know when it has finished.
hs.screen.toggleMissionControl() -> None
hs.hotkey.bind(["cmd", "alt"], "m", () => hs.screen.toggleMissionControl())
Toggles App Exposé (the current app's windows, on every space), like its trackpad gesture
hs.screen.toggleAppExpose() -> None
hs.screen.toggleAppExpose()
Toggles Launchpad
hs.screen.toggleLaunchpad() -> None
hs.screen.toggleLaunchpad()
Toggles Show Desktop, which moves every window aside to reveal the desktop
hs.screen.toggleShowDesktop() -> None
hs.screen.toggleShowDesktop()
Register a listener for screen events.
"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).
hs.screen.on(event, listener) -> None
| Name | Type | Description |
|---|---|---|
| event | any | The event to listen for |
| listener | any | Called when the event happens; see above for its argument |
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)
}
Remove a previously registered screen event listener.
hs.screen.off(event, listener) -> None
| Name | Type | Description |
|---|---|---|
| event | any | The event the listener was registered for |
| listener | any | The function originally passed to on |
const onChange = () => console.log("screens changed")
hs.screen.on('change', onChange)
// later…
hs.screen.off('change', onChange)
Register a listener that fires at most once, the next time a screen event happens. See on() for the events.
hs.screen.once(event, listener) -> None
| Name | Type | Description |
|---|---|---|
| event | any | The event to listen for |
| listener | any | Called once, then automatically removed |
try {
hs.screen.once('change', () => console.log("First change detected"))
} catch (err) {
console.error(err.message)
}