API Docs

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);

Display modes

const s = hs.screen.primary();
console.log(s.mode);
// → { width: 1440, height: 900, scale: 2, frequency: 60 }

s.setMode(1920, 1080, 1, 60);

Screenshots

hs.screen.main().snapshot()
    .then(img => img.saveToFile("/tmp/screen.png"));

Watching for display changes

hs.screen.on('change', () => {
    console.log("Display configuration changed:", hs.screen.all().length, "screens");
});

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 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)
})

Types

This module provides the following types:

Properties

This module has no properties.

Methods

hs.screen.all() -> HSScreen[]

All connected screens.

hs.screen.all() -> HSScreen[]
HSScreen[]
An array of HSScreen objects
const screens = hs.screen.all()
screens.forEach(s => console.log(s.name))

hs.screen.main() -> HSScreen

The screen that currently contains the focused window, or the screen with the keyboard focus if no window is focused.

hs.screen.main() -> HSScreen
HSScreen
An HSScreen object or null if no main screen can be determined.
const main = hs.screen.main()
console.log(main && main.name)

hs.screen.primary() -> HSScreen

The primary display — the one that contains the global menu bar.

hs.screen.primary() -> HSScreen
HSScreen
An HSScreen object or null if no primary screen can be determined.
const s = hs.screen.primary()
console.log(s && s.frame)

hs.screen.spacesAvailable() -> boolean

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
boolean
true if the private SkyLight functions are available, otherwise false
if (!hs.screen.spacesAvailable()) {
    console.error("Spaces aren't supported on this version of macOS")
}

hs.screen.screensHaveSeparateSpaces() -> boolean

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
boolean
true if each screen has its own spaces
if (!hs.screen.screensHaveSeparateSpaces()) {
    console.warn("Turn on 'Displays have separate Spaces' for per-screen spaces")
}

hs.screen.allSpaces() -> HSSpace[]

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[]
HSSpace[]
An array of HSSpace objects
const fullscreen = hs.screen.allSpaces().filter(s => s.type === "fullscreen")
console.log(fullscreen.length + " fullscreen apps")

hs.screen.focusedSpace() -> HSSpace

The focused space: the space showing on the screen with keyboard focus

hs.screen.focusedSpace() -> HSSpace
HSSpace
The focused HSSpace, or null if it can't be found
console.log("Focused space: " + hs.screen.focusedSpace().id)

hs.screen.spaceForID(id) -> HSSpace

Finds a space by its ID

hs.screen.spaceForID(id) -> HSSpace
Name Type Description
id number The space's ID
HSSpace
The HSSpace, or null if there's no space with that ID
const space = hs.screen.spaceForID(3)
if (space) space.goto()

hs.screen.spaceForUUID(uuid) -> HSSpace

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
HSSpace
The HSSpace, or null if there's no space with that UUID
const workUUID = hs.settings.get("workSpace")
const work = workUUID ? hs.screen.spaceForUUID(workUUID) : null

hs.screen.missionControlNames() -> Promise<Record>

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>
Promise<Record>
A Promise that resolves to an object mapping
hs.screen.missionControlNames().then(names => {
    for (const space of hs.screen.allSpaces()) {
        console.log(space.id + " is " + names[space.id])
    }
})

hs.screen.openMissionControl() -> Promise<void>

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>
Promise<void>
A Promise that resolves once Mission Control is fully open, or
hs.screen.openMissionControl()
    .then(() => hs.timer.doAfter(2, () => hs.screen.closeMissionControl()))

hs.screen.closeMissionControl() -> Promise<void>

Closes Mission Control, if it's open. Needs Accessibility permission.

hs.screen.closeMissionControl() -> Promise<void>
Promise<void>
A Promise that resolves once Mission Control has fully closed,
hs.screen.closeMissionControl()

hs.screen.toggleMissionControl() -> None

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
None
hs.hotkey.bind(["cmd", "alt"], "m", () => hs.screen.toggleMissionControl())

hs.screen.toggleAppExpose() -> None

Toggles App Exposé (the current app's windows, on every space), like its trackpad gesture

hs.screen.toggleAppExpose() -> None
None
hs.screen.toggleAppExpose()

hs.screen.toggleLaunchpad() -> None

Toggles Launchpad

hs.screen.toggleLaunchpad() -> None
None
hs.screen.toggleLaunchpad()

hs.screen.toggleShowDesktop() -> None

Toggles Show Desktop, which moves every window aside to reveal the desktop

hs.screen.toggleShowDesktop() -> None
None
hs.screen.toggleShowDesktop()

hs.screen.on(event, listener) -> None

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
None
Throws an Error on failure; wrap calls in try/catch to handle it.
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)
}

hs.screen.off(event, listener) -> None

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
None
const onChange = () => console.log("screens changed")
hs.screen.on('change', onChange)
// later…
hs.screen.off('change', onChange)

hs.screen.once(event, listener) -> None

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
None
Throws an Error on failure; wrap calls in try/catch to handle it.
try {
    hs.screen.once('change', () => console.log("First change detected"))
} catch (err) {
    console.error(err.message)
}