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

Using AppleScript to export individual photo titles to text file from MacOS Photos App

我想扫描我的照片库(macOS 10.15.6,照片应用程序 5.0)并将所选照片的​​原始文件名导出到文本文件。我在下面有一个简单的脚本作为起点,它不能正确地将文件名转换为可读文本。我希望我需要对文件名执行某种 'convert to string' 操作,但我对答案一无所知...

如有任何建议,我们将不胜感激

我目前使用的代码:

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