API Docs

Inspect and control the displays attached to the system.

Obtaining screens

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

Navigation

const right = hs.screen.main().toEast();
if (right) console.log("Screen to the right:", right.name);

Display modes

const s = hs.screen.primary();
console.log(s.mode);
// → { width: 1440, height: 900, scale: 2, frequency: 60 }

s.setMode(1920, 1080, 1, 60);

Screenshots

const img = await hs.screen.main().snapshot();
img.saveToFile("/tmp/screen.png");

Watching for display changes

hs.screen.addWatcher(() => {
    console.log("Display configuration changed:", hs.screen.all().length, "screens");
});

Types

This module provides the following types:

Properties

This module has no properties.

Methods

hs.screen.all() -> HSScreen[]

All connected screens.
hs.screen.all() -> HSScreen[]
HSScreen[]
An array of HSScreen objects
const screens = hs.screen.all()
screens.forEach(s => console.log(s.name))

hs.screen.main() -> HSScreen

The screen that currently contains the focused window, or the screen with the keyboard focus if no window is focused.
hs.screen.main() -> HSScreen
HSScreen
An HSScreen object or `null` if no main screen can be determined.
const main = hs.screen.main()
console.log(main && main.name)

hs.screen.primary() -> HSScreen

The primary display — the one that contains the global menu bar.
hs.screen.primary() -> HSScreen
HSScreen
An HSScreen object or `null` if no primary screen can be determined.
const s = hs.screen.primary()
console.log(s && s.frame)

hs.screen.addWatcher(listener) -> None

Registers a listener that fires whenever the display configuration changes — monitors connected/disconnected, resolution or arrangement changed, or the menu bar moved to a different display. The listener receives no arguments; call `all()`/`main()`/`primary()` inside the callback to inspect the new configuration. The OS notification subscription starts lazily on the first listener and is released automatically when the last listener is removed.
hs.screen.addWatcher(listener) -> None
Name Type Description
listener function A function called with no arguments when the display configuration changes.
None
hs.screen.addWatcher(() => {
    console.log("Screens changed, now: " + hs.screen.all().length)
})

hs.screen.removeWatcher(listener) -> None

Removes a previously registered display-configuration listener.
hs.screen.removeWatcher(listener) -> None
Name Type Description
listener function The function originally passed to `addWatcher`.
None
const handler = () => console.log("screens changed")
hs.screen.addWatcher(handler)
hs.screen.removeWatcher(handler)