AgentSkillsCN

macos-automation

控制 macOS 的系统设置与应用(深色模式、音量、应用、访达、专注模式)。

SKILL.md
--- frontmatter
name: macos-automation
description: Control macOS settings and apps (dark mode, volume, apps, Finder, Focus).
tags: [macos, automation, settings, utility]

macOS Automation

Control macOS system settings and applications via shell commands and AppleScript.

Appearance

Toggle dark mode:

code
exec: osascript -e 'tell application "System Events" to tell appearance preferences to set dark mode to not dark mode'

Check current mode:

code
exec: osascript -e 'tell application "System Events" to tell appearance preferences to return dark mode'

Set dark mode on:

code
exec: osascript -e 'tell application "System Events" to tell appearance preferences to set dark mode to true'

Set light mode:

code
exec: osascript -e 'tell application "System Events" to tell appearance preferences to set dark mode to false'

Volume Control

Get current volume:

code
exec: osascript -e 'output volume of (get volume settings)'

Set volume (0–100):

code
exec: osascript -e 'set volume output volume 50'

Mute:

code
exec: osascript -e 'set volume output muted true'

Unmute:

code
exec: osascript -e 'set volume output muted false'

Toggle mute:

code
exec: osascript -e '
set currentMute to output muted of (get volume settings)
set volume output muted (not currentMute)
if currentMute then
    return "Unmuted"
else
    return "Muted"
end if
'

Brightness (Display)

Get brightness (requires brightness tool or direct call):

code
exec: osascript -e '
tell application "System Events"
    tell appearance preferences
        return "Use brightness CLI for direct control"
    end tell
end tell
'

Via brightness CLI (install: brew install brightness):

code
exec: brightness -l
code
exec: brightness 0.7

Application Management

Launch an app:

code
exec: open -a "APP_NAME"

Quit an app:

code
exec: osascript -e 'tell application "APP_NAME" to quit'

Force quit:

code
exec: pkill -9 "APP_NAME"

List running apps:

code
exec: osascript -e '
tell application "System Events"
    set output to ""
    repeat with proc in (every process whose background only is false)
        set output to output & name of proc & linefeed
    end repeat
    return output
end tell
'

Check if app is running:

code
exec: osascript -e '
tell application "System Events"
    return (name of every process) contains "APP_NAME"
end tell
'

Bring app to front:

code
exec: osascript -e 'tell application "APP_NAME" to activate'

Finder

Open folder in Finder:

code
exec: open /path/to/folder

Reveal file in Finder:

code
exec: open -R /path/to/file

Get selected files in Finder:

code
exec: osascript -e '
tell application "Finder"
    set output to ""
    repeat with f in (selection as list)
        set output to output & (POSIX path of (f as alias)) & linefeed
    end repeat
    return output
end tell
'

Empty Trash:

code
exec: osascript -e 'tell application "Finder" to empty trash'

Get Trash size:

code
exec: du -sh ~/.Trash 2>/dev/null

Wi-Fi

Turn Wi-Fi on:

code
exec: networksetup -setairportpower Wi-Fi on

Turn Wi-Fi off:

code
exec: networksetup -setairportpower Wi-Fi off

Current Wi-Fi network:

code
exec: networksetup -getairportnetwork Wi-Fi

Bluetooth

Check Bluetooth status:

code
exec: defaults read /Library/Preferences/com.apple.Bluetooth ControllerPowerState

Toggle via blueutil (install: brew install blueutil):

code
exec: blueutil --power toggle

Do Not Disturb / Focus

Check Focus status (macOS 12+):

code
exec: plutil -p ~/Library/DoNotDisturb/DB/Assertions.json 2>/dev/null | head -20

Sleep / Lock / Restart

Lock screen:

code
exec: osascript -e 'tell application "System Events" to keystroke "q" using {command down, control down}'

Put display to sleep:

code
exec: pmset displaysleepnow

Start screen saver:

code
exec: open -a ScreenSaverEngine

Keyboard Input

Type text:

code
exec: osascript -e 'tell application "System Events" to keystroke "TEXT"'

Press key combo:

code
exec: osascript -e 'tell application "System Events" to keystroke "c" using command down'

Press return:

code
exec: osascript -e 'tell application "System Events" to key code 36'

Open URLs

code
exec: open "https://example.com"

With specific browser:

code
exec: open -a "Safari" "https://example.com"

Notes

  • Some operations require Accessibility permission in System Settings > Privacy & Security > Accessibility.
  • System Events automation requires user approval on first use.
  • Wi-Fi interface name may vary (Wi-Fi, en0). Check with networksetup -listallhardwareports.
  • Sleep/restart commands may not work in all contexts.
  • Combine with manage-cron for scheduled automation (e.g., toggle dark mode at sunset).