Unlock macOS Automation: A Practical Guide to AppleScript Basics
This article introduces AppleScript, explains its unique ability to control macOS applications, walks through the built‑in editor, and provides clear examples of core syntax such as tell statements, variables, conditionals, loops, and UI interaction for automating tasks on macOS.
What Is AppleScript
AppleScript is a scripting language introduced by Apple, built into macOS, that allows direct control of the operating system and its applications, making it a powerful tool for macOS automation. It originated from HyperTalk, the language used in HyperCard.
Key Features
Compared with other scripting languages like Python and JavaScript, AppleScript’s most notable feature is its ability to control other macOS applications. Its syntax is simple and close to natural language, enabling users to script tasks as if they were having a conversation with the system. macOS also provides a dictionary for easy syntax lookup.
AppleScript Editor
macOS includes a built‑in Script Editor that supports AppleScript. The editor offers template projects, sample code, and access to application dictionaries, making script authoring convenient.
Basic Syntax
A classic "Hello, world!" example: display dialog "Hello, world!" Running this script displays a dialog box with the message.
Common Constructs
Tell Statements – Direct an application to perform actions.
tell application "Safari"
activate
open location "https://www.huajiao.com/"
end tellVariables – Assign values for reuse.
set huajiao to "https://www.huajiao.com/"Conditionals
if num > 2 then
open location huajiao
else
open location "http://www.baidu.com"
end ifLoops
repeat with i in [1, 2, 3]
display dialog "hello, world"
end repeatUI Interaction – Simulate clicks and keystrokes on UI elements. First, discover UI element names using entire contents or the built‑in dictionary. Third‑party tools like UI Browser can also list element names. Example of clicking a checkbox and typing text:
click checkbox 2 of window "Window"
keystroke "abc"Practical AppleScript Examples
Automating QQ Login
set username to "123456789"
set qqpassword to "zhoujielun"
-- Launch QQ
tell application "QQ"
activate
end tell
delay 1
-- Enter credentials
tell application "System Events"
set value of text field 1 of window "Window" of application process "QQ" to username
delay 2
set value of text field 2 of window "Window" of application process "QQ" to qqpassword
delay 1
click checkbox 3 of window "Window" of application process "QQ"
end tellSending an Email Automatically
set theSubject to "邮件标题"
set recipientName to "test"
set recipientAddress to "[email protected]"
set theContent to "我是正文"
tell application "Mail"
set theMessage to make new outgoing message with properties {subject:theSubject, content:theContent, visible:false}
tell theMessage
make new to recipient with properties {name:recipientName, address:recipientAddress}
end tell
send theMessage
end tellConclusion
AppleScript is a straightforward UI‑automation language on macOS that requires little programming background. Many built‑in and third‑party apps expose rich scripting interfaces, allowing users to chain applications together for smooth automated workflows. Detailed syntax can be found in Apple’s official "Introduction to AppleScript Language Guide".
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Huajiao Technology
The Huajiao Technology channel shares the latest Huajiao app tech on an irregular basis, offering a learning and exchange platform for tech enthusiasts.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
