如何从Shell调用Finder的「Import from iPhone > Take Photo」?能否用AppleScript实现?
首先得明确:Finder本身并没有暴露直接的AppleScript接口来触发这个「Take Photo」选项——因为这是**连续互通相机(Continuity Camera)**的系统级功能,而非Finder专属的可脚本化操作。你查Finder库文档找不到对应接口是正常的,不过我们可以通过两种变通方法来实现类似效果:
方法1:用AppleScript模拟用户UI交互(适合快速实现)
这种方法通过模拟右键点击、菜单选择的操作来触发功能,缺点是依赖系统语言和UI布局,系统更新或语言切换可能失效,但在固定环境下好用。
比如,如果你想在桌面的「测试文件夹」上触发这个操作,可以写这样的AppleScript:
tell application "Finder" set targetFolder to folder "测试文件夹" of desktop activate select targetFolder end tell -- 模拟右键点击选中的文件夹(通过菜单栏Services路径触发) tell application "System Events" tell process "Finder" click menu item "Import from iPhone" of menu 1 of menu item "Services" of menu 1 of menu bar item "Finder" of menu bar 1 -- 等待菜单加载,避免操作超时 delay 0.5 click menu item "Take Photo" of menu 1 of menu item "Import from iPhone" of menu 1 of menu item "Services" of menu 1 of menu bar item "Finder" of menu bar 1 end tell end tell
如果你的系统是英文环境,直接保留菜单原文字即可;中文环境则替换为对应的中文菜单名。
要从Shell调用这个脚本,直接用osascript命令执行:
osascript -e 'tell application "Finder" set targetFolder to folder "测试文件夹" of desktop activate select targetFolder end tell tell application "System Events" tell process "Finder" click menu item "Import from iPhone" of menu 1 of menu item "Services" of menu 1 of menu bar item "Finder" of menu bar 1 delay 0.5 click menu item "Take Photo" of menu 1 of menu item "Import from iPhone" of menu 1 of menu item "Services" of menu 1 of menu bar item "Finder" of menu bar 1 end tell end tell'
方法2:用快捷指令+Shell调用(更稳定,官方支持)
如果你不想依赖UI模拟,推荐用macOS Monterey及以上支持的shortcuts命令,通过官方快捷指令接口触发功能:
- 打开「快捷指令」应用,新建一个快捷指令:
- 添加「从iPhone/iPad拍照」动作
- 设置照片保存路径为你指定的Finder文件夹
- 给快捷指令命名(比如「iPhone拍照存桌面」)
- 从Shell直接调用这个快捷指令:
shortcuts run "iPhone拍照存桌面"
这种方法完全基于官方API,不受UI布局变化影响,稳定性更高。
关于defaults write com.apple.finder的说明
你提到的defaults write主要用来修改Finder的偏好设置(比如显示隐藏文件、更改默认视图),它只能调整配置,无法触发任何操作,所以没法用它启动「Take Photo」功能。
关于Screen Capture场景的补充
如果你的需求是用iPhone拍摄替代Mac本地屏幕捕获,快捷指令方法可以完美适配:在快捷指令里添加「从iPhone拍照」+「保存到指定文件夹」的动作,调用后就能直接获取iPhone拍摄的照片,无需手动导入,完全可以替代屏幕捕获+手动导入的流程。
内容的提问来源于stack exchange,提问作者Marco Montalto M.

