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

applescript: automatically delete files in folder if file with same name is present

我的 google 驱动器中有一个文件夹,其中共享 .docx 文件以继续使用 Google 文档编辑它们。 Google docs 现在创建扩展名为 .gdoc 的文档,这样我现在每个文档都有两次,一次调用 "test.docx""test.docx.gdoc"。现在我想在 .docx.gdoc 同名文件存在时自动删除 .docx 文件。

我试过这个,但它没有删除 .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

有什么想法吗?

这行不通,因为您正在编写没有完整路径的 .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 个文件。