Applescript 要编辑文档的文件夹内容

Applescript 要编辑文档的文件夹内容,applescript,Applescript,我在以下文件中找到了此脚本: 此脚本对源文件夹的每个文件执行一个文本编辑文件 如何修改此脚本以创建源文件夹中所有文件的一个TextEdit文件?结果文件将包含所有文件夹文件 我不需要在文本编辑文档中列出文件名。我需要将所有文件转换为文本编辑带附件的文档(RTFD格式)。我知道您可以在命令行中执行类似的操作 ls path_to_folder > path_to_export_file.txt 例如: ls ~/Desktop/ > ~/Desktop/export2.txt 我非

我在以下文件中找到了此脚本:

此脚本对源文件夹的每个文件执行一个文本编辑文件

如何修改此脚本以创建源文件夹中所有文件的一个TextEdit文件?结果文件将包含所有文件夹文件


我不需要在文本编辑文档中列出文件名。我需要将所有文件转换为文本编辑带附件的文档(RTFD格式)。

我知道您可以在命令行中执行类似的操作

ls path_to_folder > path_to_export_file.txt
例如:

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

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

我承认,我不清楚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
编辑

根据注释,这里有一个将此例程应用于子文件夹的版本。我对代码做了一些清理(我喜欢使用系统事件而不是查找器),但过程是一样的

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

谢谢你,特德!它起作用了!如何使文本编辑文件采用文件夹的名称?我修改了上面的文本以进行这些更改(添加了第3行,编辑了第10行)。基本上,它只是在选定文件夹后立即获取文件夹的名称,然后在我们创建文件路径时将其插入。再次感谢你,Ted!最后一个问题是:可以用许多文件夹来完成吗?示例:一个文件夹“A”包含多个文件夹“B”。每个“B”文件夹都包含文件,但不包含子文件夹。该脚本将创建与文件夹“B”一样多的文本编辑文件。太棒了!非常感谢您的才华,Ted。在文件系统操作方面,通过Finder支持系统事件做得很好+1我觉得对于可靠、一致和干净的输出来说,ls是一个糟糕的选择,它打算继续用于其他目的。我建议使用通配符扩展来枚举目录:
printf“%s\n”~/Desktop/*>~/Desktop/export2.txt
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