使用AppleScript从MacOS Photos App将单个照片标题导出到文本文件

问题描述

我想通过我的照片库(macOS 10.15.6,Photos App 5.0)进行扫描,并将所选照片的​​原始文件名导出为文本文件。我在下面有一个简单的脚本作为起点,无法正确地将文件名转换为可读文本。我希望我需要对文件名执行某种“转换为字符串”操作,但是我对答案一无所知...

任何建议将不胜感激

我当前正在使用的代码:

set myFile to open for access "/Users/ed/Desktop/testFile.txt" with write permission
write "file start\n" to myFile

tell application "Photos"
    activate
    set imageSel to (get selection)
    if imageSel is {} then
        error "Please select an image."
    else
        repeat with im in imageSel
            write filename of im to myFile
            write "\nnext photo\n" to myFile
        end repeat
        
    end if
end tell

write "file end\n" to myFile
close access myFile

解决方法

我的建议是建立文件名列表,然后使用text item delimiters将列表转换为段落并将文本写入磁盘。

强烈建议在write部分中添加可靠的错误处理。

tell application "Photos"
    set imageSel to selection
    if imageSel is {} then
        error "Please select an image."
    else
        set theNames to {"file start"}
        repeat with im in imageSel
            set end of theNames to filename of im
        end repeat
        set end of theNames to "file end"
    end if
end tell
set {TID,text item delimiters} to {text item delimiters,linefeed}
set theNames to theNames as text
set text item delimiters to TID

set testFile to POSIX path of (path to desktop) & "testFile.txt"
try
    set fileDescriptor to open for access testFile with write permission
    write theNames to fileDescriptor
    close access fileDescriptor
on error
    try
       close testFile
    end try
end try

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...