HSChooser
TypeA keyboard-driven floating chooser panel.
Create via hs.chooser.create(). Configure choices, set callbacks, then call .show().
Choice format
Each choice is a plain object with required text and optional subText, image, valid,
and contextMenu fields. All other fields are passed through to the onSelect callback unchanged.
Set contextMenu: false on a choice to show no context menu on its row.
{
text: "Open Safari", subText: "com.apple.Safari",
image: HSImage.fromAppBundle("com.apple.Safari"), valid: true, myData: 42
}
Context menu
A single right-click menu, set with contextMenu, is shared by every row (except choices with
contextMenu: false). When the user picks one of its entries, onContextMenuSelect is called
with the row's choice and the menu entry.
chooser.contextMenu = [
{ title: "Open", id: "open" },
{ type: "divider" },
{ title: "Copy bundle ID", id: "copy" }
]
chooser.onContextMenuSelect = (choice, menuItem) => {
if (menuItem.id === "copy") hs.pasteboard.writeString(choice.subText)
}
Keyboard shortcuts
- Return — confirm selection
- Escape — dismiss (calls
onSelectwithnull) - ↑ / ↓ — move through results
- Ctrl-N / Ctrl-P — move through results (alternative to ↑ / ↓)
- Page Up / Page Down (or Fn-↑ / Fn-↓) — move through results a page (
visibleRowsrows) at a time - Home / End (or Fn-← / Fn-→) — jump to the first / last result
- Cmd-1 ... Cmd-9, Cmd-0 — instantly select one of the first ten results
Properties
query
The current text in the search field. Setting this from JS updates the display but
does not invoke the onQueryChange callback.
searchSubText
Whether searches match against subText in addition to text (default: false).
Only applies when a static choices array is provided.
enableDefaultForQuery
When true and the query is non-empty but there are no matching choices, onSelect
is called with { text: <query> } instead of null (default: false).
backgroundColor
Background color of the chooser panel. When nil (the default), the panel uses
the system's translucent glass/material effect.
If you set this to a solid or near-opaque color, also set textColor, subTextColor,
and placeholderColor to ensure readable contrast — they are not adjusted automatically.
borderColor
Border color drawn around the chooser panel. When nil (the default), no border is drawn.
borderWidth
Width of the border drawn around the chooser panel, in points (default: 1).
Has no effect unless borderColor is also set.
textColor
Color of each result row's main text. When nil (the default), the system's
primary label color is used.
subTextColor
Color of each result row's subtext. When nil (the default), the system's
secondary label color is used.
queryColor
Color of the text the user types into the search field. When nil (the default),
the system's primary label color is used.
placeholderColor
Color of the placeholder text (and search icon) shown in an empty search field.
When nil (the default), the system's secondary label color is used.
selectionColor
Background tint of the highlighted row. When nil (the default), a translucent
tint of the system accent color is used.
textSize
Font size of each result row's main text, in points (default: 14). Changing this
also rescales the ⌘-digit shortcut hint shown on the first ten rows, and changes the
row height (and therefore the panel height) to fit. Must be a positive, finite number;
invalid values are ignored (with a warning logged).
subTextSize
Font size of each result row's subtext, in points (default: 12). Changing this
also changes the row height (and therefore the panel height) to fit. Must be a
positive, finite number; invalid values are ignored (with a warning logged).
querySize
Font size of the search field's typed text and placeholder, in points (default: 20).
Changing this also rescales the search icon, and changes the search bar height
(and therefore the panel height) to fit. Must be a positive, finite number; invalid
values are ignored (with a warning logged).
onSelect
Called when the user confirms a selection, or null to remove the handler.
The argument is the chosen row object (the original dict you passed to setChoices,
with text, subText, image, valid, and any custom fields intact).
The argument is null when dismissed (Escape).
onQueryChange
Called on every keystroke with the new query string, or null to remove the handler.
Use this to debounce expensive searches or trigger async data fetching.
onSelectionChange
Called when the selected row changes while the chooser is visible, or null to remove the handler.
Use this to show something that depends on the selected row, such as a preview, and keep it up to date as the user moves through the rows.
The arguments are the new selectedRow and the row object (same shape as onSelect), or
null for the item when no rows match the query. It is called once when the chooser is
shown, then whenever the selection moves to a different row, or the row it is on changes
to a different choice: arrow keys, Page Up/Down, Home/End, a click, a new query, new
choices, or setting selectedRow from JavaScript.
onHide
Called after the panel is hidden (for any reason: selection, Escape, or hide()), or null to remove the handler.
onInvalid
Called when the user activates a row whose valid field is false, or null to remove the handler.
The chooser stays open; the argument is the row dict (same shape as onSelect).
If unset, activating an invalid row is silently ignored.
contextMenu
The right-click menu shown for every row (default: [], no menu).
Each entry is either { title: "..." } for a menu item, or { type: "divider" } for a
separator. Any other fields on an entry (e.g. an id) are passed back to
onContextMenuSelect unchanged, so one handler can tell the entries apart.
Rows whose choice has contextMenu: false show no menu, e.g. for a "Clear history" row.
onContextMenuSelect
Called when the user picks an entry from the contextMenu, or null to remove the handler.
choice is the row that was right-clicked (same shape as onSelect's argument) and
menuItem is the contextMenu entry that was picked, with any custom fields intact.
The chooser stays open.
Methods
setChoices(choices) -> HSChooser
Set the choices list. Pass a static array or a function:
- Array — the chooser filters it automatically as the user types.
- Function — called with the current query string on every
refreshChoices()and on show. The function is responsible for filtering; the chooser displays all items it returns.
setChoices(choices) -> HSChooser
| Name | Type | Description |
|---|---|---|
| choices | JSValue | An array of choice objects, or a function (query) => [...] |
// Static
chooser.setChoices([{text: "Foo"}, {text: "Bar"}])
// Dynamic
chooser.setChoices(q => items.filter(i => i.text.toLowerCase().includes(q.toLowerCase())))
refreshChoices() -> HSChooser
Re-apply filtering (static choices) or re-invoke the choices function (dynamic).
Call after updating an external data source in an async onQueryChange handler.
refreshChoices() -> HSChooser
chooser.refreshChoices()
show() -> HSChooser
Show the chooser.
show() -> HSChooser
chooser.show()
hide() -> HSChooser
Hide the chooser without making a selection. Restores focus to the previously active window.
hide() -> HSChooser
chooser.hide()
select(row) -> HSChooser
Programmatically confirm a selection.
Omit row to confirm the currently highlighted row. Fires onSelect (or onInvalid
for rows with valid: false) and hides the chooser.
select(row) -> HSChooser
| Name | Type | Description |
|---|---|---|
| row | JSValue | Zero-based row index, or omit to use the current selection. |
chooser.select() // confirm highlighted row
chooser.select(2) // confirm row at index 2
selectedRowContents(row) -> [String: Any]
Returns the dict for the highlighted row, or for a specific row by index.
Returns null if the index is out of range or no choices are set.
selectedRowContents(row) -> [String: Any]
| Name | Type | Description |
|---|---|---|
| row | JSValue | Zero-based row index, or omit to query the highlighted row. |
{ text, subText?, image?, valid, ...extras }) or null.const item = chooser.selectedRowContents()
const item = chooser.selectedRowContents(2)