文件夹内容到 TextEdit 文档

Folder contents to TextEdit document

我在 https://macscripter.net/viewtopic.php?id=43410 中找到了这个脚本:

tell application "Finder"
    set thisFolder to choose folder with prompt "Please select the folder you wish to process." without invisibles
    set theseFiles to files of thisFolder as alias list
end tell

tell application "TextEdit"
    activate
    repeat with thisFile in theseFiles
        set newFilename to my getFilename(thisFile)
        set thisDoc to make new document
        tell thisDoc
            make new attachment with properties {file name:thisFile}
            set pathtofile to ((thisFolder as string) & newFilename & ".rtfd") as Unicode text
            save in file pathtofile
        end tell
        close front document saving no
    end repeat
end tell

on getFilename(thisFile)
    set {atid, AppleScript's text item delimiters} to {AppleScript's text item delimiters, ":"}
    set theFilename to last text item of (thisFile as text)
    set AppleScript's text item delimiters to "."
    set newFilename to first text item of theFilename
    set AppleScript's text item delimiters to atid
    return newFilename
end getFilename

此脚本对源文件夹的每个文件执行一个 TextEdit 文件。

我如何修改这个脚本来为源文件夹的所有文件创建一个 TextEdit 文件?结果文件将包含所有文件夹文件。

我不需要将文件名列表放入TextEdit文件中。我需要将所有文件放入带有附件(RTFD 格式)的 TextEdit 文档中。

我知道您可以在命令行中执行您可能需要的操作。

ls path_to_folder > path_to_export_file.txt

示例:

ls ~/Desktop/ > ~/Desktop/export2.txt

我很确定您能够将其集成到 AppleScript 中以执行您需要的任何其他操作。

我承认我不太清楚 TextEdit 行话中的 'attachment' 是什么,或者它可能用于什么,但是如果您只想将所有这些附件添加到一个文件就这样,这很简单:

tell application "Finder"
    set thisFolder to choose folder with prompt "Please select the folder you wish to process." without invisibles
    set theFileName to name of thisFolder
    set theseFiles to files of thisFolder as alias list
end tell

tell application "TextEdit"
    activate
    set thisDoc to make new document
    set pathtofile to ((thisFolder as string) & theFileName & ".rtfd") as Unicode text
    tell thisDoc
        repeat with thisFile in theseFiles
            make new attachment with properties {file name:thisFile}
        end repeat
        save in file pathtofile
    end tell
    close front document saving no
end tell

编辑

根据评论,这是将此例程应用于子文件夹的版本。我已经稍微清理了代码(我更喜欢使用系统事件而不是 Finder),但它是相同的过程。

set thisFolder to choose folder with prompt "Please select the folder you wish to process." without invisibles

tell application "System Events"
    set subFolders to folders of thisFolder
    repeat with aFolder in subFolders
        set folderName to name of aFolder
        set filePath to (POSIX path of thisFolder) & "/" & folderName & ".rtfd"
        set fileList to (POSIX path of every file of aFolder whose visible is true)
        tell application "TextEdit"
            activate
            set thisDoc to make new document
            tell thisDoc
                repeat with aFile in fileList
                    make new attachment with properties {file name:(POSIX file aFile) as alias}
                end repeat
                save in POSIX file filePath
            end tell
            close front document saving no
        end tell
    end repeat
end tell
return