applescript通过控制finder等原生应用实现文件分享,如airdrop、邮件附件发送或右键快速操作;需辅助功能权限,支持自动压缩、填写收件人并发送,依赖系统本地化名称与正确配置。

AppleScript 本身不直接“分享文件”(如通过 AirDrop、邮件或 iCloud 链接),但它能精准控制 macOS 原生应用完成分享动作——本质是触发 Finder、邮件、Messages 或共享菜单中的标准操作流程,实现一键式、可复用的快速分享逻辑。
直接调用 Finder 共享菜单动作
适用于将选中文件快速通过 AirDrop、邮件、iMessage 或“添加到iCloud云盘”等系统级方式分发。
AppleScript 无法模拟鼠标点击,但可通过
menu bar item和menu item操作触发 Finder 的“共享”子菜单关键前提是:目标文件已在 Finder 中被选中(或脚本先激活并选中)
Manage Apple Notes下载使用 inotes CLI 从终端管理 Apple Notes。当需要在 macOS 的 Notes.app 中列出、读取、创建、编辑、删除或搜索备忘录时使用。
-
示例脚本(发送选中文件 via AirDrop):
tell application "Finder" activate set selectedItems to selection if (count of selectedItems) = 0 then error "请先在访达中选中至少一个文件" -- 激活共享菜单(需系统语言为简体中文;英文环境请改用 "Share") tell application "System Events" tell process "Finder" click menu bar item "文件" of menu bar 1 click menu item "共享" of menu 1 of menu bar item "文件" of menu bar 1 click menu item "用 AirDrop 发送" of menu 1 of menu item "共享" of menu 1 of menu bar item "文件" of menu bar 1 end tell end tell end tell⚠️ 注意:此方式依赖界面本地化名称,且需开启辅助功能权限(“系统设置 → 隐私与安全性 → 辅助功能”中勾选 Finder 和 System Events)
自动打包 + 邮件附件发送
适合固定收件人、固定主题的文件交付场景(如日报、设计稿、安装包)。
- 脚本可自动压缩选中文件、新建邮件、附加、填写收件人并发送
- 不依赖用户手动操作,全程后台执行(需邮件 App 已配置账户)
- 示例逻辑:
-- 获取访达中选中的文件 set theFiles to {} tell application "Finder" set theFiles to selection as list end tell
if (count of theFiles) = 0 then error "未选中任何文件"
-- 创建 ZIP 压缩包(临时路径) set tempZip to ((path to desktop as text) & "share" & (do shell script "date +%Y%m%d%H%M%S") & ".zip") set posixZip to POSIX path of tempZip
-- 压缩(使用系统原生命令) repeat with f in theFiles do shell script "zip -jr " & quoted form of posixZip & " " & quoted form of (POSIX path of f) end repeat
-- 发送邮件 tell application "Mail" set newMessage to make new outgoing message with properties {subject:"文件分享", content:"请查收附件。"} tell newMessage make new to recipient at end of to recipients with properties {address:"recipient@example.com"} make new attachment with properties {file name:tempZip} at after last paragraph end tell send newMessage end tell
--- ### 结合 Automator 实现“右键即分享” 真正落地日常使用,推荐嵌入 Automator 快速操作: - 新建 Automator “快速操作”,输入类型设为“文件或文件夹” - 拖入“运行 AppleScript”,粘贴上述任一逻辑(如邮件发送版) - 存储后勾选“在访达中”,即可在任意文件上右键 → “快速操作” → 点击名称执行 - 优势:无需记住脚本位置,不依赖终端,权限一次授权长期有效 --- ### 补充:避免常见失败点 - **权限缺失**:首次运行会弹窗要求授权,必须手动允许“辅助功能”和“全盘访问”(后者用于读写文件) - **邮件未就绪**:确保 Mail App 已登录有效账户,且“稍后发送”已关闭(否则脚本发不出) - **路径含空格/中文**:AppleScript 中路径务必用 `POSIX path of xxx` 转换,并配合 `quoted form of` 传给 shell - **多文件处理**:`selection as list` 是安全获取方式;避免用 `item 1 of selection`,防止单选时出错 不复杂但容易忽略。










