applescript 可通过 finder 获取所有可弹出且非本地的磁盘,过滤系统卷后提取名称、路径、容量、剩余空间和格式等信息,并以 gb 为单位显示;脚本兼容 macos monterey 至 sonoma,纯原生实现,无需第三方工具。

AppleScript 可以直接调用 macOS 系统命令和 Finder 对象来检测外部存储设备(如 USB 闪存盘、外置硬盘、SD 卡等)的连接状态、挂载路径、可用空间等信息。以下是一个实用、稳定、可直接运行的 AppleScript 脚本,用于自动化获取当前所有外部存储设备的基本状态。
检测已挂载的外部存储设备
macOS 中外部存储通常挂载在 /Volumes/ 下,且不包含系统内置卷(如 Macintosh HD、Data、Preboot 等)。AppleScript 可通过 Finder 获取所有磁盘,并过滤出“可弹出”(ejectable)且未被标记为内部的卷。
- 使用 Finder 的 disks 列表遍历所有磁盘对象
- 检查 local volume 属性为 false(表示非本地内置磁盘)
- 检查 ejectable 属性为 true(确保是物理可移除设备)
- 排除名称含 “Boot”、“Recovery”、“Preboot”、“VM”、“MobileBackups” 的系统卷
获取关键状态信息(名称、路径、容量、可用空间)
对每个匹配的外部磁盘,脚本可读取其:
- name:显示名称(如 “My Passport”)
- POSIX path:挂载路径(如 "/Volumes/My Passport")
- capacity:总容量(单位:字节)
- free space:剩余空间(字节),可转换为 GB 显示
- format:文件系统格式(如 APFS、exFAT、Mac OS Extended)
注意:free space 在某些只读或权限受限卷上可能返回错误,建议加 try…on error 包裹。
Docker Desktop for Mac (Apple Silicon) 是 Docker 官方专为搭载 Apple M 系列芯片(M1, M2, M3, M4 及其 Pro/Max/Ultra 变体)的 Mac 电脑深度优化的容器化开发平台。它利用 Apple Silicon 强大的能效比和原生 ARM64 架构,为 macOS 开发者提供了流畅、高效且原生的容器体验,彻底取代了旧版 Intel Mac 上的虚拟化方案。
完整可运行脚本示例
将以下内容保存为 .scpt 文件(如 CheckExternalDisks.scpt),双击运行或终端中用 osascript CheckExternalDisks.scpt 执行:
use framework "Foundation"
use sys: "sys:System Events"
use scriptingAdditions
<p>set externalDisks to {}
tell application "Finder"
repeat with d in disks
try
if (ejectable of d) and (not local volume of d) then
set diskName to name of d
-- 排除常见系统卷名
if diskName does not contain "Boot" and ¬
diskName does not contain "Recovery" and ¬
diskName does not contain "Preboot" and ¬
diskName does not contain "VM" and ¬
diskName does not contain "MobileBackups" and ¬
diskName ≠ "OS X Base System" then</p><pre class="brush:php;toolbar:false;"> set diskPath to POSIX path of (desktop as alias) & diskName
set totalCap to capacity of d
set freeSpace to free space of d
set fsFormat to format of d
-- 转换为 GB(保留一位小数)
set capGB to (totalCap / 1024 / 1024 / 1024) as text
set freeGB to (freeSpace / 1024 / 1024 / 1024) as text
set end of externalDisks to {¬
name:diskName, ¬
path:diskPath, ¬
capacity:capGB, ¬
free:freeGB, ¬
format:fsFormat}
end if
end if
on error
-- 忽略无法访问的卷(如未完全挂载或权限不足)
end try
end repeatend tell
if (count of externalDisks) = 0 then display alert "未检测到外部存储设备" message "请插入 USB 设备或外置硬盘后重试。" else set resultText to "检测到 " & (count of externalDisks) & " 个外部设备:\n\n" repeat with i from 1 to count of externalDisks set d to item i of externalDisks set resultText to resultText & "● " & (name of d) & return & ¬ " 路径:" & (path of d) & return & ¬ " 容量:" & (capacity of d) & " GB" & return & ¬ " 剩余:" & (free of d) & " GB" & return & ¬ " 格式:" & (format of d) & return & return end repeat display alert "外部存储状态" message resultText end if
进阶建议:集成到快捷指令或定时任务
若需长期监控或自动响应,可:
- 用 Automator 封装为“快速操作”,右键菜单一键运行
- 导出为 App 并添加到登录项,开机自检
- 配合 launchd 设置每 5 分钟检查一次,有新设备插入时触发通知(需额外 shell + osascript 配合)
- 将结果写入日志文件(如 ~/Desktop/external_disks.log),便于排查插拔异常
该脚本不依赖第三方工具,纯 AppleScript + macOS 原生 API,兼容 macOS Monterey 至 Sonoma。对 APFS 加密卷、Time Machine 备份盘(非备份时)也有效,但加密卷若未解锁则不会出现在 Finder disks 列表中——这是系统级限制,无需额外处理。










