文件夹内容到TextEdit文档

时间:2019-07-06 09:23:32

标签: applescript

我在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文档中。

2 个答案:

答案 0 :(得分:1)

我将承认,我不确定是否精确地说明了TextEdit术语中的“附件”是什么,或者它可能用于什么,但是,如果您只想要一个添加了所有这些附件的文件,那就是很简单:

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

答案 1 :(得分:0)

我知道您可以执行类似在命令行中查找的内容。

ls path_to_folder > path_to_export_file.txt

示例:

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

我非常确定您可以将其集成到AppleScript中,以执行您需要执行的其他任何操作。