使用applescript删除多个文件

时间:2013-11-15 13:35:19

标签: macos applescript finder

假设我有多个要删除的文件,并且它们在files.txt文件中每行存储一个。我可以用

删除它们
set deleteThis to paragraphs of (read "files.txt")

repeat with lineFromFile in deleteThis
    set fileName to POSIX file lineFromFile
    tell application "Finder" to delete file fileName
end repeat

这是你通常在网上找到的逻辑 - 逐个删除它们,但这不仅会阻止你的“放回”选项(你必须 z 多次),它也会连续多次播放垃圾声。

有没有办法一次删除多个文件,比如通过将它们存储在一个数组中并删除它?

修改

到目前为止,@ adayzdone的答案是最好的,但它在目录

上失败了

3 个答案:

答案 0 :(得分:2)

尝试:

    set deleteThis to paragraphs of (read "files.txt" as «class utf8»)
    set deleteList to {}

   tell application "Finder"
    repeat with aPath in deleteThis
        try
            set end of deleteList to (file aPath)
        on error
            try
                set end of deleteList to (folder aPath)
            end try
        end try
    end repeat

    delete deleteList
end tell

答案 1 :(得分:0)

是的,Finder会将路径列表作为delete命令的参数。

set deleteThis to paragraphs of (read "files.txt")

repeat with i from 1 to (count deleteThis)
    set item i of deleteThis to POSIX file (item i of deleteThis)
end repeat

tell application "Finder"
    delete deleteThis
end tell

或者,如果您能够将文件路径存储为HFS路径而不是POSIX路径,则可以跳过强制:

set deleteThis to paragraphs of (read "files.txt")

tell application "Finder"
    delete deleteThis
end tell

答案 2 :(得分:0)

tell application "Finder"
    set file_list to entire contents of (choose folder with prompt "Please select directory.")
    move file_list to trash
end tell