appshot

package
v0.4.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 30, 2026 License: MIT Imports: 7 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package appshot captures a running macOS application's accessibility context and an optional PNG of the same window.

Use Capture to collect an appshot. WriteContext writes the formatted accessibility context, Export saves the capture as a directory, and ModelInputs prepares text and image inputs for a model client.

The context and image may contain sensitive information visible in the window.

Example
package main

import (
	"fmt"

	"github.com/tmc/axmcp/appshot"
)

func main() {
	opts, err := (appshot.Options{Image: appshot.ImageOmit}).Normalize()
	if err != nil {
		return
	}
	fmt.Println(opts.Image, opts.TimeoutMS)
}
Output:
omit 30000

Index

Examples

Constants

This section is empty.

Variables

View Source
var ErrUnsupported = NewError("unsupported", errors.New("appshot capture requires macOS"))

ErrUnsupported reports that native capture is unavailable on this platform.

Functions

func ErrorCode

func ErrorCode(err error) string

ErrorCode returns the stable failure code or an empty string for unclassified errors. It recognizes context cancellation and deadline errors.

func Export

func Export(dir string, result Result) error

Export writes the capture to a new private directory. It fails if dir already exists and leaves the existing path intact. The parent directory must exist and its path must be free of symbolic links.

func ExportChecked

func ExportChecked(ctx context.Context, dir string, result Result, access func(context.Context) error) error

ExportChecked writes the capture to a new private directory. If access is non-nil, it runs immediately before publication. A failed check prevents publication. The caller supplies any live authorization checks.

func NewError

func NewError(code string, err error) error

NewError returns an error with the given code and cause.

func WriteContext

func WriteContext(w io.Writer, result Result) error

WriteContext writes the capture's formatted accessibility context to w.

Example
package main

import (
	"os"

	"github.com/tmc/axmcp/appshot"
)

func main() {
	result := appshot.Result{
		Context: `<appshot app="TextEdit" bundle-identifier="com.apple.TextEdit">TextEdit</appshot>`,
	}
	if err := appshot.WriteContext(os.Stdout, result); err != nil {
		return
	}
}
Output:
<appshot app="TextEdit" bundle-identifier="com.apple.TextEdit">TextEdit</appshot>

Types

type AppInfo

type AppInfo struct {
	Name     string `json:"name,omitempty"`
	BundleID string `json:"bundle_id,omitempty"`
	PID      int    `json:"pid,omitempty"`
}

AppInfo identifies the running process observed by capture.

type DisplayInfo

type DisplayInfo struct {
	ID          uint32 `json:"id"`
	Bounds      Rect   `json:"bounds"`
	PixelWidth  int    `json:"pixel_width"`
	PixelHeight int    `json:"pixel_height"`
}

DisplayInfo describes a display's logical bounds and physical dimensions.

type Error

type Error struct {
	Code string
	Err  error
}

Error carries a stable failure code and its underlying cause.

func (*Error) Error

func (e *Error) Error() string

Error returns the failure code and cause.

func (*Error) Unwrap

func (e *Error) Unwrap() error

Unwrap returns the underlying cause.

type ImageInfo

type ImageInfo struct {
	Name     string        `json:"name"`
	MIMEType string        `json:"mime_type"`
	Metadata ImageMetadata `json:"metadata"`
}

ImageInfo describes the optional PNG attachment.

type ImageMetadata

type ImageMetadata struct {
	ImageID      string        `json:"image_id"`
	Width        int           `json:"width"`
	Height       int           `json:"height"`
	SourceKind   string        `json:"source_kind"`
	TargetWindow uint32        `json:"target_window"`
	GlobalRect   Rect          `json:"global_rect"`
	ScaleX       float64       `json:"scale_x"`
	ScaleY       float64       `json:"scale_y"`
	Displays     []DisplayInfo `json:"displays"`
}

ImageMetadata binds PNG pixels to their source window. Scale factors measure pixels per logical point on each axis.

type ImagePolicy

type ImagePolicy string

ImagePolicy controls whether capture includes a window image.

const (
	// ImageAuto includes an image when available and otherwise returns context alone.
	ImageAuto ImagePolicy = "auto"
	// ImageRequired makes an unavailable image a capture error.
	ImageRequired ImagePolicy = "required"
	// ImageOmit captures accessibility context alone.
	ImageOmit ImagePolicy = "omit"
)

type ModelInput

type ModelInput struct {
	Type     string `json:"type"`
	Text     string `json:"text,omitempty"`
	ImageURL string `json:"image_url,omitempty"`
	Detail   string `json:"detail,omitempty"`
}

ModelInput contains either text or an image data URL for a model client.

func ModelInputs

func ModelInputs(result Result) ([]ModelInput, error)

ModelInputs validates a capture and returns its context as an input_text item followed, when available, by an input_image item containing the PNG.

Example
package main

import (
	"fmt"

	"github.com/tmc/axmcp/appshot"
)

func main() {
	result := appshot.Result{
		Version:   1,
		AppshotID: "example",
		App:       appshot.AppInfo{Name: "TextEdit", BundleID: "com.apple.TextEdit"},
		Context:   `<appshot app="TextEdit" bundle-identifier="com.apple.TextEdit">TextEdit</appshot>`,
	}
	inputs, err := appshot.ModelInputs(result)
	if err != nil {
		return
	}
	fmt.Println(inputs[0].Type)
}
Output:
input_text

type Options

type Options struct {
	Image        ImagePolicy `json:"image,omitempty"`
	TimeoutMS    int         `json:"timeout_ms,omitempty"`
	MaxNodes     int         `json:"max_nodes,omitempty"`
	MaxTextBytes int         `json:"max_text_bytes,omitempty"`
}

Options sets the limits and image policy for a capture. Zero values select the defaults documented by Options.Normalize.

func (Options) Normalize

func (o Options) Normalize() (Options, error)

Normalize returns the options with defaults applied and validates their limits. The defaults are ImageAuto, 30000 milliseconds, 2000 nodes, and 262144 bytes.

type ProcessStart

type ProcessStart struct {
	Seconds      int64 `json:"seconds"`
	Microseconds int64 `json:"microseconds"`
}

ProcessStart records when the operating system started the captured process.

type Rect

type Rect struct {
	X      float64 `json:"x"`
	Y      float64 `json:"y"`
	Width  float64 `json:"width"`
	Height float64 `json:"height"`
}

Rect describes a rectangle in global macOS logical points.

type Result

type Result struct {
	Version           int          `json:"version"`
	AppshotID         string       `json:"appshot_id"`
	CapturedAt        time.Time    `json:"captured_at"`
	CaptureStartedAt  time.Time    `json:"capture_started_at"`
	CaptureFinishedAt time.Time    `json:"capture_finished_at"`
	App               AppInfo      `json:"app"`
	ProcessStart      ProcessStart `json:"process_start"`
	Window            WindowInfo   `json:"window"`
	Context           string       `json:"context"`
	Tree              TreeInfo     `json:"tree"`
	Image             *ImageInfo   `json:"image,omitempty"`
	Warnings          []string     `json:"warnings"`
	PNG               []byte       `json:"-"`
	// contains filtered or unexported fields
}

Result holds an owned capture. Its JSON encoding uses version-1 metadata with PNG bytes kept in the separate PNG field.

func Capture

func Capture(ctx context.Context, app string, windowID uint32, opts Options) (Result, error)

Capture returns ErrUnsupported on platforms other than macOS.

func (Result) ScreenRecordingRequired

func (r Result) ScreenRecordingRequired() bool

ScreenRecordingRequired reports whether the capture needs screen recording access when it is published. A capture that observed a grant can require this check even when its result contains context alone.

type TreeInfo

type TreeInfo struct {
	NodeCount           int    `json:"node_count"`
	Truncated           bool   `json:"truncated"`
	OmittedSecureValues int    `json:"omitted_secure_values"`
	Limit               string `json:"limit,omitempty"`
}

TreeInfo describes traversal and output limits for the accessibility context.

type WindowInfo

type WindowInfo struct {
	WindowID         uint32 `json:"window_id,omitempty"`
	Title            string `json:"title,omitempty"`
	X                int    `json:"x,omitempty"`
	Y                int    `json:"y,omitempty"`
	Width            int    `json:"width,omitempty"`
	Height           int    `json:"height,omitempty"`
	ScreenshotWidth  int    `json:"screenshot_width,omitempty"`
	ScreenshotHeight int    `json:"screenshot_height,omitempty"`
}

WindowInfo describes the target window in logical points and the PNG size in pixels.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL