Introduction to AppleScript: Features, Editor, Basic Syntax, and Sample Scripts
This article introduces AppleScript—a macOS‑built scripting language that enables automation by controlling the system and applications—covers its distinctive features, the built‑in Script Editor, fundamental syntax such as dialogs, variables, conditionals, loops, and provides practical code examples for tasks like launching Safari, handling UI elements, and automating QQ login.
AppleScript is a scripting language introduced by Apple, built into macOS, allowing direct control of the operating system and its applications; it originated from the HyperTalk language used in HyperCard.
Unlike other scripting languages such as Python and JavaScript, AppleScript’s most notable characteristic is its ability to control other macOS applications, using a syntax that closely resembles natural language and providing a built‑in dictionary for easy syntax lookup.
macOS includes a built‑in Script Editor that supports AppleScript, offering template projects, code snippets, and application dictionaries, which makes writing scripts convenient.
Basic AppleScript syntax includes a simple "Hello, world!" dialog:
display dialog "Hello, world!"Common constructs demonstrated are variable assignment, conditional statements, and repeat loops.
Variable assignment example:
set huajiao to "https://www.huajiao.com/"Conditional statement example:
if num > 2 then
open location huajiao
else
open location baidu
end ifLoop statement example:
repeat with num in [1, 2, 3]
display dialog "hello,world"
end repeatAppleScript can also simulate UI interactions by referencing UI elements such as buttons and windows. For instance, to click a checkbox in a window:
click checkbox 2 of window "Window"To input text into a field:
set value of text field 1 of window "Window" of application process "QQ" to usernameKey codes can be sent to simulate keystrokes, and UI Browser (a third‑party tool) helps discover UI element names for precise automation.
Application example – 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 tellIn summary, AppleScript is a simple yet powerful macOS automation language that enables UI control and inter‑application scripting without requiring deep programming knowledge, and its full syntax reference is available in Apple’s official "Introduction to AppleScript Language Guide".
360 Tech Engineering
Official tech channel of 360, building the most professional technology aggregation platform for the brand.
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.