以下AppleScript代码获取最后添加的文件
tell application "Finder"
set latestFile to item 1 of (sort (get files of (path to downloads folder)) by creation date) as alias
set fileName to latestFile's name
end tell
来源: How to get the latest downloaded file name with applescript programatically?
但如果文件夹只有一个文件,则会失败。如何解决这个问题?
答案 0 :(得分:2)
该脚本应该与一个文件一起使用。但如果有没有文件,则会引发错误。
您可以使用try
块
tell application "Finder"
try
set latestFile to item 1 of (sort (get document files of (path to downloads folder)) by creation date) as alias
set fileName to latestFile's name
end try
end tell