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
- Open SpaceNames › Settings… › API.
- Turn on Let other apps switch desktops.
- Copy the token. You can also copy a ready-made
curlexample.
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_hereheader. - A
?token=sn_your_token_herequery 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
}
| Field | Meaning |
|---|---|
id | The Space's permanent ID. It survives renames and reordering, so it's the best thing to save in other apps. |
position | 1-based place across all displays. It matches macOS's “Switch to Desktop N”. |
display, number | Which display, and Apple's “Desktop N” on that display. |
name | The name shown everywhere: your custom name, or “Desktop N”. |
customName | Your custom name, or null. |
color | The pill colour as #RRGGBB, or null for the default. |
icon | null, 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. |
current | true for the desktop you're on. |
Endpoints
/api/v1API and app version, and a link to these docs. Handy for checking your token works.
/api/v1/statusThe 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.
/api/v1/spaces{"spaces": [Space, …]}, all desktops in order.
/api/v1/spaces/currentThe current desktop. Returns 404 no_current_space while a full-screen app is showing.
/api/v1/spaces/{id}One desktop, by its id.
/api/v1/spaces/{id}/iconThe 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.
/api/v1/switchSwitch desktops. See Switching.
Switching
Send one of these as JSON in the body, or as query parameters:
| Parameter | Example | Switches 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"
}
| Field | Meaning |
|---|---|
id | The window's number. It stays the same until the window closes. |
app, bundleId, pid | The app that owns the window. |
title | The window's title. It can be empty. |
front | true for the window in front: the one layouts act on. |
icon | The app's icon as a 96 px PNG. |
/api/v1/windows{"windows": [Window, …]}, front to back.
/api/v1/windows/frontThe window in front, or 404 window_not_found if there isn't one.
/api/v1/windows/{id}One window, by its id.
/api/v1/windows/{id}/iconThe window's app icon as a PNG.
/api/v1/windows/{id}/focusBrings 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.
/api/v1/layoutsWhether 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.
/api/v1/layoutArranges 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." } }
| Status | Code | What to do |
|---|---|---|
| 400 | bad_request | Something's missing: for /switch, send one of id, number, name or direction; for /layout, send action. |
| 400 | unknown_layout | That isn't a layout name. GET /layouts lists them. |
| 401 | unauthorized | The token is missing or wrong. Copy it again from Settings › API. |
| 403 | network_not_allowed | The request came from another device. Allow it in Settings › API, or call from the Mac itself. |
| 403 | windows_disabled | Window access is off. Turn on “Let apps see and bring forward windows” in Settings › API. |
| 403 | rectangle_disabled | The Rectangle integration is off. Turn it on in Settings › Phone Remote. |
| 403 | licence_required | The free trial has ended. Enter a licence key in SpaceNames. |
| 404 | api_disabled | The API is turned off in Settings › API. |
| 404 | space_not_found, window_not_found, no_current_space, no_icon, not_found | Nothing matched. Check the ID, name or address. |
| 405 | method_not_allowed | Use POST for /switch, /layout and /windows/{id}/focus. |
| 409 | shortcut_off | macOS's “Switch to Desktop N” shortcut for that desktop is off. Turn it on in System Settings › Keyboard › Keyboard Shortcuts… › Mission Control. |
| 409 | focus_failed | macOS didn't bring the window's app to the front. Try again, or click the window. |
| 409 | accessibility_required | Allow SpaceNames in System Settings › Privacy & Security › Accessibility. |
| 409 | rectangle_not_installed | Install 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.
- Create a shortcut called Go to Design.
- Add Get Contents of URL, set to
http://localhost:8787/api/v1/switch. - Tap the arrow to show more options, then set Method to
POST. - Under Headers, add the key
Authorizationwith the textBearer sn_your_token_here. - Set Request Body to
JSON, then add a Text field with the keynameand the valueDesign.
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.
- Get Contents of URL, set to
http://localhost:8787/api/v1/spaces, with the sameAuthorizationheader. Leave the method asGET. - Get Dictionary Value: get the Value for
spacesin Contents of URL. - Repeat with Each item in Dictionary Value. Inside the loop, add Get Dictionary Value: get the Value for
namein Repeat Item. - After the loop, add Choose from List with Repeat Results.
- Get Contents of URL, set to
http://localhost:8787/api/v1/switch, methodPOST, the same header, and a JSON body with the keynameset to Chosen Item.
Example 3: Say which desktop you're on.
- Get Contents of URL, set to
http://localhost:8787/api/v1/spaces/current, with theAuthorizationheader. - Get Dictionary Value: get the Value for
namein Contents of URL. - Show Notification or Speak Text with Dictionary Value.
Example 4: Tile the front window with Rectangle. Turn on Rectangle layouts first.
- Create a shortcut called Left Half.
- Add Get Contents of URL, set to
http://localhost:8787/api/v1/layout, methodPOST, with theAuthorizationheader. - Set Request Body to
JSONwith the keyactionand the valueleft-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.
- Get Contents of URL, set to
http://localhost:8787/api/v1/layouts, with theAuthorizationheader. - Get Dictionary Value: get the Value for
chosenin Contents of URL. These are the layouts you ticked in Settings. - Choose from List with Dictionary Value.
- Get Contents of URL, set to
http://localhost:8787/api/v1/layout, methodPOST, the same header, and a JSON body with the keyactionset to Chosen Item.
Example 6: Go to a desktop and put Safari on the left. This needs window access turned on.
- Switch to the desktop as in Example 1, then add Wait for 1 second while the desktop slides in.
- Get Contents of URL, set to
http://localhost:8787/api/v1/windows, with theAuthorizationheader. Then add Get Dictionary Value: get the Value forwindows. - Repeat with Each item in Dictionary Value. Inside the loop, add Get Dictionary Value for
appin Repeat Item, then If it isSafari:- Get Dictionary Value for
idin Repeat Item. - Get Contents of URL, set to
http://localhost:8787/api/v1/windows/Dictionary Value/focus, methodPOST, with the header. - Get Contents of URL, set to
http://localhost:8787/api/v1/layout, methodPOST, with the header and a JSON body ofaction=left-half.
- Get Dictionary Value for
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.