hs.windowfilter
ModuleWindow filters
A window filter picks out some of the windows on your Mac with a set of rules, such as "visible windows in the current Space, except Finder's". You can ask a filter for the windows it allows right now, or listen for events about them: windows being created, focused, moved, minimized, joining or leaving the filter, and so on.
// Every visible window of every app, except a few utility apps
const wf = hs.windowfilter.defaultFilter
// Only Safari and Mail's visible windows
const mailAndWeb = hs.windowfilter.create(["Safari", "Mail"])
// Visible, non-fullscreen windows in the current Space; no windows at all from Finder, and
// only titled windows from Terminal
const custom = hs.windowfilter.create({
override: { currentSpace: true, fullscreen: false },
default: { visible: true },
apps: { Finder: false, "com.apple.Terminal": { visible: true, allowTitles: 1 } },
})
custom.on("windowFocused", (win, appName) => console.log(appName + ": " + win.title))
Rules
A window must pass the override rule, if there is one. Then the rule for its application
decides, or the default rule if the application has none of its own. An application rule
replaces the default rule rather than adding to it.
A rule is true (the application's visible windows), false (none of them), or an object of
conditions, all of which must hold:
visible:truefor only visible windows (neither minimized nor hidden),falsefor only invisible onescurrentSpace:truefor only windows in a Space currently shown,falsefor only windows in other Spaces. Minimized and hidden windows count as being in every Space.fullscreen,focused,hasTitlebar:trueto require the window to be fullscreen, focused, or to have a titlebar;falseto require it not to beactiveApplication:truefor only windows of the active application,falsefor only other applications' windowsallowTitles: a number, for windows whose titles are at least that many characters long (so1rejects untitled windows); or a string (an exact title), a RegExp, or an array of them, for windows whose titles match onerejectTitles: a string, RegExp, or array of them; windows whose titles match one are rejectedallowRegions,rejectRegions: anHSRect(or{x, y, w, h}), or an array of them. A window is in a region if at least half of it is inside, or it covers at least half of the region.allowScreens,rejectScreens: anHSScreen, a screen's name or UUID, or an array of them, for the screen holding most of the windowallowRoles: a window subrole (seeHSWindow.subrole), an array of them, or"*"for any window. Defaults tohs.windowfilter.allowedWindowRoles, except in the override rule, which allows any subrole unless it says otherwise. macOS reports minimized windows, and every window of a hidden application, as"AXDialog", so these rules see the subrole a window had when it was last shown instead. A window that has only been seen minimized or hidden counts as an"AXStandardWindow"if it can be minimized, as dialogs can't (seeHSWindow.isStandard). One that can't be minimized, such as TextEdit's Settings window first seen while TextEdit is hidden, counts as an"AXDialog"until it's shown.
Regions and screens only apply to visible windows. RegExps support the i, m and s flags.
Hammerspoon's own windows (such as the Console) are included like any other application's.
An hs.canvas reports a subrole ending in .Hammerspoon (see
HSCanvas.setAccessibilitySubrole), which the default roles don't include, so only a rule
that allows its subrole sees it.
Applications that turn Accessibility off
Window filters see windows through the Accessibility API, which an application can turn off
for itself (Firefox does in kiosk mode). Such an application's windows never appear in any
filter. hs.windowfilter.inaccessibleApplications() lists them, and the module's
inaccessibleApplication event reports each one as it's found, so you can still offer them
some other way, e.g. by activating the application.
Types
This module provides the following types:
Event Types
The events this module's watchers emit, and when. In TypeScript, each type is a union of its event names that you can use as a type of your own:
HSWindowFilterModuleEvent
An event name for hs.windowfilter.on(), hs.windowfilter.off() and hs.windowfilter.once()
| Event | Description |
|---|---|
"inaccessibleApplication" |
An application turned the Accessibility API off for itself, so no window filter can see its windows |
HSWindowFilterEvent
An event name for HSWindowFilter.on(), HSWindowFilter.off() and HSWindowFilter.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 |
Properties
hs.windowfilter.defaultFilter
The default window filter: the visible windows of every application, except some whose windows are fleeting or not really windows (Spotlight, Notification Center, menu bar utilities, and so on). It's shared by everything in your config, so change it with care, before creating other filters from it.
hs.windowfilter.currentSpaceFilter
A copy of hs.windowfilter.defaultFilter that only allows windows in the current Space. It's
made the first time you use it, from the default filter as it is then.
hs.windowfilter.allowedWindowRoles
The window subroles rules allow when they don't say which subroles they allow:
AXStandardWindow, AXDialog and AXSystemDialog to begin with. Changing it affects
every filter; to allow other subroles in one filter, give its rules allowRoles.
Methods
hs.windowfilter.create(rules) -> HSWindowFilter
Creates a window filter
hs.windowfilter.create(rules) -> HSWindowFilter
| Name | Type | Description |
|---|---|---|
| rules optional | {override?: boolean | object, default?: boolean | object, apps?: Record<string, boolean | object>, sortOrder?: "focusedLast" | "focused" | "createdLast" | "created"} | string | string[] | ((window: HSWindow) => boolean) | What the filter allows: omit it for a copy of hs.windowfilter.defaultFilter (including any changes you've made to it); an application's name or bundle ID, or an array of them, for those applications' visible windows; a function, called with each window, that returns true to allow it; or an object of rules: |
| override optional | boolean | object | The rule every window must pass. See the module's description for rules' conditions |
| default optional | boolean | object | The rule for applications without one of their own. Defaults to every window with a standard role, visible or not |
| apps optional | Record<string, boolean | object> | Rules for particular applications, by name or bundle ID |
| sortOrder optional | "focusedLast" | "focused" | "createdLast" | "created" | How windows() sorts windows. Defaults to "focusedLast" |
try {
const safari = hs.windowfilter.create("Safari")
const titled = hs.windowfilter.create({ default: { visible: true, allowTitles: 1 } })
const wide = hs.windowfilter.create(win => win.frame.w > 1000)
const noScratch = hs.windowfilter.create({ apps: { TextEdit: { rejectTitles: /^Untitled/ } } })
} catch (err) {
console.error(err.message)
}
hs.windowfilter.focusEast() -> boolean
Focuses the nearest window to the right of the focused one, among those in
hs.windowfilter.currentSpaceFilter, only considering windows within 45° of that direction
hs.windowfilter.focusEast() -> boolean
true if a window was focusedhs.hotkey.bind(["cmd", "ctrl"], "right", () => hs.windowfilter.focusEast())
hs.windowfilter.focusWest() -> boolean
Focuses the nearest window to the left of the focused one, among those in
hs.windowfilter.currentSpaceFilter, only considering windows within 45° of that direction
hs.windowfilter.focusWest() -> boolean
true if a window was focusedhs.hotkey.bind(["cmd", "ctrl"], "left", () => hs.windowfilter.focusWest())
hs.windowfilter.focusNorth() -> boolean
Focuses the nearest window above the focused one, among those in
hs.windowfilter.currentSpaceFilter, only considering windows within 45° of that direction
hs.windowfilter.focusNorth() -> boolean
true if a window was focusedhs.hotkey.bind(["cmd", "ctrl"], "up", () => hs.windowfilter.focusNorth())
hs.windowfilter.focusSouth() -> boolean
Focuses the nearest window below the focused one, among those in
hs.windowfilter.currentSpaceFilter, only considering windows within 45° of that direction
hs.windowfilter.focusSouth() -> boolean
true if a window was focusedhs.hotkey.bind(["cmd", "ctrl"], "down", () => hs.windowfilter.focusSouth())
hs.windowfilter.inaccessibleApplications() -> HSApplication[]
The running applications that have turned the Accessibility API off for themselves (e.g. Firefox in kiosk mode), so no window filter can see their windows. Hammerspoon stops asking such an application until it quits.
While any filter or inaccessibleApplication listener is active, this is what Hammerspoon
has found so far, at no cost; otherwise, it asks every application. It's empty if
Hammerspoon hasn't been given Accessibility permission.
hs.windowfilter.inaccessibleApplications() -> HSApplication[]
for (const app of hs.windowfilter.inaccessibleApplications()) {
console.log(app.name + " has turned Accessibility off")
}
hs.windowfilter.on(event, listener) -> None
Register a listener for window filter module events.
"inaccessibleApplication" fires when an application is found to have turned the
Accessibility API off for itself (e.g. Firefox in kiosk mode), so no window filter can see its
windows. It fires once per launch of such an application, for applications found after the
listener is registered; hs.windowfilter.inaccessibleApplications() lists the ones already
known.
hs.windowfilter.on(event, listener) -> None
| Name | Type | Description |
|---|---|---|
| event | HSWindowFilterModuleEvent | The event to listen for |
| listener | (app: HSApplication) => void | Called with the application |
| Event | Description |
|---|---|
"inaccessibleApplication" |
An application turned the Accessibility API off for itself, so no window filter can see its windows |
try {
hs.windowfilter.on("inaccessibleApplication", app => console.log(app.name + " has turned Accessibility off"))
} catch (err) {
console.error(err.message)
}
hs.windowfilter.off(event, listener) -> None
Remove a previously registered window filter module event listener.
hs.windowfilter.off(event, listener) -> None
| Name | Type | Description |
|---|---|---|
| event | HSWindowFilterModuleEvent | The event the listener was registered for |
| listener | (app: HSApplication) => void | The function originally passed to on |
| Event | Description |
|---|---|
"inaccessibleApplication" |
An application turned the Accessibility API off for itself, so no window filter can see its windows |
const onInaccessible = app => console.log(app.name + " has turned Accessibility off")
hs.windowfilter.on("inaccessibleApplication", onInaccessible)
// later…
hs.windowfilter.off("inaccessibleApplication", onInaccessible)
hs.windowfilter.once(event, listener) -> None
Register a listener that fires at most once, the next time a window filter module event happens. See on() for the events.
hs.windowfilter.once(event, listener) -> None
| Name | Type | Description |
|---|---|---|
| event | HSWindowFilterModuleEvent | The event to listen for |
| listener | (app: HSApplication) => void | Called once, then automatically removed |
| Event | Description |
|---|---|
"inaccessibleApplication" |
An application turned the Accessibility API off for itself, so no window filter can see its windows |
try {
hs.windowfilter.once("inaccessibleApplication", app => console.log(app.name + " has turned Accessibility off"))
} catch (err) {
console.error(err.message)
}