AppleScript - 获取文件夹中唯一文件的名称?
问题描述:
下面的AppleScript代码获得最后添加的文件,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?
但是,如果该文件夹中只有一个文件失败。如何解决这个问题?
答
该脚本应该与一个文件一起使用。但是如果有没有文件,则会报错。
您可以用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
这个伟大的工程忽略错误。谢谢。我们如何指定一个像这样的路径“/ MainFolder1/SubFolder1”? –
严格说话'/ MainFolder1'位于启动磁盘的顶层。这是打算? Finder希望(以冒号分隔的)字符串路径以磁盘名称(也是启动磁盘)开始。您可以编写“获取文件夹的文件”Macintosh HD:MainFolder1:SubFolder1:“...' – vadian
再次感谢。你非常有帮助。 –