DEPRECATED. Primitives for drawing on the screen in various ways.
hs.drawing is now deprecated and will be removed in a future release. Its functionality is now implemented by hs.canvas and you should migrate your code to using that directly. The API docs for hs.drawing remain here as a convenience.
A table of predefined window levels usable with hs.drawing:setLevel(...)
Notes
This table has a __tostring() metamethod which allows listing it's contents in the Hammerspoon console by typing hs.drawing.windowLevels.
These key names map to the constants used in CoreGraphics to specify window levels and may not actually be used for what the name might suggest. For example, tests suggest that an active screen saver actually runs at a level of 2002, rather than at 1000, which is the window level corresponding to kCGScreenSaverWindowLevelKey.
Each drawing level is sorted separately and hs.drawing:orderAbove(...) and hs.drawing:orderBelow(...)` only arrange windows within the same level.
If you use Dock hiding (or in 10.11, Menubar hiding) please note that when the Dock (or Menubar) is popped up, it is done so with an implicit orderAbove, which will place it above any items you may also draw at the Dock (or MainMenu) level.
A drawing object with a hs.drawing:setClickCallback function can only reliably receive mouse click events when its window level is at hs.drawing.windowLevels.desktopIcon + 1 or higher.
Returns a table containing the default font, size, color, and paragraphStyle used by hs.drawing for text drawing objects.
Parameters
None
Returns
a table containing the default style attributes hs.drawing uses for text drawing objects in the hs.styledtext attributes table format.
Notes
This method returns the default font, size, color, and paragraphStyle used by hs.drawing for text objects. If you modify a drawing object's defaults with hs.drawing:setColor, hs.drawing:setTextFont, or hs.drawing:setTextSize, the changes will not be reflected by this function.
Tells the OS X window server to pause updating the physical displays for a short while.
Parameters
None
Returns
None
Notes
This method can be used to allow multiple changes which are being made to the users display appear as if they all occur simultaneously by holding off on updating the screen on the regular schedule.
This method should always be balanced with a call to hs.drawing.enableScreenUpdates when your updates have been completed. Failure to do so will be logged in the system logs.
The window server will only allow you to pause updates for up to 1 second. This prevents a rogue or hung process from locking the systems display completely. Updates will be resumed when hs.drawing.enableScreenUpdates is encountered or after 1 second, whichever comes first.
The underlying OS function for disabling screen updates is deprecated.
Tells the OS X window server to resume updating the physical displays after a previous pause.
Parameters
None
Returns
None
Notes
In conjunction with hs.drawing.disableScreenUpdates, this method can be used to allow multiple changes which are being made to the users display appear as if they all occur simultaneously by holding off on updating the screen on the regular schedule.
The window server will only allow you to pause updates for up to 1 second. This prevents a rogue or hung process from locking the systems display completely. Updates will be resumed when this function is encountered or after 1 second, whichever comes first.
The underlying OS function for enabling screen updates is deprecated.
hs.drawing.getTextDrawingSize(styledTextObject or theText, [textStyle]) -> sizeTable | nil
Type
Function
Description
Get the size of the rectangle necessary to fully render the text with the specified style so that is will be completely visible.
Parameters
styledTextObject - an object created with the hs.styledtext module or its table representation (see hs.styledtext).
textStyle - an optional table containing one or more of the following keys to set for the text of the drawing object (if textStyle is nil or missing, the hs.drawing defaults are used):
Returns
sizeTable - a table containing the Height and Width necessary to fully display the text drawing object, or nil if an error occurred
Notes
This function assumes the default values specified for any key which is not included in the provided textStyle.
The size returned is an approximation and may return a width that is off by about 4 points. Use the returned size as a minimum starting point. Sometimes using the "clip" or "truncateMiddle" lineBreak modes or "justified" alignment will fit, but its safest to add in your own buffer if you have the space in your layout.
Multi-line text (separated by a newline or return) is supported. The height will be for the multiple lines and the width returned will be for the longest line.
The following simplified style format is supported for use with hs.drawing:setText and hs.drawing.setTextStyle.
theText - the text which is to be displayed.
textStyle - a table containing one or more of the following keys to set for the text of the drawing object (if textStyle is nil or missing, the hs.drawing defaults are used):
font - the name of the font to use (default: the system font)
size - the font point size to use (default: 27.0)
color - ignored, but accepted for compatibility with hs.drawing:setTextStyle()
alignment - a string of one of the following indicating the texts alignment within the drawing objects frame:
"left" - the text is visually left aligned.
"right" - the text is visually right aligned.
"center" - the text is visually center aligned.
"justified" - the text is justified
"natural" - (default) the natural alignment of the text’s script
lineBreak - a string of one of the following indicating how to wrap text which exceeds the drawing object's frame:
"wordWrap" - (default) wrap at word boundaries, unless the word itself doesn’t fit on a single line
"charWrap" - wrap before the first character that doesn’t fit
"clip" - do not draw past the edge of the drawing object frame
"truncateHead" - the line is displayed so that the end fits in the frame and the missing text at the beginning of the line is indicated by an ellipsis
"truncateTail" - the line is displayed so that the beginning fits in the frame and the missing text at the end of the line is indicated by an ellipsis
"truncateMiddle" - the line is displayed so that the beginning and end fit in the frame and the missing text in the middle is indicated by an ellipsis
hs.drawing.appImage(sizeRect, bundleID) -> drawingObject or nil
Type
Constructor
Description
Creates a new image object with the icon of a given app
Parameters
sizeRect - A rect-table containing the location/size of the image. If the size values are -1 then the image will be displayed at the icon's native size
bundleID - A string containing the bundle identifier of an app (e.g. "com.apple.Safari")
hs.drawing.image(sizeRect, imageData) -> drawingObject or nil
Type
Constructor
Description
Creates a new image object
Parameters
sizeRect - A rect-table containing the location/size of the image
imageData - This can be either:
An hs.image object
A string containing a path to an image file
A string beginning with ASCII: which signifies that the rest of the string is interpreted as a special form of ASCII diagram, which will be rendered to an image. See the notes below for information about the special format of ASCII diagram.
Returns
An hs.drawing object, or nil if an error occurs
Paths relative to the PWD of Hammerspoon (typically ~/.hammerspoon/) will work, but paths relative to the UNIX homedir character, ~ will not
Animated GIFs are supported. They're not super friendly on your CPU, but they work
Notes
To use the ASCII diagram image support, see http://cocoamine.net/blog/2015/03/20/replacing-photoshop-with-nsstring/ and be sure to preface your ASCII diagram with the special string ASCII:
Places the drawing object on top of normal windows
Parameters
aboveEverything - An optional boolean value that controls how far to the front the drawing should be placed. true to place the drawing on top of all windows (including the dock and menubar), false to place the drawing above normal windows, but below the dock and menubar. Defaults to false.
Returns
The drawing object
Notes
As of macOS Sierra and later, if you want a hs.drawing object to appear above full-screen windows you must hide the Hammerspoon Dock icon first using: hs.dockicon.hide()
hs.drawing:clickCallbackActivating([false]) -> drawingObject or current value
Type
Method
Description
Get or set whether or not clicking on a drawing with a click callback defined should bring all of Hammerspoon's open windows to the front.
Parameters
flag - an optional boolean indicating whether or not clicking on a drawing with a click callback function defined should activate Hammerspoon and bring its windows forward. Defaults to true.
Returns
If a setting value is provided, the drawing object is returned; if no argument is provided, the current setting is returned.
Notes
Setting this to false changes a drawing object's AXsubrole value and may affect the results of filters defined for hs.window.filter, depending upon how they are defined.
hs.drawing:clippingRectangle([rect]) -> drawingObject or current value
Type
Method
Description
Set the screen area in which the drawing contents are visible.
Parameters
rect - an optional rectangle specifying the visible area of the screen where the drawing's contents are visible. If an explicit nil is specified, no clipping rectangle is set. Defaults to nil
Returns
if an argument is provided, returns the drawing object; otherwise the current value is returned.
Notes
This method can be used to specify the area of the display where this drawing should be visible. If any portion of the drawing extends beyond this rectangle, the image is clipped so that only the portion within this rectangle is visible.
The rectangle defined by this method is independent of the drawing's actual frame -- if you move the drawing with hs.drawing:setFrame or hs.drawing:setTopLeft, this rectangle retains its current value.
This method does not work for image objects at present.
This method immediately destroys the drawing object. If you want it to fade out, use :hide() first, with some suitable time, and hs.timer.doAfter() to schedule the :delete() call
hs.drawing:imageFrame([type]) -> drawingObject or current value
Type
Method
Description
Get or set what type of frame should be around the drawing frame of the image.
Parameters
type - an optional string value which should match one of the following (default is none):
none - no frame is drawing around the drawingObject's frameRect
photo - a thin black outline with a white background and a dropped shadow.
bezel - a gray, concave bezel with no background that makes the image look sunken.
groove - a thin groove with a gray background that looks etched around the image.
button - a convex bezel with a gray background that makes the image stand out in relief, like a button.
Returns
If a setting value is provided, the drawing object is returned; if no argument is provided, the current setting is returned.
Notes
Apple considers the photo, groove, and button style frames "stylistically obsolete" and if a frame is required, recommend that you use the bezel style or draw your own to more closely match the OS look and feel.
angle - the angle in degrees to rotate the image around its center in a clockwise direction.
Returns
The drawing object
Notes
This method works by rotating the image view within its drawing window. This means that an image which completely fills its viewing area will most likely be cropped in some places. Best results are achieved with images that have clear space around their edges or with hs.drawing.imageScaling set to "none".
Sets a callback for mouseUp and mouseDown click events
Parameters
mouseUpFn - A function, can be nil, that will be called when the drawing object is clicked on and the mouse button is released. If this argument is nil, any existing callback is removed.
mouseDownFn - A function, can be nil, that will be called when the drawing object is clicked on and the mouse button is first pressed down. If this argument is nil, any existing callback is removed.
Returns
The drawing object
Notes
No distinction is made between the left, right, or other mouse buttons -- they all invoke the same up or down function. If you need to determine which specific button was pressed, use hs.eventtap.checkMouseButtons() within your callback to check.
Sets the window level more precisely than sendToBack and bringToFront.
Parameters
theLevel - the level specified as a number or as a string where this object should be drawn. If it is a string, it must match one of the keys in hs.drawing.windowLevels.
Returns
the drawing object
Notes
see the notes for hs.drawing.windowLevels
These levels may be unable to explicitly place drawing objects around full-screen macOS windows
This method should only be used on text drawing objects
If the text of the drawing object is emptied (i.e. "") then style changes may be lost. Use a placeholder such as a space (" ") or hide the object if style changes need to be saved but the text should disappear for a while.
color - a color table as described in hs.drawing.color
Returns
The drawing object
Notes
This method should only be called on text drawing objects
This method changes the font color for portions of an hs.drawing text object which do not have a specific font set in their attributes list (see hs.styledtext for more details).
fontname - A string containing the name of the font to use
Returns
The drawing object
Notes
This method should only be used on text drawing objects
This method changes the font for portions of an hs.drawing text object which do not have a specific font set in their attributes list (see hs.styledtext for more details).
This method should only be used on text drawing objects
This method changes the font size for portions of an hs.drawing text object which do not have a specific font set in their attributes list (see hs.styledtext for more details).
Sets some simple style parameters for the entire text of a drawing object. For more control over style including having multiple styles within a single text object, use hs.styledtext and hs.drawing:setStyledText instead.
Parameters
textStyle - an optional table containing one or more of the following keys to set for the text of the drawing object (if the table is nil or left out, the style is reset to the hs.drawing defaults):
font - the name of the font to use (default: the system font)
size - the font point size to use (default: 27.0)
color - a color table as described in hs.drawing.color
alignment - a string of one of the following indicating the texts alignment within the drawing objects frame: * "left" - the text is visually left aligned. * "right" - the text is visually right aligned. * "center" - the text is visually center aligned. * "justified" - the text is justified * "natural" - (default) the natural alignment of the text’s script
lineBreak - a string of one of the following indicating how to wrap text which exceeds the drawing object's frame: * "wordWrap" - (default) wrap at word boundaries, unless the word itself doesn’t fit on a single line * "charWrap" - wrap before the first character that doesn’t fit * "clip" - do not draw past the edge of the drawing object frame * "truncateHead" - the line is displayed so that the end fits in the frame and the missing text at the beginning of the line is indicated by an ellipsis * "truncateTail" - the line is displayed so that the beginning fits in the frame and the missing text at the end of the line is indicated by an ellipsis * "truncateMiddle" - the line is displayed so that the beginning and end fit in the frame and the missing text in the middle is indicated by an ellipsis
Returns
The drawing object
Notes
This method should only be used on text drawing objects
If the text of the drawing object is currently empty (i.e. "") then style changes may be lost. Use a placeholder such as a space (" ") or hide the object if style changes need to be saved but the text should disappear for a while.
Only the keys specified are changed. To reset an object to all of its defaults, call this method with an explicit nil as its only parameter (e.g. hs.drawing:setTextStyle(nil)
The font, font size, and font color can also be set by their individual specific methods as well; this method is provided so that style components can be stored and applied collectively, as well as used by hs.drawing.getTextDrawingSize() to determine the proper rectangle size for a textual drawing object.