Carbajo
Carbajo

Reputation: 105

Folder contents to TextEdit document

I found this script in 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

This script do one TextEdit file per one file of source folder.

How can I modify this script to creates one TextEdit file of all files of source folder? The result file will contain all folder files.

I do not need a list of file names into TextEdit document. I need all FILES into TextEdit document with attachments (RTFD format).

Upvotes: 0

Views: 215

Answers (2)

Ted Wrigley
Ted Wrigley

Reputation: 3194

I'll confess that I'm not precisely clear what an 'attachment' is in TextEdit lingo, or what it might be used for, but if you just want one file with all of these attachments added to it it, that's easy enough:

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

EDIT

Per the comments, here's a version that applies this routine to subfolders. I've cleaned up the code a bit (I like using System Events better than the Finder), but it's the same process.

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

Upvotes: 1

Ralph C
Ralph C

Reputation: 47

I know you can do something like what you may be looking for in command line.

ls path_to_folder > path_to_export_file.txt

Example:

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

I am pretty sure you would be able to integrate that into AppleScript to do whatever else you need.

Upvotes: 0

Related Questions