安卓手机同步Apple Photos的配置

记录下安卓手机同步照片到苹果的 Photos 上的配置,方便以后参考
效果:安卓手机能自动上传照片到 Apple Photos 上,且可查看 Apple Photos 上的照片
前提条件:一台永远在线的 mac 电脑做为中转站
需要的软件: FolderSyncImmich
缺点:
  1. 安卓端需要将 FolderSync 设置为常驻后台
  2. 为了避免重复照片,在上传 Photos 后会自动删除安卓端上的照片
  3. 安卓端需要使用 Immich 软件才能查看 Apple Photos 上的所有照片
实施步骤:
  1. 使用 FolderSync 软件同步安卓相册到 mac 上,在安卓上将 FolderSync 设置为常驻后台,以便实时上传媒体文件到 mac 上
  2. 创建 AppleScript 自动检测文件夹更新,并上传到 Photos 后,删除文件夹内的照片。路径:/Users/versun/Documents/Scripts/import_android_photos_and_cleanup.scpt
-- ========= 配置区 =========
-- 监控目录:确保最后带 /
property watchFolderPOSIX : "/Volumes/8T/AndroidPhotos/"
-- 允许导入的扩展名(小写)
property allowedExts : {"jpg", "jpeg", "png", "heic", "heif", "tif", "tiff", "gif", "bmp", "raw", "dng", "mp4", "mov", "m4v"}
-- 每次发生“导入会话冲突”时的重试等待秒数,以及最大重试次数
property retryWaitSeconds : 5
property maxTries : 12
-- ========= 主入口 =========
on run
-- 确保监控目录存在
my ensureFolderExists(watchFolderPOSIX)
-- 扫描文件
set fileListPOSIX to my listRegularFiles(watchFolderPOSIX)
if fileListPOSIX is {} then return
-- 逐个导入(串行,避免 Photos 并发导入冲突)
repeat with p in fileListPOSIX
my importOneAndTrash(p as text)
end repeat
end run
-- ========= 工具函数 =========
on ensureFolderExists(folderPOSIX)
-- mkdir -p:目录不存在就创建
do shell script "/bin/mkdir -p " & quoted form of folderPOSIX
end ensureFolderExists
on listRegularFiles(folderPOSIX)
-- 只列出该目录“第一层”的普通文件(不递归)
-- 用 find 更稳:能处理文件名空格/特殊字符;以 \0 分隔再转行
set cmd to "/usr/bin/find " & quoted form of folderPOSIX & " -maxdepth 1 -type f -print0 | " & ¬
"/usr/bin/python3 -c " & quoted form of "import sys; data=sys.stdin.buffer.read().split(b'\\0'); print('\\n'.join([d.decode('utf-8','surrogateescape') for d in data if d]))"
set out to (do shell script cmd)
if out is "" then return {}
return paragraphs of out
end listRegularFiles
on getLowercaseExtension(filePOSIX)
-- 取最后一个点号后的扩展名并转小写(无扩展名则返回空字符串)
-- shell 参数展开:${p##*.}
set cmd to "p=" & quoted form of filePOSIX & "; " & ¬
"b=$(basename \"$p\"); " & ¬
"case \"$b\" in " & ¬
"  *.*) e=${b##*.}; echo \"$e\" | tr '[:upper:]' '[:lower:]' ;; " & ¬
"  *) echo \"\" ;; " & ¬
"esac"
return (do shell script cmd)
end getLowercaseExtension
on importOneAndTrash(filePOSIX)
-- 过滤扩展名
set ext to my getLowercaseExtension(filePOSIX)
if ext is "" then return false
if allowedExts does not contain ext then return false
-- 生成 alias(Photos 的 import 通常更喜欢文件引用)
set theAlias to POSIX file filePOSIX as alias
repeat with i from 1 to maxTries
try
tell application id "com.apple.photos"
-- activate 有时有助于自动化授权/脚本稳定性(不保证必须)
activate
-- 关键:用返回值判断是否导入成功(返回 media items 列表)
set importedItems to import {theAlias} with skip check duplicates
end tell
-- 返回非空 => 认为导入成功,再删除源文件(移入废纸篓)
if importedItems is not {} then
tell application "Finder"
move theAlias to trash
end tell
return true
else
-- 返回空:不删除,让下次循环再试
return false
end if
on error errMsg number errNum
-- Photos 正在导入其它批次时常见的冲突提示:等待后重试
if errMsg contains "Another import session is in progress" then
delay retryWaitSeconds
else
-- 其它错误:不删源文件,留待下次或人工排查
return false
end if
end try
end repeat
return false
end importOneAndTrash
  1. 在mac端添加自启动文件程序,路径:/Users/versun/Library/LaunchAgents/com.versun.photosimport.plist
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
  <key>Label</key>
  <string>com.versun.photosimport</string>

  <key>ProgramArguments</key>
  <array>
    <string>/usr/bin/osascript</string>
    <string>/Users/versun/Documents/Scripts/import_android_photos_and_cleanup.scpt</string>
  </array>

  <key>RunAtLoad</key>
  <true/>

  <key>StartInterval</key>
  <integer>60</integer>

  <key>StandardOutPath</key>
  <string>/tmp/photosimport.out</string>
  <key>StandardErrorPath</key>
  <string>/tmp/photosimport.err</string>
</dict>
</plist>
终端加载配置:`launchctl bootstrap gui/$(id -u) ~/Library/LaunchAgents/com.versun.photosimport.plist`
然后在 mac mini 上安装配置 Immich 本地相册,并添加 Photos 做为外部相册,就可在安卓手机上查看 Photos 里的所有照片了