Delete multiple files with applescript

断了今生、忘了曾经 提交于 2019-12-11 05:06:07

问题


Let’s say I have multiple files I want to delete, and they’re stored one per line in a files.txt file. I can delete them with

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

This is the logic you usually find online — deleting them one by one, but not only does this screw your “Put Back” option (you have to z multiple times), it also plays the trash sound a bunch of times in a row.

Is there a way to delete multiple files at once, say via storing them in an array and deleting that?

Edit

The answer by @adayzdone is the best so far, but it fails on directories


回答1:


Try:

    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



回答2:


Yes, Finder will take a list of paths as an argument to the delete command.

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

Alternatively, if you're able to store the file paths as HFS paths instead of POSIX paths, you can skip the coercion:

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

tell application "Finder"
    delete deleteThis
end tell



回答3:


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


来源:https://stackoverflow.com/questions/20002311/delete-multiple-files-with-applescript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!