Automator / AppleScript在同一目录中查找具有不同扩展名的相同文件名

时间:2012-03-12 06:03:19

标签: macos applescript automator xmp dng

我想创建一个自动播放器操作来帮助管理DNG / XMP文件。我希望能够将DNG(或几个)拖到将将DNG和匹配的XMP文件发送到垃圾箱的操作上。除扩展名外,文件具有相同的名称,它们位于同一目录中。例如,IMGP1361.DNG和IMGP1361.xmp

对于shell脚本来说,这将是一个简单的任务,但是有一种Automator方法可以做到这一点(了解有关Automator的更多信息)?有没有办法获取输入查找项的文件名,在变量中更改它并将其用作另一个操作的输入?

感谢。

2 个答案:

答案 0 :(得分:2)

Automator的此脚本将删除共享相同前缀且其名称扩展名在grep命令中列出的所有文件。您也可以添加其他扩展程序。 (xmp | DNG | pdf | xls)

on run {input, parameters}
try
    repeat with anItem in input
        tell (info for anItem) to set {theName, theExt} to {name, name extension}
        set shortName to text 1 thru ((get offset of "." & theExt in theName) - 1) of theName
        tell application "Finder"
            set parentFolder to parent of anItem as alias
            set matchList to every paragraph of (do shell script "ls " & POSIX path of parentFolder & " | grep -E '" & shortName & ".(xmp|DNG)'")
            delete (every file of parentFolder whose name is in matchList)
        end tell
    end repeat
end try
end run

答案 1 :(得分:1)

好的,明白了。您可以在Automator工作流程中使用下面给出的AppleScript,如下所示:

Automator workflow

对于Finder中的每个选定的文件,如果其扩展名位于ext_list,则会将其移至“废纸篓”,同时所有其他同名文件也将移至“废纸篓”中扩展名为also_these_extensions

之一的文件夹

它可能很有用,例如还用于清理包含辅助LaTeX文件的文件夹:只需将"tex"放入ext_list,将所有其他扩展名(例如"aux", "dvi", "log")放入also_these_extensions

所选文件不需要位于同一文件夹中;您还可以在Spotlight搜索结果窗口中选择多个项目。

on run {input, parameters}
    -- for every item having one of these extensions:
    set ext_list to {"dng"}
    -- also process items with same name but these extensions:
    set other_ext_list to {"xmp"}

    tell application "Finder"
        set the_delete_list to {}
        set delete_list to a reference to the_delete_list
        -- populate list of items to delete
        repeat with the_item in input
            set the_item to (the_item as alias)
            if name extension of the_item is in ext_list then
                copy the_item to the end of delete_list
                set parent_folder to (container of the_item) as alias as text
                set item_name to text 1 thru ((length of (the_item's name as text)) - (length of (the_item's name extension as text))) of (the_item's name as text)
                repeat with ext in other_ext_list
                    try
                        copy ((parent_folder & item_name & ext) as alias) to the end of delete_list
                    end try
                end repeat
            end if
        end repeat
        -- delete the items, show info dialog
        move the_delete_list to the trash
        display dialog "Moved " & (length of the_delete_list) & " files to the Trash." buttons {"OK"}
    end tell
end run