Fundamentals 7 min read

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.

Huajiao Technology
Huajiao Technology
Huajiao Technology
Unlock macOS Automation: A Practical Guide to AppleScript Basics

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 tell

Variables – 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 if

Loops

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

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

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

Conclusion

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

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

UI automationScriptingAppleScriptmacOS automationAppleScript tutorial
Huajiao Technology
Written by

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.

0 followers
Reader feedback

How this landed with the community

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.