#
hs.menubar
Create and manage menubar icons
#
API Overview
Constants - Useful values which cannot be changed
imagePositions
Constructors - API calls which return an object, typically one that offers API methods
new
Methods - API calls which can only be made on an object returned by a constructor
autosaveName delete frame icon imagePosition isInMenuBar popupMenu removeFromMenuBar returnToMenuBar setClickCallback setIcon setMenu setTitle setTooltip stateImageSize title
#
API Documentation
#
Constants
#
imagePositions
#
Constructors
#
new
#
Methods
#
autosaveName
#
delete
#
frame
#
icon
#
imagePosition
#
isInMenuBar
#
popupMenu
#
removeFromMenuBar
#
returnToMenuBar
#
setClickCallback
| | |
| --------------------------------------------|-------------------------------------------------------------------------------------|
| Signature | hs.menubar:setClickCallback([fn]) -> menubaritem
|
| Type | Method |
| Description | Registers a function to be called when the menubar item is clicked |
| Parameters |
fn
- An optional function to be called when the menubar item is clicked. If this argument is not provided, any existing function will be removed. The function can optionally accept a single argument, which will be a table containing boolean values indicating which keyboard modifiers were held down when the menubar item was clicked; The possible keys are: cmd alt shift ctrl fn
- the menubaritem
- If a menu has been attached to the menubar item, this callback will never be called
- Has no affect on the display of a pop-up menu, but changes will be be in effect if hs.menubar:returnToMenuBar() is called on the menubaritem.
#
setIcon
| | |
| --------------------------------------------|-------------------------------------------------------------------------------------|
| Signature | hs.menubar:setIcon(imageData[, template]) -> menubaritem or nil
|
| Type | Method |
| Description | Sets the image of a menubar item object. The image will be displayed in the system menubar |
| Parameters |
- imageData - This can one of the following:
An
hs.image
object A string containing a path to an image file A string beginning withASCII:
which signifies that the rest of the string is interpreted as a special form of ASCII diagram, which will be rendered to an image and used as the icon. See the notes below for information about the special format of ASCII diagram. nil, indicating that the current image is to be removed - template - An optional boolean value which defaults to true. If it's true, the provided image will be treated as a "template" image, which allows it to automatically support OS X 10.10's Dark Mode. If it's false, the image will be used as is, supporting colour.
- the menubaritem if the image was loaded and set,
nil
if it could not be found or loaded
- ** API Change **
- This method used to return true on success -- this has been changed to return the menubaritem on success to facilitate method chaining. Since Lua treats any value which is not nil or false as "true", this should only affect code where the return value was actually being compared to true, e.g.
if result == true then...
rather than the (unaffected)if result then...
. - If you set a title as well as an icon, they will both be displayed next to each other
- Has no affect on the display of a pop-up menu, but changes will be be in effect if hs.menubar:returnToMenuBar() is called on the menubaritem.
- Icons should be small, transparent images that roughly match the size of normal menubar icons, otherwise they will look very strange. Note that if you're using an
hs.image
image object as the icon, you can force it to be resized withhs.image:setSize({w=16,h=16})
- Retina scaling is supported if the image is either scalable (e.g. a PDF produced by Adobe Illustrator) or contain multiple sizes (e.g. a TIFF with small and large images). Images will not automatically do the right thing if you have a @2x version present
- Icons are by default specified as "templates", which allows them to automatically support OS X 10.10's Dark Mode, but this also means they cannot be complicated, colour images.
- For examples of images that work well, see Hammerspoon.app/Contents/Resources/statusicon.tiff (for a retina-capable multi-image TIFF icon) or https://github.com/jigish/slate/blob/master/Slate/status.pdf (for a scalable vector PDF icon)
- For guidelines on the sizing of images, see http://alastairs-place.net/blog/2013/07/23/nsstatusitem-what-size-should-your-icon-be/
#
setMenu
| | |
| --------------------------------------------|-------------------------------------------------------------------------------------|
| Signature | hs.menubar:setMenu(menuTable) -> menubaritem
|
| Type | Method |
| Description | Attaches a dropdown menu to the menubar item |
| Parameters |
menuTable
: If this argument isnil
: Removes any previously registered menu If this argument is a table: Sets the menu for this menubar item to the supplied table. The format of the table is documented below If this argument is a function: The function will be called each time the user clicks on the menubar item and the function should return a table that specifies the menu to be displayed. The table should be of the same format as described below. The function can optionally accept a single argument, which will be a table containing boolean values indicating which keyboard modifiers were held down when the menubar item was clicked; The possible keys are: cmd alt shift ctrl fn Table Format:lua { { title = "my menu item", fn = function() print("you clicked my menu item!") end }, { title = "-" }, { title = "other item", fn = some_function }, { title = "disabled item", disabled = true }, { title = "checked item", checked = true }, }
The available keys for each menu item are (note thattitle
is the only required key -- all other keys are optional):title
- A string orhs.styledtext
object to be displayed in the menu. If this is the special string"-"
the item will be rendered as a menu separator. This key can be set to the empty string (""), but it must be present.fn
- A function to be executed when the menu item is clicked. The function will be called with two arguments. The first argument will be a table containing boolean values indicating which keyboard modifiers were held down when the menubar item was clicked (seemenuTable
parameter for possible keys) and the second is the table representing the item.checked
- A boolean to indicate if the menu item should have a checkmark (by default) next to it or not. Defaults to false.state
- a text value of "on", "off", or "mixed" indicating the menu item state. "on" and "off" are equivalent tochecked
being true or false respectively, and "mixed" will have a dash (by default) beside it.disabled
- A boolean to indicate if the menu item should be unselectable or not. Defaults to false (i.e. menu items are selectable by default)menu
- a table, in the same format as above, which will be presented as a sub-menu for this menu item.A menu item that is disabled and has a sub-menu will show the arrow at the right indicating that it has a sub-menu, but the items within the sub-menu will not be available, even if the sub-menu items are not disabled themselves.A menu item with a sub-menu is also a clickable target, so it can also have anfn
key.image
- An image to display in the menu to the right of any state image or checkmark and to the left of the menu item title. This image is not constrained by the size set withhs.menubar:stateImageSize , so you should adjust it withhs.image:setSize
if your image is extremely large or small.tooltip
- A tool tip to display if you hover the cursor over a menu item for a few seconds.shortcut
- A string containing a single character, which will be used as the keyboard shortcut for the menu item. Note that if you use a capital letter, the Shift key will be required to activate the shortcut.indent
- An integer from 0 to 15 indicating how far to the right a menu item should be indented. Defaults to 0.onStateImage
- An image to display whenchecked
is true orstate
is set to "on". This image size is constrained to the size set byhs.menubar:stateImageSize . If this key is not set, a checkmark will be displayed for checked or "on" menu items.offStateImage
- An image to display whenchecked
is false orstate
is set to "off". This image size is constrained to the size set byhs.menubar:stateImageSize . If this key is not set, no special marking appears next to the menu item.mixedStateImage
- An image to display whenstate
is set to "mixed". This image size is constrained to the size set byhs.menubar:stateImageSize . If this key is not set, a dash will be displayed for menu items with a state of "mixed".
- the menubaritem
- If you are using the callback function, you should take care not to take too long to generate the menu, as you will block the process and the OS may decide to remove the menubar item