【问题标题】:Applescript - Recursively step through directoriesApplescript - 递归遍历目录
【发布时间】:2019-06-24 14:38:31
【问题描述】:

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

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】:

    您可以重命名和删除文件,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
    

    或者您可以使用 Finder file 说明符,而不是 alias

    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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-01-23
      • 1970-01-01
      • 2016-09-06
      • 2013-06-01
      • 2012-04-07
      • 2018-11-15
      • 2013-04-18
      • 2015-12-18
      相关资源
      最近更新 更多