hammerspoon2-docs
    Preparing search index...

    Namespace screen

    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)
    })
    all
    allSpaces
    closeMissionControl
    focusedSpace
    main
    missionControlNames
    off
    on
    once
    openMissionControl
    primary
    screensHaveSeparateSpaces
    spaceForID
    spaceForUUID
    spacesAvailable
    toggleAppExpose
    toggleLaunchpad
    toggleMissionControl
    toggleShowDesktop