HSMenuBarItem
TypeObject representing a macOS system menu bar item.
Create instances with hs.menubar.create().
Properties
Methods
setIcon(image) -> None
Set the icon displayed in the menu bar
Declaration
setIcon(image) -> None
Parameters
| Name | Type | Description |
|---|---|---|
| image | HSImage | An HSImage object, or null to remove the icon |
Returns
None
Example
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
Declaration
setTooltip(tooltip) -> None
Parameters
| Name | Type | Description |
|---|---|---|
| tooltip | string | Tooltip text, or null to remove the tooltip |
Returns
None
Example
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).
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"` (and `modifiers` still includes `"ctrl"`).
`hs.eventtap.currentModifiers()` (e.g. `["cmd", "leftCmd"]`).
To offer a menu alongside a click action, leave the menu unset and call `popUpMenu()` from this callback.
Declaration
setClickCallback(fn) -> None
Parameters
| Name | Type | Description |
|---|---|---|
| fn | function | A function to call on click, or null to remove the callback |
Returns
None
Example
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.
Declaration
setMenu(menuOrFn) -> None
Parameters
| Name | Type | Description |
|---|---|---|
| menuOrFn | JSValue | Array of menu item objects, a function returning such an array, or null to remove the menu |
Returns
None
Example
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") }
])
// 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.
Declaration
popUpMenu(entries) -> None
Parameters
| Name | Type | Description |
|---|---|---|
| entries | JSValue | Array of menu item objects |
Returns
None
Example
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.
Declaration
hide() -> None
Returns
None
Example
const item = hs.menubar.create()
item.hide()
show() -> None
Show this item in the menu bar.
Declaration
show() -> None
Returns
None
Notes
A visible item, and its callbacks, stay in the menu bar until it's hidden or destroyed, even if you don't keep a reference to it
Example
item.show()
isVisible() -> boolean
Check if this item is currently visible in the menu bar.
Declaration
isVisible() -> boolean
Returns
boolean
true if the item is visible in the menu bar
Example
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.
Declaration
destroy() -> None
Returns
None
Example
const item = hs.menubar.create()
item.destroy()