HSScreen
TypeAn object representing a single display attached to the system.
Coordinate system
All geometry is returned in Hammerspoon screen coordinates: the origin (0, 0)
is at the top-left of the primary display, and y increases downward.
This matches Hammerspoon v1 and is the inverse of the raw macOS/CoreGraphics convention.
Examples
const s = hs.screen.main();
console.log(s.name); // e.g. "Built-in Retina Display"
console.log(s.frame.w); // usable width in points
console.log(s.mode.width, s.mode.scale); // e.g. 1440, 2
s.desktopImage = "/Users/me/wallpaper.jpg";
Properties
fullFrame
The full screen area in Hammerspoon coordinates, including menu bar and Dock regions.
mode
The currently active display mode.
An object with keys: width, height, scale, frequency.
availableModes
All display modes supported by this screen.
Each element has keys: width, height, scale, frequency.
rotation
The current screen rotation in degrees (0, 90, 180, or 270).
Assign one of 0, 90, 180, or 270 to rotate the display.
desktopImage
The URL string of the current desktop background image for this screen, or null.
Assign a new absolute file path or file:// URL string to change the wallpaper.
ambientLight
The ambient light level measured by this display's built-in sensor, in lux.
Returns null if the display does not have an ambient light sensor or if the
reading is currently unavailable.
spaces
The screen's spaces (its desktops and fullscreen apps), in the order Mission Control shows
them. Empty if Spaces are unavailable (see hs.screen.spacesAvailable()).
This relies on "Displays have separate Spaces" being turned on in System Settings → Desktop & Dock, which is macOS's default. If it's off, all screens share one set of spaces, so every screen reports the same ones.
Methods
setMode(width, height, scale, frequency) -> boolean
Switch to the given display mode.
Pass 0 for scale or frequency to match any value.
setMode(width, height, scale, frequency) -> boolean
| Name | Type | Description |
|---|---|---|
| width | number | Horizontal resolution in pixels. |
| height | number | Vertical resolution in pixels. |
| scale | number | Backing scale factor (e.g. 2 for HiDPI, 1 for non-HiDPI). Pass 0 to ignore. |
| frequency | number | Refresh rate in Hz. Pass 0 to ignore. |
true on success.const s = hs.screen.primary()
s.setMode(1920, 1080, 1, 60)
snapshot() -> Promise<HSImage>
Capture the current contents of this screen as an image.
Requires Screen Recording permission.
snapshot() -> Promise<HSImage>
hs.screen.main().snapshot().then(img => img.saveToFile("/tmp/screen.png"))
next() -> HSScreen
The next screen in hs.screen.all() order, wrapping around.
next() -> HSScreen
const next = hs.screen.main().next()
previous() -> HSScreen
The previous screen in hs.screen.all() order, wrapping around.
previous() -> HSScreen
const prev = hs.screen.main().previous()
toEast() -> HSScreen
The nearest screen whose left edge is at or beyond this screen's right edge, or null.
toEast() -> HSScreen
const right = hs.screen.main().toEast()
toWest() -> HSScreen
The nearest screen whose right edge is at or before this screen's left edge, or null.
toWest() -> HSScreen
const left = hs.screen.main().toWest()
toNorth() -> HSScreen
The nearest screen that is physically above this screen, or null.
toNorth() -> HSScreen
const above = hs.screen.main().toNorth()
toSouth() -> HSScreen
The nearest screen that is physically below this screen, or null.
toSouth() -> HSScreen
const below = hs.screen.main().toSouth()
setOrigin(x, y) -> boolean
Move this screen so its top-left corner is at the given position in global Hammerspoon coordinates.
setOrigin(x, y) -> boolean
| Name | Type | Description |
|---|---|---|
| x | number | The X coordinate to move to |
| y | number | The Y coordinate to move to |
true on success.const s = hs.screen.primary()
s.setOrigin(0, 0)
setPrimary() -> boolean
Designate this screen as the primary display (moves the menu bar here).
setPrimary() -> boolean
true on success.hs.screen.all()[1].setPrimary()
mirrorOf(screen) -> boolean
Configure this screen to mirror another screen.
mirrorOf(screen) -> boolean
| Name | Type | Description |
|---|---|---|
| screen | HSScreen | The screen to mirror. |
true on success.const all = hs.screen.all()
all[1].mirrorOf(all[0])
mirrorStop() -> boolean
Stop mirroring, restoring this screen to an independent display.
mirrorStop() -> boolean
true on success.hs.screen.all()[1].mirrorStop()
absoluteToLocal(rect) -> HSRect
Convert a rect in global Hammerspoon coordinates to coordinates local to this screen.
The result origin is relative to this screen's top-left corner.
absoluteToLocal(rect) -> HSRect
| Name | Type | Description |
|---|---|---|
| rect | HSRect | An HSRect in global Hammerspoon coordinates. |
null if the input is invalid.const s = hs.screen.main()
const local = s.absoluteToLocal({x: 1000, y: 500, w: 200, h: 100})
localToAbsolute(rect) -> HSRect
Convert a rect in local screen coordinates to global Hammerspoon coordinates.
localToAbsolute(rect) -> HSRect
| Name | Type | Description |
|---|---|---|
| rect | HSRect | An HSRect relative to this screen's top-left corner. |
null if the input is invalid.const s = hs.screen.main()
const abs = s.localToAbsolute({x: 10, y: 10, w: 100, h: 100})
getBrightness() -> NSNumber
The current brightness of this display, from 0.0 (darkest) to 1.0 (brightest).
Returns null if the display does not support software brightness control (e.g.
most third-party monitors, which are controlled via DDC rather than software).
getBrightness() -> NSNumber
0.0 to 1.0, or null if unsupported.const level = hs.screen.main().getBrightness()
if (level !== null) console.log("Brightness: " + level)
setBrightness(brightness) -> boolean
Set the brightness of this display.
setBrightness(brightness) -> boolean
| Name | Type | Description |
|---|---|---|
| brightness | number | The desired brightness, from 0.0 (darkest) to 1.0 (brightest). |
true on success, false if the display does not support software brightness control.hs.screen.main().setBrightness(0.5)
addSpace() -> Promise<HSSpace>
Adds a desktop to the end of this screen's spaces, by opening Mission Control and clicking its add button. Needs Accessibility permission.
addSpace() -> Promise<HSSpace>
hs.screen.main().addSpace()
.then(space => space.goto())
.catch(err => console.error(err))
toJSON() -> {[key: string]: any}
Returns a plain object describing this screen, for JSON serialisation
JSON.stringify() calls this automatically, so you rarely need to call it yourself.
toJSON() -> {[key: string]: any}
id, name, uuid, frame, fullFrame, position, mode and rotationconsole.log(JSON.stringify(hs.screen.all(), null, 2))