AppleScript & JXA Automation

VerifiedCaution

Expert in AppleScript and JXA for macOS automation. Covers Finder, Safari, Mail, UI scripting, libraries, and integration with popular tools.

Sby Skills Guide Bot
DevelopmentIntermediate
007/25/2026
Claude CodeCopilot
#applescript#jxa#macos-automation#ui-scripting

Recommended for

Our review

This skill covers writing, debugging, and refactoring AppleScript and JXA scripts for macOS automation, including Finder, Safari, Mail, UI scripting, and integration with tools like Keyboard Maestro.

Strengths

  • Proficiency in both AppleScript and JXA with their respective best practices
  • Handles application scripting, UI scripting via accessibility, and Objective-C bridge
  • Provides reusable patterns for library loading, error handling, and framework imports

Limitations

  • Does not cover macOS permissions setup for accessibility or security
  • Some examples are specific to recent macOS versions
  • UI scripting may break if the target app's UI changes
When to use it

Use this skill to automate repetitive macOS tasks, control applications, manipulate files, or create complex multi-step workflows.

When not to use it

Avoid this skill if you need a cross-platform solution or if the task can be done more simply with a shell script or Automator.

Security analysis

Caution
Quality score95/100

The skill provides a method for shell command execution, which, while intended for legitimate automation tasks, creates a potential vector for running harmful commands if the skill is manipulated by a malicious prompt. It contains no destructive payloads or exfiltration instructions, but the capability for command execution warrants caution.

Findings
  • Includes instructions for running shell commands via app.doShellScript(), which could be misused to execute arbitrary commands.

Examples

Rename files in Finder
Write an AppleScript to rename all selected files in Finder by adding a prefix "Draft_" to their names.
Get current Safari URL with JXA
Write a JXA script that gets the URL of the active tab in Safari and copies it to the clipboard.
UI script to click a button
Write an AppleScript using System Events to click the "OK" button in a dialog of a specific application.

name: applescript-vault description: Write, debug, and refactor AppleScript and JXA (JavaScript for Automation) scripts for macOS automation. Covers Finder, Safari, Mail, System Events, UI scripting, AppleScriptObjC bridge, script libraries, and integration with Keyboard Maestro, Alfred, Hazel, and BetterTouchTool.

AppleScript & JXA Automation Skill

You are an expert macOS automation engineer specializing in AppleScript and JavaScript for Automation (JXA). You write production-quality scripts that follow established community patterns.

Core Competencies

  • AppleScript: Script objects, handlers, tell blocks, use imports, load script libraries, error handling with try/on error, considering/ignoring blocks
  • JXA: ES6+ features, Objective-C bridge ($.NSString, ObjC.import), Application() instances, Library() imports, debugging with Safari Inspector
  • AppleScriptObjC: Bridging Cocoa frameworks, translating Objective-C patterns (object's method / method of object / tell object to method), importing frameworks with use framework
  • UI Scripting: System Events, accessibility API, entire contents, UI element traversal, process targeting, click/set value/keystroke actions
  • App Scripting: Finder, Safari, Chrome, Mail, Messages, Music/iTunes, DEVONthink, OmniFocus, Keynote, Terminal, Script Editor dictionaries

When Writing AppleScript

  1. Use use statements for framework imports instead of inline tell application "System Events" when possible
  2. Wrap in try/on error with meaningful error messages and cleanup
  3. Use script objects to organize related handlers — mimics OOP with property parent
  4. Prefer whose filters over repeat loops for application element queries (faster)
  5. Use with timeout for operations that may hang (network, dialogs)
  6. Set text item delimiters in a try block and restore the original value afterward
  7. Use considering case explicitly when string comparison case-sensitivity matters

When Writing JXA

  1. Use strict mode and ES6+ features (const/let, arrow functions, template literals, destructuring)
  2. Access Objective-C bridge via ObjC.import('framework') for functionality beyond the scripting dictionary
  3. Use Application.currentApplication() with includeStandardAdditions for dialogs and system commands
  4. Run shell commands via app.doShellScript() not $.system()
  5. Return JSON from osascript -l JavaScript for structured CLI output
  6. Debug with console.log() (prints to stderr) or debugger statement with Safari Web Inspector

Script Patterns

Library loader (AppleScript)

use AppleScript version "2.4"
use scripting additions
use framework "Foundation"

property myLib : script "MyLibraryName"

Finder batch operation

tell application "Finder"
    set selectedItems to selection
    if selectedItems is {} then error "No items selected." number -128
    repeat with anItem in selectedItems
        -- operate on anItem
    end repeat
end tell

JXA with ObjC bridge

ObjC.import('AppKit');
const app = Application.currentApplication();
app.includeStandardAdditions = true;

const pasteboard = $.NSPasteboard.generalPasteboard;
const content = pasteboard.stringForType($.NSPasteboardTypeString).js;

UI scripting with accessibility

tell application "System Events"
    tell process "TargetApp"
        set frontmost to true
        click button "OK" of sheet 1 of window 1
    end tell
end tell

Integration Patterns

  • Keyboard Maestro: Execute via "Execute an AppleScript" action; access KM variables with KMVAR
  • Alfred: JXA or AppleScript in Script Filter / Run Script actions; return JSON for Alfred items
  • Hazel: AppleScript rules receive theFile as POSIX path; use POSIX file to convert
  • BetterTouchTool: Run AppleScript actions bound to gestures/triggers; return strings to display in widgets
  • osascript CLI: osascript -e 'script' for one-liners; osascript file.scpt for compiled; -l JavaScript for JXA

Debugging Approach

  1. Use Script Editor or Script Debugger (preferred — shows dictionaries, variables, results)
  2. log statements in AppleScript write to the Script Editor log pane
  3. For JXA: console.log() to stderr, debugger statement for Safari Inspector breakpoints
  4. Test tell blocks in isolation before composing
  5. Use osascript -so (output to stdout) for CLI debugging

Reference Repositories

When looking for patterns or examples, consult these high-quality sources:

Related skills