惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

雷峰网
雷峰网
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
T
Tailwind CSS Blog
F
Fortinet All Blogs
Microsoft Azure Blog
Microsoft Azure Blog
Jina AI
Jina AI
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
N
Netflix TechBlog - Medium
B
Blog RSS Feed
Blog — PlanetScale
Blog — PlanetScale
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
T
The Blog of Author Tim Ferriss
D
Docker
博客园 - 聂微东
博客园 - 【当耐特】
博客园 - 三生石上(FineUI控件)
L
LangChain Blog
量子位
宝玉的分享
宝玉的分享
博客园 - 司徒正美
The Cloudflare Blog
G
Google Developers Blog
Microsoft Security Blog
Microsoft Security Blog
腾讯CDC

张小凯的博客

再见2025,你好2026! 下一代提示词工程语言POML简明教程 开了一个新的专门学习日语的博客 一、并行编程导论与CUDA入门 一些免费的GPU资源 debian12部署kubernetes-1.28集群 分享两个服务器实用脚本:xsync和xcall RSS订阅工具Folo使用 多平台消息推送工具ntfy使用 uv使用 gemini-cli使用 跑步一年多的一些总结和感想 通过GithubActions拉取并推送Docker镜像到国内云 使用AnythingLLM+SillconFlow+Milvus快速搭建个人云端知识库 さよなら2024、こんにちは2025! Excel通过身份证号列计算性别 开源的个人书籍管理系统Talebook 2024年安装Docker的方法 Python项目Linter、Formatter和Github-Actions配置
AppleScript介绍与简单实战
2025-07-15 · via 张小凯的博客

    1. AppleScript介绍与简单实战
      1. 一、什么是AppleScript
      2. 二、基础语法
        1. 1、告诉应用做某事
        2. 2、设置变量
        3. 3、条件语句
        4. 4、循环语句
        5. 5、模拟点击和输入
      3. 三、简单实战:命令完成后显示通知
      4. 四、一些常用脚本
        1. 1、通过参数发送系统通知
        2. 2、命令完成后展示结果
      5. 五、后记
    2. 附录

AppleScript是macOS内置的脚本语言,可以自动化操作应用程序。本文介绍了AppleScript的基本概念、语法和一个简单实战示例。


AppleScript介绍与简单实战

一、什么是AppleScript

AppleScript是苹果公司推出的一种脚本语言,内置于macOS中,可以直接操作和控制macOS及其应用程序。它是一个实现macOS自动化的强大工具,AppleScript的前身是HyperCard所使用的脚本语言HyperTalk。

与其他脚本语言如Python和JavaScript相比,AppleScript最显著的特点是能够控制其他macOS上的应用程序。通过AppleScript,我们可以完成一些繁琐重复的工作。其语法简单,接近自然语言,就像在和系统对话一样。此外,系统提供了语法查询字典,方便查询语法。


二、基础语法

按照惯例,用AppleScript写一个Hello World:

display dialog "Hello, world!"

命令行执行:

osascript -e 'display dialog "Hello, world!"'

运行后,系统会弹出“Hello, world!”的弹窗。

下面介绍几种常用语法:

1、告诉应用做某事

AppleScript的语法接近自然语言,例如:

tell application "Safari"
    activate
    open location "https://qq.com/"
end tell

这告诉Safari启动并打开指定网址。

2、设置变量

set qq to "https://qq.com/"
tell application "Safari"
    activate
    open location qq
end tell

3、条件语句

if num > 2 then
    // ...
else
    // ...
end if

4、循环语句

repeat with num in {1, 2, 3}
    display dialog "hello, world"
end repeat

5、模拟点击和输入

可以使用click命令模拟点击,或keystroke输入文本。


三、简单实战:命令完成后显示通知

执行以下命令可以展示一条通知:

osascript -e 'display notification "The command finished" with title "Success"'

在.zshrc中定义一个函数:

function notifyResult () {
  if [ $? -eq 0 ]; then
    osascript -e 'display notification "The command finished" with title "Success"'
  else
    osascript -e 'display notification "The command failed" with title "Failed"'
  fi
}

运行长时间命令时:

some_program; notifyResult

执行完后会收到通知是否执行成功!

可以设置通知音效:


四、一些常用脚本

1、通过参数发送系统通知

源代码:

代码如下:

sys-notify

#!/bin/bash

# 帮助函数
show_help() {
    echo "Usage: $0 [Option] [message]"
    echo "Show system notification"
    echo
    echo "Option:"
    echo "  -h, --help         Show help info"
    echo "  -t, --title TITLE  Set notification title (Default: Notify)"
    exit 0
}

# 默认值
title="Notify"

# 解析命令行参数
while [[ $# -gt 0 ]]; do
    case "$1" in
        -h|--help)
            show_help
            ;;
        -t|--title)
            if [[ -z "$2" ]]; then
                echo "错误: --title 需要一个参数" >&2
                exit 1
            fi
            title="$2"
            shift 2
            ;;
        *)
            # 第一个非选项参数视为消息内容
            message="$1"
            shift
            break
            ;;
    esac
done

# 如果没有提供消息内容,则从标准输入读取
if [[ -z "$message" ]]; then
    if [[ -t 0 ]]; then
        echo "错误: 没有提供消息内容" >&2
        show_help
        exit 1
    else
        message=$(cat)
    fi
fi

# 发送通知
case "$(uname -s)" in
    Darwin)
        # macOS 使用 AppleScript
        osascript -e "display notification \"$message\" with title \"$title\""
        ;;
    Linux)
        # Linux 使用 notify-send (libnotify)
        if command -v notify-send &>/dev/null; then
            notify-send "$title" "$message"
        else
            echo "错误: 未找到 notify-send 命令。请安装 libnotify 包。" >&2
            exit 1
        fi
        ;;
    *)
        echo "错误: 不支持的操作系统" >&2
        exit 1
        ;;
esac    

准备:

  • 编写 sys-notify
  • 增加可执行权限 chmod +x sys-notify
  • 加入到 PATH 环境变量;

使用方法:

  • 基本用法:sys-notify "操作已完成"
  • 自定义标题:sys-notify -t "操作结果" "操作已完成"
  • 从标准输入读取内容:echo "操作已完成" | sys-notify -t "操作结果"

2、命令完成后展示结果

源代码:

function notifyResult () {
  if [ $? -eq 0 ]; then
    osascript -e 'display notification "The command finished" with title "Success"'
  else
    osascript -e 'display notification "The command failed" with title "Failed"'
  fi
}

在运行某些需要比较长时间的程序时,执行以下命令:

some_program; notifyResult

五、后记

AppleScript是一个简单而强大的工具,能自动化macOS操作;

更多内容可参考附录链接;


附录

源代码:

官方文档:

参考文章: