HSUIWindow allows you to create custom windows with a SwiftUI-like
declarative syntax. Build interfaces using shapes, text, images, and layout containers.
** Note: ** A shown window stays on screen, with its callbacks, until it's hidden or destroyed,
even if you don't keep a reference to it. Clicking the macOS close button only hides the
window (firing the onHide() callback, if you have one configured) — it does not destroy the
window while you hold a reference to it in JavaScript, so you can show it again. Call
destroy() explicitly (for example from within an onHide() handler) if you want to release
it sooner. See onShow(), onHide(), and onDestroy() below for the full set of lifecycle
callbacks.
Building UI Elements
Shapes: rectangle(), circle()
Text: text(content)
Buttons: button(label) — uses SwiftUI's native Button for press-state feedback
Set a callback to fire when the window is hidden
Fires when `hide()` is called, **and** when the user clicks the macOS close
button — clicking that button only hides the window from Hammerspoon's
perspective (see the class-level note above), so this is the callback that reacts
to it. Does not fire when the window is destroyed via `destroy()` — use
`onDestroy()` for that.
Declaration
onHide(callback) -> HSUIWindow
Parameters
Name
Type
Description
callback
function
A JavaScript function called after the window is hidden
Returns
HSUIWindow
Self for chaining
Example
const win = hs.ui.window({x: 100, y: 100, w: 300, h: 200})
.text("Hello")
.onHide(() => console.log("Window hidden (via hide() or the close button)"))
.show()
Set a callback to fire after the window is destroyed via `destroy()`
Only fires when `destroy()` is called explicitly — whether directly, or from
within an `onHide()` handler. It does **not** fire when the user clicks the macOS
close button by itself; that only hides the window, so use `onHide()` to react to
the button click, and call `destroy()` from that handler if you also want to
release the window.
Declaration
onDestroy(callback) -> HSUIWindow
Parameters
Name
Type
Description
callback
function
A JavaScript function called after the window is destroyed
Show or hide the window's title bar
By default windows have a title bar. Pass `false` to create a borderless window.
`.closable()`, `.miniaturizable()`, and `.allowResize()` only take visual effect
when the window is titled.
Set how the window behaves with Spaces, fullscreen apps and window cycling, from a single behavior name
`"fullScreenAuxiliary"` lets it appear over another app's fullscreen Space
Use `behaviorList()` to combine several. An unknown name is logged and leaves the behavior unchanged.
Can be called before or after `show()`.
Set how the window behaves with Spaces, fullscreen apps and window cycling, from a combination of behavior names
`"canJoinAllSpaces"` with `"moveToActiveSpace"`; any two of `"managed"`, `"transient"` and `"stationary"`;
`"participatesInCycle"` with `"ignoresCycle"`; any two of the `"fullScreen…"` Primary/Auxiliary/None names;
and `"fullScreenAllowsTiling"` with `"fullScreenDisallowsTiling"`. Combining them throws, leaving the
behavior unchanged. Unknown names are logged and ignored. Can be called before or after `show()`.
Declaration
behaviorList(names) -> HSUIWindow
Parameters
Name
Type
Description
names
string[]
The behavior names to combine, or an empty array for the system default
Returns
HSUIWindow
Self for chaining
Throws
Throws an Error on failure; wrap calls in try/catch to handle it.
Example
// An alert that appears above everything, including fullscreen apps, on every Space
hs.ui.window({x: 100, y: 100, w: 400, h: 200})
.level("screenSaver")
.behaviorList(["canJoinAllSpaces", "fullScreenAuxiliary"])
.text("Meeting starts now")
.show()
Add a video element
Renders a SwiftUI `VideoPlayer` for the given `HSVideo`. Keep a reference to the
`HSVideo` object to control playback (`play()`, `pause()`, `seek()`, `volume`) after
the window is shown.
Declaration
video(videoValue) -> HSUIWindow
Parameters
Name
Type
Description
videoValue
HSVideo
Video as an HSVideo object
Returns
HSUIWindow
Self for chaining (apply modifiers like `frame()`, `opacity()`)
Embed a web browser element created with `hs.ui.webview()` (macOS 26+)
The element fills the available space in the window layout.
Keep a reference to the element to call navigation methods after the window is shown.