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
const image = await HSImage.fromURL("https://example.com/image.png")

Image Manipulation

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

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

// Resize image
const resized = img.setSize({w: 100, h: 100}, false)  // Proportional

// Crop image
const cropped = img.croppedCopy({x: 10, y: 10, w: 50, h: 50})

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

Properties

This type has no properties.

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
HSImage
An HSImage object, or null if the file couldn't be loaded

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")
HSImage
An HSImage object, or null if the image couldn't be found

staticHSImage.fromAppBundle(bundleID) -> HSImage

Load an app's icon by bundle identifier
HSImage.fromAppBundle(bundleID) -> HSImage
Name Type Description
bundleID string Bundle identifier of the application
HSImage
An HSImage object, or null if the app couldn't be found

staticHSImage.iconForFile(path) -> HSImage

Get the icon for a file
HSImage.iconForFile(path) -> HSImage
Name Type Description
path string Path to the file
HSImage
An HSImage object representing the file's icon

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")
HSImage
An HSImage object representing the file type's icon

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

size(size) -> JSValue

Get or set the image size
size(size) -> JSValue
Name Type Description
size JSValue Optional HSSize to set (if provided, returns a resized copy)
JSValue
The current size as HSSize, or a resized copy if size was provided

name(name) -> string

Get or set the image name
name(name) -> string
Name Type Description
name JSValue Optional name to set
string
The current or new name

setSize(size, absolute) -> HSImage

Create a resized copy of the image
setSize(size, absolute) -> HSImage
Name Type Description
size JSValue Target size as HSSize
absolute boolean If true, resize exactly to specified dimensions. If false, maintain aspect ratio
HSImage
A new resized HSImage

copyImage() -> HSImage

Create a copy of the image
copyImage() -> HSImage
HSImage
A new HSImage copy

croppedCopy(rect) -> HSImage

Create a cropped copy of the image
croppedCopy(rect) -> HSImage
Name Type Description
rect JSValue HSRect defining the crop area
HSImage
A new cropped HSImage, or null if cropping failed

saveToFile(path) -> boolean

Save the image to a file
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

template(state) -> boolean

Get or set the template image flag
template(state) -> boolean
Name Type Description
state JSValue Optional boolean to set template state
boolean
Current template state

set(value) -> None

Replace the image with a new one, triggering a re-render if bound to a UI element
set(value) -> None
Name Type Description
value JSValue New image as an HSImage object or a file path string
None