Hammerspoon macOS Automation

Problem

Need AHK-level desktop automation on macOS. AutoHotkey is Windows-only; macOS lacks a native scripting equivalent for window management, hotkeys, and system event handling.

Solution

Hammerspoon bridges macOS APIs and Lua scripting, providing deterministic OS control via the Accessibility API rather than input simulation.

Platform Context

ToolPlatformApproach
AutoHotkeyWindows onlySimulation-based (“sends keystrokes”)
HammerspoonmacOS onlyState-based (talks to window server/Accessibility API)
AutoKeyLinux only (X11)Python scripting with X11 hooks

These tools cannot coexist on the same machine — each covers one OS.

Feature Comparison vs AHK

FeatureAutoHotkeyHammerspoonEdge
Hotkeys/RemappingGold standard, nativehs.hotkey.bind — stable, slightly verboseDraw
Window ManagementRequires math for snappingSuperior with hs.window + YabaiHammerspoon
GUI CreationEasy input boxes, menus, formsHard — hs.webview/hs.canvas onlyAHK
Text ExpansionNative HotstringsWeak — use Espanso insteadAHK
System EventsLimited file watchersSuperior: USB, WiFi, battery, audio, screenHammerspoon
Standalone ExportCompiles to .exeRequires Hammerspoon installedAHK

Key Advantages

Watcher Pattern

AHK polls for changes (loops checking window existence). Hammerspoon uses event watchers — code fires instantly on WiFi connect, USB plug, audio device change.

Drawing Engine

hs.drawing and hs.canvas create visual overlays and HUDs far more elegantly than AHK’s tooltip system.

REPL Console

Built-in console for live Lua execution and testing — no save-and-reload cycle.

  • Hammerspoon — Logic, window management, system events
  • Karabiner-Elements — Low-level key remapping (before OS sees input)
  • Espanso — Cross-platform text expansion

Advanced: Productivity Analysis

Hammerspoon can access macOS databases (knowledgeC.db) to detect unproductive app-switching “death loops” and trigger AI-powered interventions.