API Docs

One space (a desktop, or a fullscreen app's space) on a screen.

Get these from a screen (screen.spaces, screen.currentSpace), a window (window.spaces) or hs.screen (e.g. hs.screen.focusedSpace()); don't create them yourself. An HSSpace always reports the space's current state, so there's no need to fetch it again after something changes. If the space is removed, exists becomes false, its other properties become null, and its methods reject.

To move a window to a space, use the window's moveToSpace().

Compare spaces by id, not with ===: two lookups of the same space give two different objects.

Properties

id

number

The space's ID. macOS assigns these, and they last until the space is removed, so a removed and re-added space gets a new ID. They may change when you log out or restart: use uuid (with hs.screen.spaceForUUID()) to find a space again in a later session.

uuid

string

The space's UUID, which macOS keeps across logins (it's how it remembers which space is which). null if the space no longer exists, or if macOS hasn't given it one, which it doesn't for the first desktop of a new user account.

exists

boolean

Whether the space still exists

type

string

What kind of space this is: "desktop" for an ordinary desktop, "fullscreen" for a fullscreen app (or two apps in Split View), or "unknown" for a kind of space this version of Hammerspoon doesn't recognise. null if the space no longer exists.

screen

HSScreen

The screen the space belongs to. Every space belongs to exactly one screen. null if the space no longer exists, or its screen has been disconnected.

If "Displays have separate Spaces" is turned off, all screens share one set of spaces, which macOS attributes to the primary screen.

desktopNumber

NSNumber

The "Desktop N" number Mission Control shows for this desktop. Mission Control only numbers desktops, and numbers them across all displays in turn, so a second display's first desktop can be "Desktop 3". (When there's only one desktop, Mission Control calls it just "Desktop", but this is still 1.) null for a fullscreen space, or if the space no longer exists.

isCurrent

boolean

Whether this is the space its screen is currently showing. Each screen shows one space at a time, so with several screens, several spaces are current.

isFocused

boolean

Whether this is the focused space: the current space of the screen with keyboard focus

Methods

windowIDs() -> number[]

The IDs of the windows on this space, front to back. Only ordinary windows are included (not menus, panels, the menu bar, etc). Windows set to appear on every space are included for every space. Hammerspoon can't make HSWindow objects for windows on other spaces, which is why these are IDs.

windowIDs() -> number[]
number[]
An array of window IDs, empty if the space no longer exists
const space = hs.screen.focusedSpace()
console.log(space.windowIDs().length + " windows on this space")

goto() -> Promise<void>

Switches this space's screen to this space, by opening Mission Control and clicking it. Needs Accessibility permission.

goto() -> Promise<void>
Promise<void>
A Promise that resolves once the screen is showing this space
hs.screen.main().spaces[0].goto()
    .then(() => console.log("Switched"))
    .catch(err => console.error(err))

remove() -> Promise<void>

Removes this desktop, by opening Mission Control and clicking its close button. Any windows on it move to another desktop on the same screen. Needs Accessibility permission.

Only desktops can be removed - not fullscreen spaces - and macOS won't remove a screen's only desktop, or the desktop the screen is currently showing.

remove() -> Promise<void>
Promise<void>
A Promise that resolves once the desktop has gone, or rejects
hs.screen.main().addSpace()
    .then(space => space.remove())

toJSON() -> {[key: string]: any}

A summary of the space, for JSON.stringify() and the Console

toJSON() -> {[key: string]: any}
{[key: string]: any}
An object with the space's id, uuid, type, screen (its UUID),
console.log(JSON.stringify(hs.screen.focusedSpace()))