Applescript-递归地浏览目录

时间:2019-01-31 07:32:31

标签: applescript automator finder

我发现了类似的解决方案:

property theOpenFile : missing value

tell application "Finder" to set theSel to selection
if theSel is not {} then
    set pathToYourTextFile to (path to desktop folder as string) & "SampleTextFile2.txt"
    set theOpenFile to open for access file (pathToYourTextFile as string) with write permission
    repeat with aItem in theSel
        tell application "Finder" to class of aItem is folder
        if the result then my getFilesIn(aItem) -- aItem is a folder
    end repeat
    close access theOpenFile
end if

on getFilesIn(thisFolder)
    tell application "Finder" to set theseFiles to files of thisFolder as alias list
    repeat with thisFile in theseFiles
        set f to thisFile as string
        set pathLength to length of f
        if pathLength > 255 then my writeToFile(f)
    end repeat
    tell application "Finder" to set theseSubFolders to folders of thisFolder
    repeat with tSubF in theseSubFolders
        my getFilesIn(tSubF) -- call this handler (recursively through this folder)
    end repeat
end getFilesIn

on writeToFile(t)
    write (t & return) to theOpenFile starting at eof
end writeToFile

它们都使用别名格式的文件引用进行工作。据我了解,在这种情况下,我无法更改原始文件名,也无法将其删除等,这就是我想要做的。

我是对的吗?如果可以,我还能做什么?

1 个答案:

答案 0 :(得分:0)

您可以重命名和删除文件,Finder可以处理alias指定符

repeat with thisFile in (get theseFiles) -- you need get to capture the reference list
    set f to thisFile as string
    set pathLength to length of f
    if pathLength > 255 then my writeToFile(f)
    tell application "Finder" to set name of thisFile to "foo.txt"
    -- or 
    tell application "Finder" to delete thisFile
end repeat

或者不是alias,而是可以使用Finder file指定符

repeat with thisFile in (get theseFiles)
    set f to thisFile as string
    set pathLength to length of f
    if pathLength > 255 then my writeToFile(f)
    tell application "Finder" to set name of file f to "foo.txt"
    -- or 
    tell application "Finder" to delete file f
end repeat