如何解决使用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
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。