AppleScript - 获取文件夹中唯一文件的名称?
AppleScript - Get the name of the only file in a folder?
以下 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?
但如果文件夹只有一个文件,则此操作失败。如何解决这个问题?
该脚本应该与一个 文件一起使用。但是如果有 no 文件,它会抛出一个错误。
您可以使用 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
以下 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?
但如果文件夹只有一个文件,则此操作失败。如何解决这个问题?
该脚本应该与一个 文件一起使用。但是如果有 no 文件,它会抛出一个错误。
您可以使用 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