HSWindow
TypeObject representing a window. You should not instantiate these directly, but rather, use the methods in hs.window to create them for you. Note that this type uses private macOS APIs
Properties
application
The application that owns this window
id
The window's underlying ID. A value of 0 or -1 likely means no window ID could be determined.
isStandard
Whether the window is an ordinary window (its subrole is AXStandardWindow), rather than
e.g. a dialog or a floating panel.
macOS reports minimized windows, and every window of a hidden application, as AXDialog
(see subrole). While a window is minimized or hidden, this counts it as standard if it can
be minimized, which dialogs can't. A standard window that can't be minimized either, such as
TextEdit's Settings window, can't be told apart from a dialog while its application is hidden,
so this is false for it until it's shown again. This reads macOS each time and doesn't
remember what a window was while it was shown; hs.windowfilter does.
role
The window's accessibility role, usually "AXWindow", or null if it can't be read. GNU
Emacs's windows report "AXTextField", but are given "AXWindow" here.
subrole
The window's accessibility subrole, which says what kind of window it is: e.g.
"AXStandardWindow" for an ordinary window, "AXDialog", "AXSystemDialog" or
"AXFloatingWindow". Null if the window has no subrole, or it can't be read.
This is what macOS says right now, which for a minimized window, or any window of a hidden
application, is always "AXDialog". isStandard allows for that, and so do the rules of
hs.windowfilter.
position
The window's position on screen, as an HSPoint
While the window is animating to a new frame, this is the position it's animating to. Setting it ends the animation first.
size
The window's size, as an HSSize
While the window is animating to a new frame, this is the size it's animating to. Setting it ends the animation first.
frame
The window's frame, as an HSRect
Setting frame, position or size briefly turns off the owning application's
AXEnhancedUserInterface, and restores it afterwards. Some applications (Chromium and Electron
ones in particular) otherwise animate the change, or end up at the wrong size.
Setting frame animates the window to its new frame over hs.window.animationDuration
seconds (0.2 unless you change it, 0 for no animation). Use moveAndResize() to give a
duration for one change. While the window is animating, frame is the frame it's animating
to, not wherever it has got to.
screen
The screen that contains the largest portion of this window.
spaces
The spaces this window is on. That's usually one, but windows set to appear on every
space are on all of them. Empty if Spaces are unavailable (see
hs.screen.spacesAvailable()).
Methods
moveAndResize(frame, duration) -> boolean
Move and resize the window, reporting whether it succeeded
The same as setting frame, except that the result tells you whether the window accepted
the change, and you can say how long to animate it for.
An animated change carries on after this returns. Changing the window's frame again while it
animates takes over from wherever it has got to, and setting its position or size,
minimizing it or making it fullscreen moves it straight to the frame it was animating to first.
moveAndResize(frame, duration) -> boolean
| Name | Type | Description |
|---|---|---|
| frame | HSRect | The window's new frame |
| duration optional | number | How many seconds to animate the change over. Leave it out to use hs.window.animationDuration, or pass 0 to change the frame straight away |
const win = hs.window.focusedWindow()
if (!win.moveAndResize(new HSRect(0, 0, 1024, 768))) {
console.log("The window refused to move")
}
// Glide the window to the top-left corner over half a second
win.moveAndResize(new HSRect(0, 0, 800, 600), 0.5)
moveToSpace(space) -> Promise<void>
Moves this window to another space, without switching to it. This works for any app's windows, and doesn't use Mission Control.
Only windows on a desktop can be moved, and only to a desktop - not to or from a fullscreen space. Windows set to appear on every space can't be moved either.
moveToSpace(space) -> Promise<void>
| Name | Type | Description |
|---|---|---|
| space | HSSpace | The desktop to move the window to |
const desktops = hs.screen.main().spaces.filter(s => s.type === "desktop")
hs.window.focusedWindow().moveToSpace(desktops[desktops.length - 1])
focus() -> boolean
Focus this window
The window becomes its application's main window, and the application is brought to the front with only this window raised. The application's other windows keep their place in the stacking order, including those on other displays.
The application finishes coming to the front after this returns, and some applications then bring
a different window forward, so this makes the window main again 0.3 seconds later. That is skipped
if another of the application's windows has been chosen in the meantime, or if focus() is called again.
focus() -> boolean
hs.window.focusedWindow() after a short delay if it matters.const wins = hs.window.allWindows()
wins[0].focus()
becomeMain() -> boolean
Make this window its application's main window
This does not activate the application. If the application is already frontmost, the window comes to the front.
becomeMain() -> boolean
const win = hs.window.allWindows()[0]
win.becomeMain()
minimize() -> boolean
Minimize this window
minimize() -> boolean
const win = hs.window.focusedWindow()
win.minimize()
unminimize() -> boolean
Unminimize this window
unminimize() -> boolean
const win = hs.window.allWindows()[0]
win.unminimize()
raise() -> boolean
Raise this window to the front
raise() -> boolean
const win = hs.window.allWindows()[0]
win.raise()
toggleFullscreen() -> boolean
Toggle fullscreen mode
toggleFullscreen() -> boolean
const win = hs.window.focusedWindow()
win.toggleFullscreen()
close() -> boolean
Close this window
close() -> boolean
const win = hs.window.focusedWindow()
win.close()
centerOnScreen() -> None
Center the window on the screen
centerOnScreen() -> None
const win = hs.window.focusedWindow()
win.centerOnScreen()
windowsToEast(options) -> HSWindow[]
The windows to the right of this one, nearest first
Windows straight ahead are preferred to nearer windows off to the side.
windowsToEast(options) -> HSWindow[]
| Name | Type | Description |
|---|---|---|
| options optional | object | null | How to choose the windows |
| windows optional | HSWindow[] | The windows to choose from, e.g. those allowed by a window filter. Defaults to every window on screen. Windows that aren't on screen are always ignored |
| frontmost optional | boolean | true to put windows that can be seen before windows hidden behind others |
| strict optional | boolean | true to only consider windows within 45° of the direction, rather than anywhere on that side of this window |
const next = hs.window.focusedWindow()?.windowsToEast()[0]
if (next) console.log("Next window east: " + next.title)
focusWindowEast(options) -> boolean
Focuses the nearest window to the right of this one
focusWindowEast(options) -> boolean
| Name | Type | Description |
|---|---|---|
| options optional | object | null | How to choose the windows |
| windows optional | HSWindow[] | The windows to choose from, e.g. those allowed by a window filter. Defaults to every window on screen. Windows that aren't on screen are always ignored |
| frontmost optional | boolean | true to put windows that can be seen before windows hidden behind others |
| strict optional | boolean | true to only consider windows within 45° of the direction, rather than anywhere on that side of this window |
true if a window was focused, false if there was none to focushs.hotkey.bind(["cmd", "ctrl"], "right", () => {
hs.window.focusedWindow()?.focusWindowEast({ frontmost: true })
})
windowsToWest(options) -> HSWindow[]
The windows to the left of this one, nearest first
Windows straight ahead are preferred to nearer windows off to the side.
windowsToWest(options) -> HSWindow[]
| Name | Type | Description |
|---|---|---|
| options optional | object | null | How to choose the windows |
| windows optional | HSWindow[] | The windows to choose from, e.g. those allowed by a window filter. Defaults to every window on screen. Windows that aren't on screen are always ignored |
| frontmost optional | boolean | true to put windows that can be seen before windows hidden behind others |
| strict optional | boolean | true to only consider windows within 45° of the direction, rather than anywhere on that side of this window |
const next = hs.window.focusedWindow()?.windowsToWest()[0]
if (next) console.log("Next window west: " + next.title)
focusWindowWest(options) -> boolean
Focuses the nearest window to the left of this one
focusWindowWest(options) -> boolean
| Name | Type | Description |
|---|---|---|
| options optional | object | null | How to choose the windows |
| windows optional | HSWindow[] | The windows to choose from, e.g. those allowed by a window filter. Defaults to every window on screen. Windows that aren't on screen are always ignored |
| frontmost optional | boolean | true to put windows that can be seen before windows hidden behind others |
| strict optional | boolean | true to only consider windows within 45° of the direction, rather than anywhere on that side of this window |
true if a window was focused, false if there was none to focushs.hotkey.bind(["cmd", "ctrl"], "left", () => {
hs.window.focusedWindow()?.focusWindowWest({ frontmost: true })
})
windowsToNorth(options) -> HSWindow[]
The windows above this one, nearest first
Windows straight ahead are preferred to nearer windows off to the side.
windowsToNorth(options) -> HSWindow[]
| Name | Type | Description |
|---|---|---|
| options optional | object | null | How to choose the windows |
| windows optional | HSWindow[] | The windows to choose from, e.g. those allowed by a window filter. Defaults to every window on screen. Windows that aren't on screen are always ignored |
| frontmost optional | boolean | true to put windows that can be seen before windows hidden behind others |
| strict optional | boolean | true to only consider windows within 45° of the direction, rather than anywhere on that side of this window |
const next = hs.window.focusedWindow()?.windowsToNorth()[0]
if (next) console.log("Next window north: " + next.title)
focusWindowNorth(options) -> boolean
Focuses the nearest window above this one
focusWindowNorth(options) -> boolean
| Name | Type | Description |
|---|---|---|
| options optional | object | null | How to choose the windows |
| windows optional | HSWindow[] | The windows to choose from, e.g. those allowed by a window filter. Defaults to every window on screen. Windows that aren't on screen are always ignored |
| frontmost optional | boolean | true to put windows that can be seen before windows hidden behind others |
| strict optional | boolean | true to only consider windows within 45° of the direction, rather than anywhere on that side of this window |
true if a window was focused, false if there was none to focushs.hotkey.bind(["cmd", "ctrl"], "up", () => {
hs.window.focusedWindow()?.focusWindowNorth({ frontmost: true })
})
windowsToSouth(options) -> HSWindow[]
The windows below this one, nearest first
Windows straight ahead are preferred to nearer windows off to the side.
windowsToSouth(options) -> HSWindow[]
| Name | Type | Description |
|---|---|---|
| options optional | object | null | How to choose the windows |
| windows optional | HSWindow[] | The windows to choose from, e.g. those allowed by a window filter. Defaults to every window on screen. Windows that aren't on screen are always ignored |
| frontmost optional | boolean | true to put windows that can be seen before windows hidden behind others |
| strict optional | boolean | true to only consider windows within 45° of the direction, rather than anywhere on that side of this window |
const next = hs.window.focusedWindow()?.windowsToSouth()[0]
if (next) console.log("Next window south: " + next.title)
focusWindowSouth(options) -> boolean
Focuses the nearest window below this one
focusWindowSouth(options) -> boolean
| Name | Type | Description |
|---|---|---|
| options optional | object | null | How to choose the windows |
| windows optional | HSWindow[] | The windows to choose from, e.g. those allowed by a window filter. Defaults to every window on screen. Windows that aren't on screen are always ignored |
| frontmost optional | boolean | true to put windows that can be seen before windows hidden behind others |
| strict optional | boolean | true to only consider windows within 45° of the direction, rather than anywhere on that side of this window |
true if a window was focused, false if there was none to focushs.hotkey.bind(["cmd", "ctrl"], "down", () => {
hs.window.focusedWindow()?.focusWindowSouth({ frontmost: true })
})
snapshot(keepTransparency) -> Promise<HSImage>
Capture the current on-screen contents of this window as an image.
Requires Screen Recording permission.
snapshot(keepTransparency) -> Promise<HSImage>
| Name | Type | Description |
|---|---|---|
| keepTransparency optional | boolean | Whether to preserve the window's alpha channel. If false (the default), transparent regions are filled with an opaque black background. |
const win = hs.window.focusedWindow()
win.snapshot().then(img => img.saveToFile("/tmp/window.png"))
axElement() -> HSAXElement
Get the underlying AXElement
axElement() -> HSAXElement
const win = hs.window.focusedWindow()
const ax = win.axElement()
toJSON() -> object
Returns a plain object describing this window, for JSON serialisation
JSON.stringify() calls this automatically, so you rarely need to call it yourself.
toJSON() -> {id: number, pid: number, title: string | null, application: string | null, frame: HSRect | null, isMinimized: boolean, isFullscreen: boolean, isStandard: boolean, role: string | null, subrole: string | null}
application, which is the name of the window's application. Values that can't be read are null| Field | Type | Description |
|---|---|---|
| id | number | |
| pid | number | |
| title | string | null | |
| application | string | null | |
| frame | HSRect | null | |
| isMinimized | boolean | |
| isFullscreen | boolean | |
| isStandard | boolean | |
| role | string | null | |
| subrole | string | null |
console.log(JSON.stringify(hs.window.focusedWindow()))