hs.chooser
Modulehs.chooser
A Spotlight-style chooser for presenting options to the user
hs.chooser lets you show a floating search panel that users can type into to filter and
select from a list of items. It's ideal for launchers, emoji pickers, command palettes, and
any interface where you want fast, keyboard-driven selection.
Quick Start
const chooser = hs.chooser.create()
chooser.setChoices([
{ text: "Open Safari", subText: "Web browser", action: "safari" },
{ text: "Open Terminal", subText: "Command line", action: "terminal" }
])
chooser.onSelect = (item) => {
if (item) console.log("Selected: " + item.text + " (" + item.action + ")")
}
chooser.show()
Dynamic Choices
For search-as-you-type filtering powered by your own data:
const allApps = hs.application.runningApplications()
chooser.setChoices((query) => {
const q = query.toLowerCase()
return allApps
.filter(a => a.name.toLowerCase().includes(q))
.map(a => ({ text: a.name, subText: a.bundleID }))
})
Async Choices (with debounce)
For results fetched from an external source:
let debounceTimer = null
let cachedResults = []
chooser.setChoices(() => cachedResults)
chooser.onQueryChange = (query) => {
if (debounceTimer) debounceTimer.invalidate()
debounceTimer = hs.timer.doAfter(0.05, () => {
fetchFromAPI(query).then(results => {
cachedResults = results
chooser.refreshChoices()
})
})
}
Types
This module provides the following types:
Object Types
Plain JavaScript objects that this module's methods and properties take or return:
HSChooserChoice
A row of a chooser
| Field | Type | Description |
|---|---|---|
| text | string | The row's main text, which the query is matched against |
| subText optional | string | Smaller text shown under text, also matched against the query if searchSubText is true |
| image optional | HSImage | An icon shown at the left of the row |
| valid optional | boolean | false makes choosing the row call onInvalid instead of onSelect. Defaults to true |
| contextMenu optional | boolean | false shows no contextMenu for the row |
| [key] | any | Any other fields, which are passed back to your callbacks unchanged |
HSChooserMenuItem
An entry of a chooser's contextMenu
| Field | Type | Description |
|---|---|---|
| title optional | string | The entry's label. Required unless type is "divider" |
| type optional | "divider" | Makes the entry a separator line |
| shortcut optional | string | HSMenuShortcut | A keyboard shortcut shown beside the entry, as for HSMenuItem. It only works while the menu is open |
| [key] | any | Any other fields, such as an id, which are passed back to onContextMenuSelect unchanged |
Properties
This module has no properties.
Methods
hs.chooser.create() -> HSChooser
Create a new chooser.
Declaration
hs.chooser.create() -> HSChooser
Returns
A new
HSChooser object ready for configurationExample
const c = hs.chooser.create()
c.setChoices([{text: "Hello"}]).onSelect = item => console.log(item.text)
c.show()