hs.window
ModuleModule for interacting with windows
Types
This module provides the following types:
Properties
hs.window.allWindowsIgnore
A list of bundle identifiers to ignore when fetching a list of all windows
hs.window.animationDuration
How many seconds changes to a window's frame are animated over, when they don't give a
duration of their own. Defaults to 0.2, like Hammerspoon 1. Set it to 0 to change frames
straight away.
This applies to setting a window's frame, to moveAndResize() without a duration, and to
the layout functions such as hs.window.moveToLeftHalf(). Setting a window's position or
size is never animated. Windows are moved a step at a time through the Accessibility API,
so an application that's slow to respond, or that redraws slowly, animates less smoothly.
Methods
hs.window.focusedWindow() -> HSWindow
Get the currently focused window
This is the window with keyboard focus, which isn't always a window of hs.application.frontmost():
panels that take focus without activating their app (such as Spotlight's search panel) are returned too.
hs.window.focusedWindow() -> HSWindow
const win = hs.window.focusedWindow()
console.log(win && win.title)
hs.window.frontmostWindow() -> HSWindow
Get the frontmost window
This is the focused window or, if no window has focus (for example when the desktop is clicked, or the
active application has no windows open), the window in front of all the others, from orderedWindows().
hs.window.frontmostWindow() -> HSWindow
const win = hs.window.frontmostWindow()
console.log(win && win.title)
hs.window.forID(id) -> HSWindow
Get the window with the given ID
This is much faster than searching allWindows() for the ID, because it only asks the
application that owns the window for its windows. Like allWindows(), this can find
minimized windows, windows of hidden applications, and windows in native fullscreen and on
other Spaces, with the same limitation.
hs.window.forID(id) -> HSWindow
| Name | Type | Description |
|---|---|---|
| id | number | The window's ID (see the id property on hs.window objects) |
const id = hs.window.focusedWindow().id
// ...later
const win = hs.window.forID(id)
if (win) win.focus()
hs.window.allWindows() -> HSWindow[]
Get all windows from all applications
This includes windows in native fullscreen and on other Spaces, with one limitation: macOS only reveals a window on a Space that isn't visible if its app has already handed that window to an accessibility client, e.g. because Hammerspoon listed it while its Space was visible. A window that no accessibility client has looked at since it was created is missing until its Space is next visible, unless it's its app's main or focused window, which are always included.
hs.window.allWindows() -> HSWindow[]
const wins = hs.window.allWindows()
console.log(wins.length)
hs.window.visibleWindows() -> HSWindow[]
Get all visible (not minimized) windows
hs.window.visibleWindows() -> HSWindow[]
const vis = hs.window.visibleWindows()
hs.window.windowsForApp(app) -> HSWindow[]
Get windows for a specific application
Like allWindows(), this includes windows in native fullscreen and on other Spaces, with the
same limitation.
hs.window.windowsForApp(app) -> HSWindow[]
| Name | Type | Description |
|---|---|---|
| app | HSApplication | An HSApplication object |
app is nullconst app = hs.application.frontmost()
const wins = hs.window.windowsForApp(app)
hs.window.windowsOnScreen(screenIndex) -> HSWindow[]
Get all windows on a specific screen
hs.window.windowsOnScreen(screenIndex) -> HSWindow[]
| Name | Type | Description |
|---|---|---|
| screenIndex | number | The screen index (0 for main screen) |
const wins = hs.window.windowsOnScreen(0)
hs.window.windowAtPoint(point) -> HSWindow
Get the window at a specific screen position
hs.window.windowAtPoint(point) -> HSWindow
| Name | Type | Description |
|---|---|---|
| point | HSPoint | An HSPoint containing the coordinates |
const win = hs.window.windowAtPoint({x: 500, y: 300})
hs.window.orderedWindows() -> HSWindow[]
Get the windows on screen, ordered front to back
The order is the WindowServer's stacking order across all displays, so the first window is
the one in front of all the others, whichever application owns it. Only windows on the
currently visible Space(s) are included: minimized windows, windows of hidden applications
and windows on other Spaces have no place in the stacking order, so use allWindows() for those.
hs.window.orderedWindows() -> HSWindow[]
const wins = hs.window.orderedWindows()
hs.window.snapshotForID(id, keepTransparency) -> Promise<HSImage>
Capture the current on-screen contents of the window with the given ID.
Requires Screen Recording permission.
hs.window.snapshotForID(id, keepTransparency) -> Promise<HSImage>
| Name | Type | Description |
|---|---|---|
| id | number | The window's underlying ID (see the id property on hs.window objects). |
| keepTransparency optional | boolean | Whether to preserve the window's alpha channel. If false (the default), transparent regions are filled with an opaque black background. |
hs.window.snapshotForID(12345).then(img => img.saveToFile("/tmp/window.png"))
hs.window.spacesForID(id) -> HSSpace[]
The spaces the window with the given ID is on. This is the same as an HSWindow's
spaces, for windows you only have the ID of, such as those from HSSpace.windowIDs()
(Hammerspoon can't make HSWindow objects for windows on other spaces).
hs.window.spacesForID(id) -> HSSpace[]
| Name | Type | Description |
|---|---|---|
| id | number | The window's ID |
for (const id of hs.screen.allSpaces()[0].windowIDs()) {
console.log(id + " is on " + hs.window.spacesForID(id).length + " space(s)")
}
hs.window.moveToSpaceForID(id, space) -> Promise<void>
Moves the window with the given ID to another space, without switching to it. This is the
same as an HSWindow's moveToSpace(), for windows you only have the ID of, such as
those from HSSpace.windowIDs().
hs.window.moveToSpaceForID(id, space) -> Promise<void>
| Name | Type | Description |
|---|---|---|
| id | number | The window's ID |
| space | HSSpace | The desktop to move the window to |
// Gather every window from the main screen's other desktops onto its current space
const screen = hs.screen.main()
const here = screen.currentSpace
for (const space of screen.spaces) {
if (space.id === here.id || space.type !== "desktop") continue
for (const id of space.windowIDs()) hs.window.moveToSpaceForID(id, here)
}
hs.window.findByTitle(title) -> HSWindow[]
Find windows by title Parameter title: The window title to search for. All windows with titles that include this string, will be matched
hs.window.findByTitle(title) -> HSWindow[]
| Name | Type | Description |
|---|---|---|
| title | any | The window title to search for. All windows with titles that include this string, will be matched |
hs.window.currentWindows() -> HSWindow[]
Get all windows for the current application
hs.window.currentWindows() -> HSWindow[]
hs.window.moveToLeftHalf(win) -> boolean
Move a window to left half of screen Parameter win: An HSWindow object
hs.window.moveToLeftHalf(win) -> boolean
| Name | Type | Description |
|---|---|---|
| win | any | An HSWindow object |
hs.window.moveToRightHalf(win) -> boolean
Move a window to right half of screen Parameter win: An HSWindow object
hs.window.moveToRightHalf(win) -> boolean
| Name | Type | Description |
|---|---|---|
| win | any | An HSWindow object |
hs.window.maximize(win) -> boolean
Maximize a window Parameter win: An HSWindow object
hs.window.maximize(win) -> boolean
| Name | Type | Description |
|---|---|---|
| win | any | An HSWindow object |