HSMenuBarItem
TypeObject representing a macOS system menu bar item.
Create instances with hs.menubar.create().
Properties
titleColor
Get or set the colour of the menu item's title text, or null to use the menu bar's standard text colour.
Methods
setIcon(image) -> None
Set the icon displayed in the menu bar
setIcon(image) -> None
| Name | Type | Description |
|---|---|---|
| image | HSImage | An HSImage object, or null to remove the icon |
const item = hs.menubar.create()
item.setIcon(HSImage.fromSymbol("star.fill"))
setTooltip(tooltip) -> None
Set the tooltip shown when hovering over the menu bar item
setTooltip(tooltip) -> None
| Name | Type | Description |
|---|---|---|
| tooltip | string | Tooltip text, or null to remove the tooltip |
const item = hs.menubar.create()
item.setTooltip("My menu bar item")
setClickCallback(fn) -> None
Set a callback invoked when the item is clicked with any mouse button (only fires when no menu is set).
The callback receives two arguments:
button(string):"left","right"or"middle", or"buttonN"for other buttons, numbered from 1 (so a mouse's fourth and fifth buttons are"button4"and"button5"). A Ctrl-click, the standard macOS secondary click, is reported as"right"(andmodifiersstill includes"ctrl").modifiers(string[]): The modifier keys held during the click, in the same form ashs.eventtap.currentModifiers()(e.g.["cmd", "leftCmd"]).
To offer a menu alongside a click action, leave the menu unset and call popUpMenu() from this callback.
setClickCallback(fn) -> None
| Name | Type | Description |
|---|---|---|
| fn | ((button: string, modifiers: string[]) => void) | null | A function to call on click, or null to remove the callback |
const item = hs.menubar.create()
item.title = "Click me"
item.setClickCallback((button, modifiers) => {
if (button === "right") {
item.popUpMenu([{ title: "Quit", fn: () => item.destroy() }])
} else {
console.log(`${button} click with ${modifiers.join("+")}`)
}
})
setMenu(menuOrFn) -> None
Set the menu for this item. Pass an array of menu item objects for a static menu,
or a function that returns an array for a dynamic menu populated each time it opens.
The function receives one argument: the modifier keys held when the menu opened, in the same form as
hs.eventtap.currentModifiers().
While a menu is set, clicking the item opens the menu and the click callback is not called.
The menu items are HSMenuItem objects. An item's shortcut is shown at the right-hand side of the
item, either as a string of modifier symbols (⌘ ⇧ ⌥ ⌃) followed by one key, as the menu shows it
("⌘T", "⇧⌘N", or just "F" for no modifiers), or as an object with the modifiers and key spelled out
({ mods: ["cmd", "shift"], key: "n" }; leave out mods for no modifiers). The key can be a character,
a key name such as "return", "delete", "left" or "f5", or the symbol a menu draws for it such as
↩, ⌫, ⎋ or ← ("⌘⌫"). Modifier and key names are the same as for hs.hotkey.bind().
A menu shortcut is not a system-wide hotkey. Pressing it chooses the item only while the menu is
open; while the menu is closed it does nothing. To run something from anywhere with a key combination,
also bind it with hs.hotkey.bind().
setMenu(menuOrFn) -> None
| Name | Type | Description |
|---|---|---|
| menuOrFn | HSMenuItem[] | ((modifiers: string[]) => HSMenuItem[]) | null | Array of menu items, a function returning such an array, or null to remove the menu |
const item = hs.menubar.create()
item.title = "Menu"
item.setMenu([
{ title: "Option A", fn: () => console.log("A") },
{ title: "-" },
{ title: "Option B", checked: true, fn: () => console.log("B") },
{ title: "Danger", color: "#FF3B30", fn: () => console.log("danger") }
])
// Show shortcuts next to items. These are labels only, so bind the same keys with hs.hotkey
// for them to work while the menu is closed
const reload = () => hs.reload()
hs.hotkey.bind(["cmd", "alt", "ctrl"], "r", reload)
item.setMenu([
{ title: "Reload config", shortcut: "⌃⌥⌘R", fn: reload },
{ title: "Open Console", shortcut: { mods: ["cmd"], key: "o" }, fn: () => hs.openConsole() },
{ title: "Free draw", shortcut: "F", fn: () => console.log("free draw") }
])
// Show extra entries when Option is held
item.setMenu((modifiers) => [
{ title: "Option A", fn: () => console.log("A") },
...(modifiers.includes("alt") ? [{ title: "Advanced", fn: () => console.log("advanced") }] : [])
])
popUpMenu(entries) -> None
Immediately open a menu below this item, independently of any menu set with setMenu().
This is intended to be called from a click callback, so that a single item can perform an action on one
kind of click and offer a menu on another. The menu item objects are the same as for setMenu().
If the item is hidden, the menu opens at the mouse pointer instead.
popUpMenu(entries) -> None
| Name | Type | Description |
|---|---|---|
| entries | HSMenuItem[] | The menu's items |
const item = hs.menubar.create()
item.title = "Dictate"
item.setClickCallback((button) => {
if (button === "right") {
item.popUpMenu([
{ title: "Settings…", fn: () => console.log("settings") },
{ title: "-" },
{ title: "Remove", fn: () => item.destroy() }
])
} else {
console.log("start dictation")
}
})
hide() -> None
Remove this item from the menu bar. It can be shown again with show() while you keep a reference to it.
hide() -> None
const item = hs.menubar.create()
item.hide()
show() -> None
Show this item in the menu bar.
show() -> None
item.show()
isVisible() -> boolean
Check if this item is currently visible in the menu bar.
isVisible() -> boolean
const visible = item.isVisible()
destroy() -> None
Permanently remove this item from the menu bar and release all resources.
After calling destroy(), the item is no longer usable.
This is called automatically on hs.reload(). Use hide() instead
if you only want to temporarily remove the item without freeing it.
destroy() -> None
const item = hs.menubar.create()
item.destroy()