Applescript - 递归遍历目录

问题描述 投票:0回答:1

我找到了类似的解决方案:

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

它们都以别名格式处理文件引用。据我所知,我无法在这种情况下更改原始文件名,或删除它等等,这就是我想要做的。

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

applescript automator finder
1个回答
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
© www.soinside.com 2019 - 2024. All rights reserved.