API Docs

A window filter: a set of rules choosing some of the windows on the system.

Get the windows it allows with windows(), or listen for windows being created, moved, focused etc. with on(). Create one with hs.windowfilter.create().

Events

Listeners are called with the window, its application's name, and the event's name. They're only told about windows the filter allows, or has just stopped allowing (so they hear why it stopped).

  • windowCreated, windowDestroyed. A destroyed window has gone, so only its id can still be read (and its application's name is passed to the listener).
  • windowMoved: moved or resized, including going fullscreen
  • windowFullscreened, windowUnfullscreened
  • windowMinimized, windowUnminimized
  • windowHidden, windowUnhidden: its application was hidden or unhidden
  • windowVisible, windowNotVisible: it's visible when it's neither minimized nor hidden
  • windowInCurrentSpace, windowNotInCurrentSpace: whether it's on a Space currently shown. Minimized and hidden windows count as being in every Space.
  • windowOnScreen, windowNotOnScreen: whether it's actually on screen: visible, and in a Space currently shown
  • windowFocused, windowUnfocused
  • windowTitleChanged

Five more events are about the filter's set of allowed windows:

  • windowAllowed: a window is now allowed. Emitted before any other event about it, e.g. before windowCreated for a new window.
  • windowRejected: a window is no longer allowed, or was destroyed. Emitted after the events that caused it.
  • hasWindow: the filter allows a window, having allowed none
  • hasNoWindows: the filter allows no windows any more. The listener gets the last one.
  • windowsChanged: the set of allowed windows changed. The listener gets one of the allowed windows, or null if there are none.

Moves, resizes and title changes are reported once the window has stopped changing for a moment, so dragging a window emits one windowMoved, not hundreds.

Properties

rules

JSValue

This filter's rules, as an object hs.windowfilter.create() and setRules() accept, or null for a filter made from a function

sortOrder

string

How windows() sorts windows when not told otherwise: "focusedLast" (most recently focused first, the default), "focused", "createdLast" (newest first) or "created". Windows that existed before Hammerspoon started watching count as older than any created since, in order of their IDs, and as focused in their order on screen, front to back.

Methods

windows(sortOrder) -> HSWindow[]

The windows this filter allows, sorted

windows(sortOrder) -> HSWindow[]
Name Type Description
sortOrder optional "focusedLast" | "focused" | "createdLast" | "created" How to sort them: most or least recently focused first, or newest or oldest first. Defaults to the filter's sortOrder
The windows
const wf = hs.windowfilter.create({ default: { visible: true, currentSpace: true } })
for (const win of wf.windows()) console.log(win.title)

isWindowAllowed(window) -> boolean

Whether this filter allows a window

isWindowAllowed(window) -> boolean
Name Type Description
window HSWindow The window
boolean
true if the window is allowed
const win = hs.window.focusedWindow()
if (win && hs.windowfilter.defaultFilter.isWindowAllowed(win)) console.log("The focused window counts")

isAppAllowed(app) -> boolean

Whether this filter could allow any of an application's windows: false if the application is rejected outright

isAppAllowed(app) -> boolean
Name Type Description
app string The application's name or bundle ID
boolean
false if none of the application's windows can be allowed
console.log(hs.windowfilter.defaultFilter.isAppAllowed("Spotlight"))  // false

setRules(rules) -> HSWindowFilter

Replaces all of this filter's rules

setRules(rules) -> HSWindowFilter
Name Type Description
rules object The rules, as for hs.windowfilter.create()
This filter
Throws an Error on failure; wrap calls in try/catch to handle it.
try {
    wf.setRules({ default: false, apps: { Safari: true } })
} catch (err) {
    console.error(err.message)
}

setAppRule(app, rule) -> HSWindowFilter

Sets the rule for one application's windows, which replaces the default rule for them

setAppRule(app, rule) -> HSWindowFilter
Name Type Description
app string The application's name or bundle ID
rule boolean | object | null true for its visible windows, false for none, an object of conditions (as for the default rule of hs.windowfilter.create()), or null to remove the application's rule so the default applies again
This filter
Throws an Error on failure; wrap calls in try/catch to handle it.
try {
    wf.setAppRule("com.apple.Safari", { visible: true, allowTitles: 1 })
} catch (err) {
    console.error(err.message)
}

setDefaultRule(rule) -> HSWindowFilter

Sets the rule for applications that have no rule of their own

setDefaultRule(rule) -> HSWindowFilter
Name Type Description
rule boolean | object | null true for visible windows, false for none, an object of conditions, or null for every window with a standard role
This filter
Throws an Error on failure; wrap calls in try/catch to handle it.
try {
    wf.setDefaultRule({ visible: true, fullscreen: false })
} catch (err) {
    console.error(err.message)
}

setOverrideRule(rule) -> HSWindowFilter

Sets the rule every window must pass, whatever its application's rule says

setOverrideRule(rule) -> HSWindowFilter
Name Type Description
rule boolean | object | null true for visible windows only, false for none, an object of conditions, or null for no override rule
This filter
Throws an Error on failure; wrap calls in try/catch to handle it.
try {
    wf.setOverrideRule({ currentSpace: true })
} catch (err) {
    console.error(err.message)
}

allowApp(app) -> HSWindowFilter

Allows an application's visible windows. The same as setAppRule(app, true).

allowApp(app) -> HSWindowFilter
Name Type Description
app string The application's name or bundle ID
This filter
hs.windowfilter.defaultFilter.allowApp("Spotlight")

rejectApp(app) -> HSWindowFilter

Rejects all of an application's windows. The same as setAppRule(app, false).

rejectApp(app) -> HSWindowFilter
Name Type Description
app string The application's name or bundle ID
This filter
hs.windowfilter.defaultFilter.rejectApp("Cool New Launcher")

setCurrentSpace(value) -> HSWindowFilter

Sets the override rule's currentSpace condition, leaving its other conditions alone

setCurrentSpace(value) -> HSWindowFilter
Name Type Description
value boolean | null true to allow only windows in the current Space (along with minimized and hidden windows, which belong to every Space), false to reject them, or null to ignore Spaces
This filter
const here = hs.windowfilter.create().setCurrentSpace(true)

duplicate() -> HSWindowFilter

A new filter with the same rules, which can then be changed without affecting this one

duplicate() -> HSWindowFilter
The new filter
const noFullscreen = hs.windowfilter.defaultFilter.duplicate().setOverrideRule({ fullscreen: false })

on(event, listener) -> None

Registers a listener for an event about this filter's windows. A filter with listeners keeps working after you drop your reference to it, until its listeners are removed or it's paused.

on(event, listener) -> None
Name Type Description
event HSWindowFilterEvent The event to listen for
listener (window: HSWindow | null, appName: string | null, event: string) => void Called with the window, its application's name, and the event's name. Only windowsChanged passes nulls, when there are no windows left
Event Description
"windowCreated" A window was created
"windowDestroyed" A window was destroyed. Only its id can still be read
"windowMoved" A window was moved or resized, including going fullscreen
"windowFullscreened" A window went fullscreen
"windowUnfullscreened" A window left fullscreen
"windowMinimized" A window was minimized
"windowUnminimized" A window was unminimized
"windowHidden" A window's application was hidden
"windowUnhidden" A window's application was unhidden
"windowVisible" A window became visible: neither minimized nor hidden
"windowNotVisible" A window stopped being visible: it was minimized or hidden
"windowOnScreen" A window came on screen: visible, and in a Space currently shown
"windowNotOnScreen" A window went off screen
"windowInCurrentSpace" A window is now in a Space currently shown
"windowNotInCurrentSpace" A window is no longer in a Space currently shown
"windowFocused" A window gained focus
"windowUnfocused" A window lost focus
"windowTitleChanged" A window's title changed
"windowAllowed" A window is now allowed, emitted before any other event about it
"windowRejected" A window is no longer allowed, or was destroyed, emitted after the events that caused it
"hasWindow" The filter allows a window, having allowed none
"hasNoWindows" The filter no longer allows any windows
"windowsChanged" The set of allowed windows changed
None
Throws an Error on failure; wrap calls in try/catch to handle it.
try {
    hs.windowfilter.defaultFilter.on("windowFocused", (win, appName) => {
        console.log("Focused " + appName + ": " + win.title)
    })
} catch (err) {
    console.error(err.message)
}

off(event, listener) -> None

Removes a listener registered with on() or once()

off(event, listener) -> None
Name Type Description
event HSWindowFilterEvent The event it was registered for
listener (window: HSWindow | null, appName: string | null, event: string) => void The function originally registered
Event Description
"windowCreated" A window was created
"windowDestroyed" A window was destroyed. Only its id can still be read
"windowMoved" A window was moved or resized, including going fullscreen
"windowFullscreened" A window went fullscreen
"windowUnfullscreened" A window left fullscreen
"windowMinimized" A window was minimized
"windowUnminimized" A window was unminimized
"windowHidden" A window's application was hidden
"windowUnhidden" A window's application was unhidden
"windowVisible" A window became visible: neither minimized nor hidden
"windowNotVisible" A window stopped being visible: it was minimized or hidden
"windowOnScreen" A window came on screen: visible, and in a Space currently shown
"windowNotOnScreen" A window went off screen
"windowInCurrentSpace" A window is now in a Space currently shown
"windowNotInCurrentSpace" A window is no longer in a Space currently shown
"windowFocused" A window gained focus
"windowUnfocused" A window lost focus
"windowTitleChanged" A window's title changed
"windowAllowed" A window is now allowed, emitted before any other event about it
"windowRejected" A window is no longer allowed, or was destroyed, emitted after the events that caused it
"hasWindow" The filter allows a window, having allowed none
"hasNoWindows" The filter no longer allows any windows
"windowsChanged" The set of allowed windows changed
None
const onFocus = (win) => console.log(win.title)
wf.on("windowFocused", onFocus)
// later…
wf.off("windowFocused", onFocus)

once(event, listener) -> None

Registers a listener that's called once, the next time the event happens, then removed

once(event, listener) -> None
Name Type Description
event HSWindowFilterEvent The event to listen for
listener (window: HSWindow | null, appName: string | null, event: string) => void Called once
Event Description
"windowCreated" A window was created
"windowDestroyed" A window was destroyed. Only its id can still be read
"windowMoved" A window was moved or resized, including going fullscreen
"windowFullscreened" A window went fullscreen
"windowUnfullscreened" A window left fullscreen
"windowMinimized" A window was minimized
"windowUnminimized" A window was unminimized
"windowHidden" A window's application was hidden
"windowUnhidden" A window's application was unhidden
"windowVisible" A window became visible: neither minimized nor hidden
"windowNotVisible" A window stopped being visible: it was minimized or hidden
"windowOnScreen" A window came on screen: visible, and in a Space currently shown
"windowNotOnScreen" A window went off screen
"windowInCurrentSpace" A window is now in a Space currently shown
"windowNotInCurrentSpace" A window is no longer in a Space currently shown
"windowFocused" A window gained focus
"windowUnfocused" A window lost focus
"windowTitleChanged" A window's title changed
"windowAllowed" A window is now allowed, emitted before any other event about it
"windowRejected" A window is no longer allowed, or was destroyed, emitted after the events that caused it
"hasWindow" The filter allows a window, having allowed none
"hasNoWindows" The filter no longer allows any windows
"windowsChanged" The set of allowed windows changed
None
Throws an Error on failure; wrap calls in try/catch to handle it.
try {
    hs.windowfilter.create(["Safari"]).once("windowCreated", (win) => console.log("New Safari window: " + win.title))
} catch (err) {
    console.error(err.message)
}

pause() -> HSWindowFilter

Stops calling this filter's listeners, until resume(). Its listeners stay registered. A paused filter doesn't keep itself alive, so keep a reference to one you mean to resume.

pause() -> HSWindowFilter
This filter
wf.pause()

resume() -> HSWindowFilter

Starts calling this filter's listeners again after pause()

resume() -> HSWindowFilter
This filter
wf.resume()

windowsToEast(options) -> HSWindow[]

The windows this filter allows that are to the right of a window, nearest first. Only windows on screen count, so combine this with a currentSpace rule (or use hs.windowfilter.currentSpaceFilter) to ignore windows on other Spaces cheaply.

windowsToEast(options) -> HSWindow[]
Name Type Description
options optional object | null How to choose the windows
window optional HSWindow The window to start from. Defaults to the focused window
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 the window
The windows, nearest first
const next = hs.windowfilter.currentSpaceFilter.windowsToEast()[0]
if (next) console.log("Next window east: " + next.title)

focusWindowEast(options) -> boolean

Focuses the nearest window this filter allows that is to the right of a window

focusWindowEast(options) -> boolean
Name Type Description
options optional object | null How to choose the windows
window optional HSWindow The window to start from. Defaults to the focused window
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 the window
boolean
true if a window was focused, false if there was none to focus
hs.hotkey.bind(["cmd", "ctrl"], "right", () => {
    hs.windowfilter.currentSpaceFilter.focusWindowEast({ frontmost: true })
})

windowsToWest(options) -> HSWindow[]

The windows this filter allows that are to the left of a window, nearest first. Only windows on screen count, so combine this with a currentSpace rule (or use hs.windowfilter.currentSpaceFilter) to ignore windows on other Spaces cheaply.

windowsToWest(options) -> HSWindow[]
Name Type Description
options optional object | null How to choose the windows
window optional HSWindow The window to start from. Defaults to the focused window
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 the window
The windows, nearest first
const next = hs.windowfilter.currentSpaceFilter.windowsToWest()[0]
if (next) console.log("Next window west: " + next.title)

focusWindowWest(options) -> boolean

Focuses the nearest window this filter allows that is to the left of a window

focusWindowWest(options) -> boolean
Name Type Description
options optional object | null How to choose the windows
window optional HSWindow The window to start from. Defaults to the focused window
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 the window
boolean
true if a window was focused, false if there was none to focus
hs.hotkey.bind(["cmd", "ctrl"], "left", () => {
    hs.windowfilter.currentSpaceFilter.focusWindowWest({ frontmost: true })
})

windowsToNorth(options) -> HSWindow[]

The windows this filter allows that are above a window, nearest first. Only windows on screen count, so combine this with a currentSpace rule (or use hs.windowfilter.currentSpaceFilter) to ignore windows on other Spaces cheaply.

windowsToNorth(options) -> HSWindow[]
Name Type Description
options optional object | null How to choose the windows
window optional HSWindow The window to start from. Defaults to the focused window
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 the window
The windows, nearest first
const next = hs.windowfilter.currentSpaceFilter.windowsToNorth()[0]
if (next) console.log("Next window north: " + next.title)

focusWindowNorth(options) -> boolean

Focuses the nearest window this filter allows that is above a window

focusWindowNorth(options) -> boolean
Name Type Description
options optional object | null How to choose the windows
window optional HSWindow The window to start from. Defaults to the focused window
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 the window
boolean
true if a window was focused, false if there was none to focus
hs.hotkey.bind(["cmd", "ctrl"], "up", () => {
    hs.windowfilter.currentSpaceFilter.focusWindowNorth({ frontmost: true })
})

windowsToSouth(options) -> HSWindow[]

The windows this filter allows that are below a window, nearest first. Only windows on screen count, so combine this with a currentSpace rule (or use hs.windowfilter.currentSpaceFilter) to ignore windows on other Spaces cheaply.

windowsToSouth(options) -> HSWindow[]
Name Type Description
options optional object | null How to choose the windows
window optional HSWindow The window to start from. Defaults to the focused window
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 the window
The windows, nearest first
const next = hs.windowfilter.currentSpaceFilter.windowsToSouth()[0]
if (next) console.log("Next window south: " + next.title)

focusWindowSouth(options) -> boolean

Focuses the nearest window this filter allows that is below a window

focusWindowSouth(options) -> boolean
Name Type Description
options optional object | null How to choose the windows
window optional HSWindow The window to start from. Defaults to the focused window
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 the window
boolean
true if a window was focused, false if there was none to focus
hs.hotkey.bind(["cmd", "ctrl"], "down", () => {
    hs.windowfilter.currentSpaceFilter.focusWindowSouth({ frontmost: true })
})