darwin

package
v1.3.9 Latest Latest
Warning

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

Go to latest
Published: Dec 1, 2026 License: Apache-2.0 Imports: 21 Imported by: 0 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

Rendered for darwin/amd64

Overview

Package darwin provides a pure-Go macOS implementation of the robotgo API for desktop automation: mouse, keyboard and screen capture.

Unlike the upstream go-vgo/robotgo, this package uses NO Cgo. It is built entirely on top of the Quartz/CoreGraphics and CoreFoundation system frameworks, dynamically loaded at runtime through github.com/ebitengine/purego, so it cross-compiles with CGO_ENABLED=0.

The exported API mirrors the Windows and Wayland sibling packages (github.com/go-vgo/robotgo/win, github.com/go-vgo/robotgo/wayland) so callers can swap implementations per platform with a build tag.

Most input and screen-capture APIs silently fail (or return empty results) unless the host process has been granted Accessibility and Screen Recording permissions in System Settings → Privacy & Security. Window management (GetTitle/GetBounds/ActivePid/ActiveName/MinWindow/MaxWindow/CloseWindow) uses the Accessibility (AXUIElement) API and needs the Accessibility permission; MaxWindow toggles native full screen. Write operations never fall back to the frontmost window for a pid <= 0, except CloseWindow() called with no argument.

Index

Constants

View Source
const Version = "v0.1.0-darwin"

Version is the robotgo-darwin version string.

Variables

View Source
var ErrNotFound = errors.New("robotgo: not found")

ErrNotFound is returned when no matching window or element is found.

View Source
var ErrNotSupported = errors.New("robotgo: operation not supported")

ErrNotSupported is returned when an operation is not supported by this pure-Go backend.

Functions

func ActiveName

func ActiveName(name string) error

ActiveName activates the app whose name best matches name (exact, then prefix, then substring; case insensitive) among apps with a window.

func ActivePid added in v1.3.2

func ActivePid(pid int, args ...int) error

ActivePid brings the application with pid to the foreground, restoring and raising its window. args are accepted for API parity: as in Cgo, pid is always an app pid.

func Alert added in v1.3.4

func Alert(title, msg, ok, cancel string) bool

Alert shows a modal alert with the ok and cancel buttons and reports whether ok was chosen; false on failure.

func Capture

func Capture(args ...int) (*image.RGBA, error)

Capture captures the screen and returns an *image.RGBA.

func CaptureImg

func CaptureImg(args ...int) (image.Image, error)

CaptureImg captures the screen and returns an image.Image. Optional args: x, y, w, h (region) and a trailing display index. With no args the full main display is captured.

func CheckAccess added in v1.2.0

func CheckAccess(prompt bool) bool

CheckAccess reports whether the process is trusted for Accessibility (needed to post input events); prompt shows the system dialog if not.

func Click

func Click(args ...interface{}) error

Click clicks a mouse button. Default is the left button. Use "left", "right", or "center". Pass true for a double-click.

func CloseWindow

func CloseWindow(args ...int) error

CloseWindow closes the frontmost app's window (no argument), or the window of the pid given as the first argument, by pressing its close button.

func CmdCtrl

func CmdCtrl() string

CmdCtrl returns "cmd" on macOS.

func DisplaysNum

func DisplaysNum() int

DisplaysNum returns the number of active displays.

func DragSmooth

func DragSmooth(x, y int, args ...interface{}) error

DragSmooth moves the mouse smoothly while holding the left button down.

func GetActiveApp added in v1.2.0

func GetActiveApp() (name, bundleID string, pid int)

GetActiveApp returns the frontmost app's localized name, bundle id and pid. The pid comes from the live AX / window server state (NSWorkspace's frontmostApplication is cached without a main run loop).

func GetBounds added in v1.3.2

func GetBounds(pid int, args ...int) (x, y, w, h int)

GetBounds returns the window bounds (x, y, w, h) of pid's window; pid <= 0 selects the frontmost app. It returns zeros when not reachable. args are accepted for API parity: as in Cgo, pid is always an app pid.

func GetMousePos

func GetMousePos() (int, int)

GetMousePos returns the current mouse position. Alias of Location.

func GetPid

func GetPid() int

GetPid returns the current process's PID.

func GetPixelColor

func GetPixelColor(x, y int, displayId ...int) string

GetPixelColor returns the pixel color at (x, y) as a 6-digit hex string.

func GetScaleSize

func GetScaleSize(displayId ...int) (int, int)

GetScaleSize returns the screen size in physical pixels (point size times the backing scale), matching robotgo.GetScaleSize on the Cgo backend.

func GetScreenSize

func GetScreenSize() (int, int)

GetScreenSize returns the main display's width and height in pixels.

func GetTitle

func GetTitle(args ...int) string

GetTitle returns the title of the frontmost app's window, or of the window of the pid given as the first argument. It returns "" when the window is not reachable (e.g. without the Accessibility permission).

func GetVersion

func GetVersion() string

GetVersion returns the robotgo version.

func Height

func Height(img image.Image) int

Height returns the height of an image.

func KeyDown

func KeyDown(key string, args ...interface{}) error

KeyDown presses a key down.

func KeyPress

func KeyPress(key string, args ...interface{}) error

KeyPress presses and releases a key (alias of KeyTap).

func KeyTap

func KeyTap(key string, args ...interface{}) error

KeyTap taps a key (press + release). Trailing arguments may be modifier names (or a []string of them) and an int pid: when a pid is supplied the event is delivered to that process via CGEventPostToPid, mirroring the C SendTo() helper; otherwise it is posted to the global HID event tap.

KeyTap("a")
KeyTap("a", "cmd")
KeyTap("a", "cmd", "shift")
KeyTap("a", pid)
KeyTap("a", pid, "cmd")

func KeyToggle

func KeyToggle(key string, args ...interface{}) error

KeyToggle toggles a key. Default is "down"; pass "up" to release. Trailing arguments may be modifier names (or a []string of them) and an int pid: when a pid is supplied the event is delivered to that process via CGEventPostToPid, mirroring the C SendTo() helper.

KeyToggle("a")
KeyToggle("a", "up")
KeyToggle("a", "up", "cmd")
KeyToggle("a", pid)

func KeyUp

func KeyUp(key string, args ...interface{}) error

KeyUp releases a key.

func Kill

func Kill(pid int) error

Kill kills a process by PID.

func Location

func Location() (int, int)

Location returns the current mouse position.

func MainDisplayID

func MainDisplayID() int

MainDisplayID returns the main display's CGDirectDisplayID, mirroring the Cgo backend's GetMainId. Returns 0 when the frameworks are unavailable.

func MaxWindow

func MaxWindow(pid int, args ...interface{}) error

MaxWindow enters (or exits, if the bool arg is false) native full screen for pid's window.

func MinWindow

func MinWindow(pid int, args ...interface{}) error

MinWindow minimizes (or restores, if the bool arg is false) pid's window.

func MouseDown

func MouseDown(key ...interface{}) error

MouseDown sends a mouse button down event.

func MouseUp

func MouseUp(key ...interface{}) error

MouseUp sends a mouse button up event.

func Move

func Move(x, y int, displayId ...int) error

Move moves the mouse to absolute position (x, y). The optional displayId is accepted for API parity but ignored.

func MoveClick

func MoveClick(x, y int, args ...interface{}) error

MoveClick moves to (x, y) then clicks.

func MoveRelative

func MoveRelative(x, y int) error

MoveRelative moves the mouse relative to its current position.

func MoveSmooth

func MoveSmooth(x, y int, args ...interface{}) bool

MoveSmooth moves the mouse smoothly to (x, y) with an ease-in-out curve. Optional args: steps (int), sleepMs (int). Returns true on success.

func MultiClick added in v1.3.2

func MultiClick(button string, count int) error

MultiClick clicks button count times at the current location; the n-th click carries click state n, so macOS sees a double/triple... click.

func PadHex

func PadHex(hex uint32) string

PadHex pads a hex color value to 6 characters.

func Run

func Run(path string) ([]byte, error)

Run runs a shell command and returns its combined output.

func Save

func Save(img image.Image, path string, quality ...int) error

Save saves an image to a file. Format is chosen from the extension.

func SaveCapture

func SaveCapture(path string, args ...int) error

SaveCapture captures the screen and saves it to a file.

func SaveJpeg

func SaveJpeg(img image.Image, path string, quality ...int) error

SaveJpeg saves an image as JPEG.

func SavePng

func SavePng(img image.Image, path string) error

SavePng saves an image as PNG.

func ScaleF

func ScaleF(displayId ...int) float64

ScaleF returns the backing scale factor (pixel width / point width) of the selected display, mirroring the Cgo backend's sys_scale. Returns 1 when the frameworks are unavailable or the mode cannot be read.

func Scroll

func Scroll(x, y int, args ...int) error

Scroll scrolls the mouse by wheel notches. Positive y scrolls up, negative scrolls down; positive x scrolls left, negative scrolls right (matching robotgo's Cgo backend convention). Optional arg: delay ms.

func ScrollDir

func ScrollDir(x int, direction ...interface{}) error

ScrollDir scrolls in a named direction: "up", "down", "left", "right".

func ScrollSmooth

func ScrollSmooth(to int, args ...int) error

ScrollSmooth scrolls the mouse smoothly by `to` steps, repeating `num` times (default 5) with `tm` ms between steps (default 100). An optional third arg sets the horizontal offset per step.

func Toggle

func Toggle(key ...interface{}) error

Toggle toggles a mouse button down or up.

Toggle("left")        // press down
Toggle("left", "up")  // release

func Type

func Type(str string, args ...int) int

Type types a string using Unicode key events, supporting any character (including those not on the active keyboard layout). An optional first int argument is the target pid: when non-zero the events are delivered to that process via CGEventPostToPid, mirroring the C SendTo() helper.

Type("hello")
Type("hello", pid)

It returns the number of characters (runes) typed and stops at the first event that can not be posted.

func TypeDelay

func TypeDelay(str string, delay int) error

TypeDelay types a string with a per-character delay in milliseconds.

func TypeStr

func TypeStr(str string, args ...int) error

TypeStr types a string, mirroring the robotgo API. It returns an error if not every character was typed.

func Width

func Width(img image.Image) int

Width returns the width of an image.

Types

type CGFloat

type CGFloat = float64

CGFloat is a 64-bit C double on every 64-bit Apple platform (amd64/arm64).

type CGPoint

type CGPoint struct {
	X, Y CGFloat
}

CGPoint mirrors the C struct CGPoint { CGFloat x, y; }.

type CGRect

type CGRect struct {
	Origin CGPoint
	Size   CGSize
}

CGRect mirrors the C struct CGRect { CGPoint origin; CGSize size; }.

type CGSize

type CGSize struct {
	Width, Height CGFloat
}

CGSize mirrors the C struct CGSize { CGFloat width, height; }.

type Nps

type Nps struct {
	Pid  int
	Name string
}

Nps represents a process.

type Point

type Point struct {
	X, Y int
}

Point represents a 2D point.

type Rect

type Rect struct {
	Point
	Size
}

Rect represents a rectangle.

func GetScreenRect

func GetScreenRect(displayId ...int) Rect

GetScreenRect returns the selected display's bounds (origin in global display space, size in points), defaulting to the main display.

type Size

type Size struct {
	W, H int
}

Size represents a 2D size.

Jump to

Keyboard shortcuts

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