ターミナルからの osascript コマンド実行から macOS アプリケーションの自動操作まで、AppleScript の基本構文を網羅したクイックリファレンス。
AppleScript の実行
ターミナルからの実行 (osascript)
# インライン式の実行
osascript -e 'display notification "Task completed!" with title "Build Script"'
# スクリプトファイルの実行 (.scpt または .applescript)
osascript script.scpt
ネイティブシステム通知
display notification "Backup finished successfully" with title "System Status" subtitle "Automated Backup"
コメント記法
-- 1行コメント
# これも1行コメント
(*
複数行の
コメントブロック
*)
サウンドとオーディオ
音声合成 (say)
-- デフォルトのシステム音声
say "Build succeeded"
-- 指定したシステム音声
say "Warning: low battery" using "Zarvox"
say "All systems nominal" using "Samantha"
システム警告音 (Beep)
-- 1回ビープ音を鳴らす
beep
-- 複数回ビープ音を鳴らす
beep 3
実行待機 (Delay)
-- 5秒間処理を一時停止
delay 5
ハンドラ (関数 / Handlers)
on doubleNumber(n)
return n * 2
end doubleNumber
set doubledValue to my doubleNumber(21)
-- → 42
複数引数を取るハンドラ
on showDialog(msg, titleText)
display alert titleText message msg as informational
end showDialog
my showDialog("Operation completed.", "Success")
アプリケーションの操作
tell application "Finder"
activate
open folder "Documents" of home
end tell
tell application "Google Chrome"
reload active tab of front window
end tell