使用Apple Script删除父文件夹中的文件

时间:2018-03-15 17:02:45

标签: applescript

我想用" .acr"删除(移至垃圾箱)所有文件父文件和当前文件夹中的文件扩展名。我试过这样的话:

tell application "Finder"
    try
        delete (every file of folder of (file (path to me)) whose name ends with ".acr")
        delete (every file of container of (file (path to me)) whose name ends with ".acr")
    end try
end tell

仅删除当前文件夹中的文件。不删除父文件夹中的文件。

1 个答案:

答案 0 :(得分:1)

如果,通过当前文件夹,表示当前文件夹在 Finder 中打开,那么这将检索该文件夹及其parent文件夹中的所有.acr个文件,然后将它们移动到 Trash

    tell application "Finder" to ¬
        tell front Finder window to if exists then
            set f to (every file of its target ¬
                whose name extension is "acr") as alias list
            set g to (every file of its target's container ¬
                whose name extension is "acr") as alias list

            delete f & g
        end if

如果您实际上意味着要像在代码段中那样实现path to me,那么问题在于folder of...container of...基本上意味着相同的事情,因此两者都是代码行正在查找同一目录中的文件:即脚本所在的目录,如果你是从脚本编辑器中运行它。

以下是修复:

    tell application "Finder"
        set f to every file ¬
            of folder ¬
            of (path to me) ¬
            whose name extension is "acr"

        set g to every file ¬
            of container ¬
            of folder ¬
            of (path to me) ¬
            whose name extension is "acr"

        delete f & g
    end tell