【问题标题】:Applescript - Recursively searching through directoriesApplescript - 递归搜索目录
【发布时间】:2016-09-06 01:54:13
【问题描述】:

我有一个由另一个用户在这里慷慨创建的脚本,但我发现它不会递归地读取目录。该脚本的目标是读取在 Finder 中选择的目录的所有子目录,并写出 255 个字符或更长的文件的任何绝对路径(路径不是文件名)。这是为了查找绝对路径长度在 OSX 上对于具有 255 个字符路径限制的 Windows 机器来说太长的文件,然后再将它们从一个文件传输到另一个文件。

我尝试引用这篇文章以使其递归但无济于事,因为这里的方法看起来完全不同:AppleScript Processing Files in Folders recursively

on run
set longPaths to {}
tell application "Finder" to set theSel to selection
repeat with aFile in theSel
    set aFile to aFile as string
    set pathLength to count of characters in aFile
    if pathLength > 255 then
        set end of longPaths to aFile
    end if
end repeat

if longPaths is not equal to {} then
    -- do something with your list of long paths, write them to a text file or whatever you want
    set pathToYourTextFile to (path to desktop folder as string)&"SampleTextFile.txt"
    set tFile to open for access file (pathToYourTextFile as string) with write permission
    repeat with filePath in longPaths
        write (filePath & return as string) to tFile starting at eof
    end repeat
    close access tFile
end if
end run

有谁知道向该脚本添加递归元素的最佳方法,以便 theSel 包含所选目录的子目录中的所有文件?

【问题讨论】:

  • 你现在得到的结果是什么?请在您的问题中添加一个示例。
  • 目前,该脚本只会读取 Finder 中突出显示的文件或文件夹。如果文件或目录的绝对路径超过 255 个字符,它会将该路径写入桌面上名为“SmpleTextFile.txt”的文本文件。所以问题在于“将 theSel 设置为选择”只是在 Finder 中突出显示的那些项目,而我正在寻找一种方法让选择包括所选目录(包括子目录)中的所有文件。
  • 检查我在“列出绝对路径...”问题中的答案。

标签: macos recursion applescript


【解决方案1】:

试试:

tell application "Finder" to set theSel to every file in (get entire contents of selection)

或者类似的东西。抱歉,我没有 Mac 来测试精确的代码。每当您请求 entire contents 时,您都会获得所选文件夹的所有子文件夹中的所有内容。

【讨论】:

  • 不幸的是,这不起作用,我得到:“Finder 出错:无法获取选择的全部内容。”选择的全部内容中的数字 -1728
【解决方案2】:

这是一个通过目录递归读取的脚本:

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

【讨论】:

    猜你喜欢
    • 2013-06-17
    • 1970-01-01
    • 2019-06-24
    • 2011-08-15
    • 1970-01-01
    • 2010-12-01
    • 1970-01-01
    相关资源
    最近更新 更多