如何获得文件夹的第一个子路径(当第一个子路径是文件夹时)?
假设你有…
Folder 1 ¬
File 1
File 2
Folder 2 ¬
File A
File B
File C
File D
用例:
我选择了一批文件夹,它们都与Folder 1
处于同一级别:set foldersToProcess to choose folder with multiple selections allowed
然后,我循环遍历每个排队的文件夹foldersToProcess
,在这个过程中,我想查看Folder 1
的第一个子文件夹(即Folder 2
),每一次。
我该怎么做?
你应该使用try block因为可能有些选定的文件夹根本不包含子文件夹。因此,如果没有try块,它会在请求目录1为空列表时抛出错误。
获取第一个文件夹作为System Event的文件夹引用:
set chosenFolders to (choose folder with multiple selections allowed)
set firstFolders to {}
repeat with anAlias in chosenFolders
try
tell application "System Events" to ¬
set end of firstFolders to folder 1 of folder (anAlias as text)
end try
end repeat
获取HFS路径在第一个文件夹中,用
编辑相应的代码行tell application "System Events" to ¬
set end of firstFolders to path of folder 1 of folder (anAlias as text)
获取Posix路径在第一个文件夹中,用
编辑相应的代码行tell application "System Events" to ¬
set end of firstFolders to POSIX path of folder 1 of folder (anAlias as text)