applescript - 文件夹内容到 TextEdit 文档
问题描述
我在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 文档中。
解决方案
我承认我并不清楚 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