applescript:如果存在同名文件,则自动删除文件夹中的文件

时间:2016-04-19 17:14:07

标签: applescript

我的google云端硬盘中有一个文件夹,其中共享.docx个文件以继续使用Google文档进行编辑。 Google文档现在会创建扩展名为.gdoc的文档,以便我现在可以将每个文档两次,一次调用"test.docx""test.docx.gdoc"。现在,我想在存在具有相同名称的.docx文件时自动删除.docx.gdoc文件。

我试过这个,但它没有删除.docx文件:

on adding folder items to this_folder after receiving added_items
    tell application "Finder"
    set docxFiles to every file of this_folder whose name extension is "docx"
    repeat with aFile in docxFiles
        set baseName to text 1 thru -6 of (get name of aFile)
        set gdocFile to baseName & ".docx.gdoc"
        if exists gdocFile then delete aFile
    end repeat
end tell
end adding folder items to

在我的测试中,我相对确定一切都符合行

set gdocFile to baseName & ".docx.gdoc

没有做任何事情的行似乎是

if exists gdocFile then delete aFile

任何想法?

1 个答案:

答案 0 :(得分:0)

这不起作用,因为您在没有完整路径的情况下编写.docx.gdoc文件名

试试这个

on adding folder items to this_folder after receiving added_items
    tell application "Finder"
        set docxFiles to every file of this_folder whose name extension is "docx"
        repeat with aFile in docxFiles
            set gdocFile to (aFile as text) & ".gdoc"
            if exists file gdocFile then delete aFile
        end repeat
    end tell
end adding folder items to

我会反过来这样做。通过.docx.gdoc文件迭代并删除相应的docx文件。