Fundamentals 6 min read

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.

360 Tech Engineering
360 Tech Engineering
360 Tech Engineering
Introduction to AppleScript: Features, Editor, Basic Syntax, and Sample Scripts

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 if

Loop statement example:

repeat with num in [1, 2, 3]
    display dialog "hello,world"
end repeat

AppleScript 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 username

Key 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 tell

In 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".

automationscriptingAppleScriptAppleScript BasicsmacOS Automation
360 Tech Engineering
Written by

360 Tech Engineering

Official tech channel of 360, building the most professional technology aggregation platform for the brand.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.