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
id
The window's underlying ID. A value of 0 or -1 likely means no window ID could be determined.
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.
Methods
moveAndResize(frame) -> 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.
moveAndResize(frame) -> boolean
| Name | Type | Description |
|---|---|---|
| frame | HSRect | The window's new frame |
const win = hs.window.focusedWindow()
if (!win.moveAndResize(new HSRect(0, 0, 1024, 768))) {
console.log("The window refused to move")
}
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.
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
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()
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 | 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() -> {[key: string]: any}
Returns a plain object describing this window, for JSON serialisation
JSON.stringify() calls this automatically, so you rarely need to call it yourself.
toJSON() -> {[key: string]: any}
id, pid, title, application (the owning app's title), frame, isMinimized, isFullscreen and isStandard. Values that can't be read are nullconsole.log(JSON.stringify(hs.window.focusedWindow()))