API Docs

Bridge type for working with images in JavaScript

HSImage provides a comprehensive API for loading, manipulating, and saving images. It supports various image sources including files, system icons, app bundles, and URLs.

Loading Images

// Load from file
const img = HSImage.fromPath("/path/to/image.png")

// Load system image
const icon = HSImage.fromName("NSComputer")

// Load app icon
const appIcon = HSImage.fromAppBundle("com.apple.Safari")

// Load from URL (asynchronous with Promise)
HSImage.fromURL("https://example.com/image.png")
    .then(image => console.log("Image loaded:", image.size))
    .catch(err => console.error("Failed to load image:", err))

// Or with async/await
async function loadImage() {
    const image = await HSImage.fromURL("https://example.com/image.png")
    console.log("Image loaded:", image.size)
}

Image Manipulation

const img = HSImage.fromPath("/path/to/image.png")

// Get size
const size = img.size  // Returns HSSize

// Resize image (mutates in place)
img.size = new HSSize(100, 100)

// Crop image
const cropped = img.croppedCopy(new HSRect(10, 10, 50, 50))

// Save to file
img.saveToFile("/path/to/output.png")

Properties

size

The size of the image. Setting this resizes the image in place to the exact dimensions.

name

string

The name of the image, or null if not set.

template

boolean

Whether the image is a template image.

Template images are tinted by the system to match the appearance context (e.g. menu bar icons).

Methods

staticHSImage.fromPath(path) -> HSImage

Load an image from a file path

HSImage.fromPath(path) -> HSImage
Name Type Description
path string Path to the image file
An HSImage object, or null if the file couldn't be loaded
const img = HSImage.fromPath("/path/to/image.png")

staticHSImage.fromName(name) -> HSImage

Load a system image by name

HSImage.fromName(name) -> HSImage
Name Type Description
name string Name of the system image (e.g., "NSComputer", "NSFolder")
An HSImage object, or null if the image couldn't be found
const icon = HSImage.fromName("NSComputer")

staticHSImage.fromSymbol(name) -> HSImage

Load a system symbol by name

HSImage.fromSymbol(name) -> HSImage
Name Type Description
name string Name of the symbol (e.g., "hammer", "questionmark.circle")
An HSImage object, or null if the symbol couldn't be found
const sym = HSImage.fromSymbol("star.fill")

staticHSImage.fromAppBundle(bundleID, withFallbackSymbol) -> HSImage

Load an app's icon by bundle identifier

HSImage.fromAppBundle(bundleID, withFallbackSymbol) -> HSImage
Name Type Description
bundleID string Bundle identifier of the application
withFallbackSymbol optional string The name of an SF Symbol to use if no bundle image could be loaded. Defaults to questionmark.circle
An HSImage object, or null if the app couldn't be found
const appIcon = HSImage.fromAppBundle("com.apple.Safari")

staticHSImage.iconForFile(path) -> HSImage

Get the icon for a file

HSImage.iconForFile(path) -> HSImage
Name Type Description
path string Path to the file
An HSImage object representing the file's icon
const icon = HSImage.iconForFile("/Applications/Safari.app")

staticHSImage.iconForFileType(fileType) -> HSImage

Get the icon for a file type

HSImage.iconForFileType(fileType) -> HSImage
Name Type Description
fileType string File extension or UTI (e.g., "png", "public.png")
An HSImage object representing the file type's icon
const icon = HSImage.iconForFileType("pdf")

staticHSImage.fromURL(url) -> Promise<HSImage>

Load an image from a URL (asynchronous)

HSImage.fromURL(url) -> Promise<HSImage>
Name Type Description
url string URL string of the image
Promise<HSImage>
A Promise that resolves to the loaded image, or rejects on error
HSImage.fromURL("https://example.com/image.png")
    .then(img => console.log("Image loaded:", img.size))

staticHSImage.fromUint8Array(bytes) -> HSImage

Decode an image from encoded bytes in memory, without going through the disk.

This is the counterpart to toUint8Array(), accepting a Uint8Array, any other typed array, or an ArrayBuffer holding PNG, JPEG, TIFF, GIF, BMP, HEIC, or any other format supported by NSImage.

HSImage.fromUint8Array(bytes) -> HSImage
Name Type Description
bytes Uint8Array | ArrayBuffer The encoded image bytes
An HSImage object, or null if the input isn't a typed array/ArrayBuffer or couldn't be decoded as an image
const bytes = hs.pasteboard.readBytes("public.png")
const img = bytes ? HSImage.fromUint8Array(bytes) : null

duplicate() -> HSImage

Create a copy of the image

duplicate() -> HSImage
A new HSImage copy
const copy = img.duplicate()
copy.size = new HSSize(64, 64)

croppedCopy(rect) -> HSImage

Create a cropped copy of the image

The crop area is measured from the top left corner of the image, like the frames of screens and windows, so a screen snapshot can be cropped to a window's frame (after subtracting the screen's origin).

croppedCopy(rect) -> HSImage
Name Type Description
rect HSRect HSRect defining the crop area (x, y, w, h), with y measured down from the top of the image
A new cropped HSImage, or null if the rect falls outside the image bounds
const cropped = img.croppedCopy(new HSRect(10, 10, 80, 60))

saveToFile(path) -> boolean

Save the image to a file. The file is written atomically, so anything reading it sees either the previous contents or the complete new image, never a partial write.

saveToFile(path) -> boolean
Name Type Description
path string Destination file path (extension determines format: png, jpg, tiff, bmp, gif)
boolean
true if saved successfully, false otherwise
img.saveToFile("/tmp/output.png")

toDataURL(type) -> string

Encode the image and return it as a base64 data: URL, without writing anything to disk.

This is useful for handing an image to anything that accepts data URLs, such as web views or JSON messages sent to other programs.

toDataURL(type) -> string
Name Type Description
type optional string The image format, case-insensitive: "png" (the default), "jpeg"/"jpg", "tiff"/"tif", "bmp" or "gif"
string
A string such as "data:image/png;base64,iVBOR...", or null if the format is unknown or the image couldn't be encoded
const url = HSImage.fromSymbol("star.fill").toDataURL()
console.log(url.slice(0, 22))  // "data:image/png;base64,"
const jpeg = img.toDataURL("jpeg")

toUint8Array(type) -> Uint8Array | null

Encode the image and return its bytes as a Uint8Array, without writing anything to disk.

The bytes are the same as saveToFile() would write for that format, so they can be sent over a socket, used as an HTTP body, stored in a database, and so on.

toUint8Array(type) -> Uint8Array | null
Name Type Description
type optional string The image format, case-insensitive: "png" (the default), "jpeg"/"jpg", "tiff"/"tif", "bmp" or "gif"
Uint8Array | null
The encoded image bytes, or null if the format is unknown or the image couldn't be encoded
const bytes = HSImage.fromSymbol("star.fill").toUint8Array("png")
console.log(bytes.length)

set(value) -> None

Replace this image's content.

If this image is bound to a UI element, the canvas re-renders automatically.

set(value) -> None
Name Type Description
value string | HSImage A file path string (~ is expanded) or another HSImage object
None
const reactive = HSImage.fromName("NSStatusAvailable")
reactive.set("/path/to/image.png")
reactive.set(HSImage.fromName("NSStatusUnavailable"))