SpaceNames API

Let other apps see your Mac's desktops and switch between them, from Shortcuts, Raycast, Stream Deck, Keyboard Maestro or your own scripts.

Overview

SpaceNames runs a small API on your Mac. It's a plain local HTTP API that returns JSON:

http://localhost:8787/api/v1
  • Local only. Nothing goes through the internet. By default only apps on the same Mac can connect.
  • Off until you turn it on, and every request needs your API token.
  • What it can do: list desktops, report the current one, and switch desktops. You can also turn on window access (list the windows on the current desktop and bring one to the front) and Rectangle layouts. SpaceNames never moves or resizes windows itself, and the API can't change your names, colours or icons.
  • Requires SpaceNames 1.0.1 or later, licensed or in its free trial.

Turning it on

  1. Open SpaceNames › Settings… › API.
  2. Turn on Let other apps switch desktops.
  3. Copy the token. You can also copy a ready-made curl example.

The address is shown there too. It's normally port 8787; if that port is taken, SpaceNames uses the next free one and shows it in Settings. The API and the Phone Remote share the same port.

Switching needs macOS's own shortcuts. SpaceNames switches desktops by pressing Switch to Desktop 1…N. Turn those on once in System Settings › Keyboard › Keyboard Shortcuts… › Mission Control. If one is off, shortcut_off tells you which.

Authentication

Send your token with every request, preferably as a bearer token:

Authorization: Bearer sn_your_token_here

For tools that can't set that header, these also work, in order of preference:

  • An X-API-Key: sn_your_token_here header.
  • A ?token=sn_your_token_here query parameter. Use it only when nothing else works, because URLs end up in logs and history.

New Token in Settings makes a fresh token and stops the old one working immediately.

Requests from other computers or phones are refused with network_not_allowed unless you turn on Allow other devices on this network. Then use http://YourMacName.local:8787/api/v1, also shown in Settings.

The Space object

Desktops are listed in Mission Control order, across all displays. Full-screen app Spaces aren't included.

{
  "id": "6CCA3537-EFC2-B364-B65B-5CD6764C6456",
  "position": 2,
  "display": 1,
  "number": 2,
  "name": "Work",
  "customName": "Work",
  "color": "#30B35A",
  "icon": { "type": "symbol", "value": "briefcase.fill", "url": "/api/v1/spaces/6CCA3537-…/icon" },
  "current": true
}
FieldMeaning
idThe Space's permanent ID. It survives renames and reordering, so it's the best thing to save in other apps.
position1-based place across all displays. It matches macOS's “Switch to Desktop N”.
display, numberWhich display, and Apple's “Desktop N” on that display.
nameThe name shown everywhere: your custom name, or “Desktop N”.
customNameYour custom name, or null.
colorThe pill colour as #RRGGBB, or null for the default.
iconnull, or an object whose type is symbol (an SF Symbol name), emoji, or image (an imported image). url serves it as a 256 px PNG.
currenttrue for the desktop you're on.

Endpoints

GET/api/v1

API and app version, and a link to these docs. Handy for checking your token works.

GET/api/v1/status

The app version, licence state (licensed, trial or expired), the number of desktops, and currentSpace: a Space object, or null while a full-screen app is showing.

GET/api/v1/spaces

{"spaces": [Space, …]}, all desktops in order.

GET/api/v1/spaces/current

The current desktop. Returns 404 no_current_space while a full-screen app is showing.

GET/api/v1/spaces/{id}

One desktop, by its id.

GET/api/v1/spaces/{id}/icon

The desktop's icon as a 256 px PNG. SF Symbols are tinted to suit the pill colour. Returns 404 no_icon if it has none.

POST/api/v1/switch

Switch desktops. See Switching.

Switching

Send one of these as JSON in the body, or as query parameters:

ParameterExampleSwitches to
id{"id": "6CCA3537-…"}That desktop. Use this one: it's the most reliable.
number{"number": 3}The desktop at that position.
name{"name": "Design"}The first desktop with that name, ignoring upper and lower case.
direction{"direction": "next"}The next or previous desktop, wrapping round at the ends.
curl -X POST http://localhost:8787/api/v1/switch \
  -H "Authorization: Bearer sn_your_token_here" \
  -H "Content-Type: application/json" \
  -d '{"name": "Design"}'

Response:

{ "ok": true, "switched": true, "space": { …Space… } }

switched is false when you were already on that desktop.

Windows

Turn on Let apps see and bring forward windows in Settings › API to use these. It's off by default because it shows your window titles. Only windows on the current desktop are listed or can be brought forward. Minimised windows and hidden apps aren't included. SpaceNames needs Accessibility access for this, which it already uses for Mission Control names.

{
  "id": 1793,
  "app": "Safari",
  "bundleId": "com.apple.Safari",
  "pid": 912,
  "title": "Apple Developer",
  "front": true,
  "icon": "/api/v1/windows/1793/icon"
}
FieldMeaning
idThe window's number. It stays the same until the window closes.
app, bundleId, pidThe app that owns the window.
titleThe window's title. It can be empty.
fronttrue for the window in front: the one layouts act on.
iconThe app's icon as a 96 px PNG.
GET/api/v1/windows

{"windows": [Window, …]}, front to back.

GET/api/v1/windows/front

The window in front, or 404 window_not_found if there isn't one.

GET/api/v1/windows/{id}

One window, by its id.

GET/api/v1/windows/{id}/icon

The window's app icon as a PNG.

POST/api/v1/windows/{id}/focus

Brings the window to the front and makes its app active, like clicking on it. Nothing is moved or resized.

{ "ok": true, "window": { …Window… } }

Rectangle layouts

If you use Rectangle or Rectangle Pro, SpaceNames can ask it to arrange the window in front, for example into the left half, a third, or maximised. Rectangle does the arranging through its own URL commands, exactly as if you'd pressed one of its keyboard shortcuts. SpaceNames never moves windows itself.

Turn on Show Rectangle layouts on the phone in Settings › Phone Remote. The same switch turns on these endpoints. Rectangle needs to be running.

GET/api/v1/layouts

Whether the integration is on, which app is used, every layout you can send, and the ones chosen for the phone:

{
  "enabled": true,
  "app": "Rectangle Pro",
  "chosen": ["left-half", "right-half", "maximize"],
  "layouts": [
    { "id": "left-half", "title": "Left Half", "group": "Halves", "rect": [0, 0, 0.5, 1] },
    { "id": "restore", "title": "Restore", "group": "Size", "glyph": "restore" },
    …
  ]
}

rect is the part of the screen the layout fills (x, y, width, height, as fractions from the top left), handy for drawing a button. Layouts that don't fill a fixed area have a glyph instead.

POST/api/v1/layout

Arranges the front window. Send {"action": "left-half"}, or ?action=left-half.

Available layouts:

  • Halves: left-half, right-half, top-half, bottom-half, center-half.
  • Quarters: top-left, top-right, bottom-left, bottom-right.
  • Thirds: first-third, center-third, last-third, first-two-thirds, last-two-thirds.
  • Fourths: first-fourth, second-fourth, third-fourth, last-fourth, first-three-fourths, last-three-fourths.
  • Sixths: top-left-sixth, top-center-sixth, top-right-sixth, bottom-left-sixth, bottom-center-sixth, bottom-right-sixth.
  • Size: maximize, almost-maximize, maximize-height, center, smaller, larger, restore.
  • Displays: next-display, previous-display.

To arrange a particular window, bring it forward first:

curl -X POST -H "Authorization: Bearer $TOKEN" http://localhost:8787/api/v1/windows/1793/focus
curl -X POST -H "Authorization: Bearer $TOKEN" -d '{"action": "left-half"}' http://localhost:8787/api/v1/layout

Errors

Errors return an HTTP status and a JSON body:

{ "error": { "code": "space_not_found", "message": "No Space matches that request." } }
StatusCodeWhat to do
400bad_requestSomething's missing: for /switch, send one of id, number, name or direction; for /layout, send action.
400unknown_layoutThat isn't a layout name. GET /layouts lists them.
401unauthorizedThe token is missing or wrong. Copy it again from Settings › API.
403network_not_allowedThe request came from another device. Allow it in Settings › API, or call from the Mac itself.
403windows_disabledWindow access is off. Turn on “Let apps see and bring forward windows” in Settings › API.
403rectangle_disabledThe Rectangle integration is off. Turn it on in Settings › Phone Remote.
403licence_requiredThe free trial has ended. Enter a licence key in SpaceNames.
404api_disabledThe API is turned off in Settings › API.
404space_not_found, window_not_found, no_current_space, no_icon, not_foundNothing matched. Check the ID, name or address.
405method_not_allowedUse POST for /switch, /layout and /windows/{id}/focus.
409shortcut_offmacOS's “Switch to Desktop N” shortcut for that desktop is off. Turn it on in System Settings › Keyboard › Keyboard Shortcuts… › Mission Control.
409focus_failedmacOS didn't bring the window's app to the front. Try again, or click the window.
409accessibility_requiredAllow SpaceNames in System Settings › Privacy & Security › Accessibility.
409rectangle_not_installedInstall Rectangle or Rectangle Pro from rectangleapp.com.

If SpaceNames isn't running, or the API is off and the Phone Remote is too, the connection is simply refused.

Recipes

Terminal / shell scripts

TOKEN="sn_your_token_here"
# List desktops
curl -s -H "Authorization: Bearer $TOKEN" http://localhost:8787/api/v1/spaces
# Next desktop
curl -s -X POST -H "Authorization: Bearer $TOKEN" "http://localhost:8787/api/v1/switch?direction=next"

Shortcuts

Everything goes through the Get Contents of URL action. On the Mac, use http://localhost:8787. On an iPhone or iPad, use http://YourMacName.local:8787, turn on Allow other devices on this network in Settings › API, and allow Shortcuts to use the local network when iOS asks.

Example 1: Switch to a named desktop.

  1. Create a shortcut called Go to Design.
  2. Add Get Contents of URL, set to http://localhost:8787/api/v1/switch.
  3. Tap the arrow to show more options, then set Method to POST.
  4. Under Headers, add the key Authorization with the text Bearer sn_your_token_here.
  5. Set Request Body to JSON, then add a Text field with the key name and the value Design.

For next and previous, use the key direction with next or previous. Use number to switch by position.

Example 2: Choose a desktop from a menu.

  1. Get Contents of URL, set to http://localhost:8787/api/v1/spaces, with the same Authorization header. Leave the method as GET.
  2. Get Dictionary Value: get the Value for spaces in Contents of URL.
  3. Repeat with Each item in Dictionary Value. Inside the loop, add Get Dictionary Value: get the Value for name in Repeat Item.
  4. After the loop, add Choose from List with Repeat Results.
  5. Get Contents of URL, set to http://localhost:8787/api/v1/switch, method POST, the same header, and a JSON body with the key name set to Chosen Item.

Example 3: Say which desktop you're on.

  1. Get Contents of URL, set to http://localhost:8787/api/v1/spaces/current, with the Authorization header.
  2. Get Dictionary Value: get the Value for name in Contents of URL.
  3. Show Notification or Speak Text with Dictionary Value.

Example 4: Tile the front window with Rectangle. Turn on Rectangle layouts first.

  1. Create a shortcut called Left Half.
  2. Add Get Contents of URL, set to http://localhost:8787/api/v1/layout, method POST, with the Authorization header.
  3. Set Request Body to JSON with the key action and the value left-half. Any name from the layout list works.

Make a few of these, for example Left Half, Right Half and Maximise, and give each a keyboard shortcut. Run them with a keyboard shortcut, from the menu bar or with Siri, not from inside the Shortcuts editor, because otherwise the Shortcuts window is the one in front and that's the one that gets tiled.

Example 5: Choose a layout from a menu.

  1. Get Contents of URL, set to http://localhost:8787/api/v1/layouts, with the Authorization header.
  2. Get Dictionary Value: get the Value for chosen in Contents of URL. These are the layouts you ticked in Settings.
  3. Choose from List with Dictionary Value.
  4. Get Contents of URL, set to http://localhost:8787/api/v1/layout, method POST, the same header, and a JSON body with the key action set to Chosen Item.

Example 6: Go to a desktop and put Safari on the left. This needs window access turned on.

  1. Switch to the desktop as in Example 1, then add Wait for 1 second while the desktop slides in.
  2. Get Contents of URL, set to http://localhost:8787/api/v1/windows, with the Authorization header. Then add Get Dictionary Value: get the Value for windows.
  3. Repeat with Each item in Dictionary Value. Inside the loop, add Get Dictionary Value for app in Repeat Item, then If it is Safari:
    • Get Dictionary Value for id in Repeat Item.
    • Get Contents of URL, set to http://localhost:8787/api/v1/windows/Dictionary Value/focus, method POST, with the header.
    • Get Contents of URL, set to http://localhost:8787/api/v1/layout, method POST, with the header and a JSON body of action = left-half.

Every Safari window on that desktop goes to the left half. Use a different action inside the loop, or a second If, to put another app on the right.

Running them: on the Mac, open a shortcut's Details to give it a keyboard shortcut, add it to the menu bar, or pin it in Spotlight. You can also ask Siri by its name. On an iPhone, put it on the Home Screen or the Action button for a one-tap desktop switcher.

To keep the token in one place, put it in a Text action at the top of the shortcut and use that variable in the header.

Raycast script command

#!/bin/bash
# @raycast.schemaVersion 1
# @raycast.title Switch Space
# @raycast.mode silent
# @raycast.argument1 { "type": "text", "placeholder": "Space name" }
curl -s -X POST -H "Authorization: Bearer sn_your_token_here" -H "Content-Type: application/json" \
  -d "{\"name\": \"$1\"}" http://localhost:8787/api/v1/switch > /dev/null

Keyboard Maestro, BetterTouchTool, Stream Deck

Use an Execute Shell Script or Run Shell Script action with the curl line above. For Stream Deck, use any HTTP-request plugin, or the System › Open action pointed at a shell script.

JavaScript (Node or a local web page)

const api = (path, body) => fetch(`http://localhost:8787/api/v1${path}`, {
  method: body ? "POST" : "GET",
  headers: { Authorization: "Bearer sn_your_token_here", "Content-Type": "application/json" },
  body: body && JSON.stringify(body),
}).then((r) => r.json());

const { spaces } = await api("/spaces");
await api("/switch", { id: spaces[0].id });

Python

import requests
H = {"Authorization": "Bearer sn_your_token_here"}
spaces = requests.get("http://localhost:8787/api/v1/spaces", headers=H).json()["spaces"]
requests.post("http://localhost:8787/api/v1/switch", headers=H, json={"direction": "previous"})

Security

  • Treat the token like a password. Anyone who has it, and can reach your Mac, can switch your desktops, and, if you've turned them on, see window titles, bring windows forward and use Rectangle layouts. It can't do anything else.
  • Default access: only apps on this Mac can use the API. Allow other devices only if you need to, and only on networks you trust.
  • No encryption on the network. The API uses plain HTTP, so on a shared or public Wi-Fi, leave network access off.
  • Revoking access: choose New Token in Settings, or turn the API off.