AppleScript

12.22'21

AppleScript 是用于 MacOS 的自动化脚本语言

Hello, world!

Run in Script Editor

  • Launch Script Editor

  • Type

    tell application "System Events" to display dialog "Hello world!"
    
  • Click Run the Script

Run in terminal

hello.applescript

tell application "System Events" to display dialog "Hello world!"
osascript hello.applescript

例子

打开 iTerm,并切换到指定目录

on run argv
  set path to (quoted form of (item 1 of argv))
  set command to "clear; cd " & path
  
  tell application "System Events"
    -- some versions might identify as "iTerm2" instead of "iTerm"
    set isRunning to (exists (processes where name is "iTerm")) or (exists (processes where name is "iTerm2"))
  end tell
  
  tell application "iTerm"
    activate
    set hasNoWindows to ((count of windows) is 0)
    if isRunning and hasNoWindows then
      create window with default profile
    end if
    select first window
    
    tell the first window
      if isRunning and hasNoWindows is false then
        create tab with default profile
      end if
      tell current session to write text command
    end tell
  end tell
end run
📖