1

I'd like to create an automator action to help manage DNG/XMP files. I'd like to be able to drag a DNG (or several) onto the action which will send the DNG and the matching XMP file to the trash. The files have the same name except for the extension, they are in the same directory. For example, IMGP1361.DNG and IMGP1361.xmp

This would be a simple task for a shell script, but is there an Automator way to do it (to learn more about Automator)? Is there a way to get at the file name of the input finder item, change it in a variable and use that as input to another action?

Thanks.

Zak J
  • 156
  • 1
  • 8
  • It's easy to write an AppleScript (which can, but doesn't need to, be embedded in an Automator workflow) but I don't really see the point: why not just sort the Finder view by file name, select all the files you want to get rid of, and hit cmd+baskspace? – fanaugen Mar 12 '12 at 08:42
  • Good question -- I want to be able to scroll through a directory full of images uploaded via an EyeFi card and just drag out the ones I don't want to save and not have to worry about finding the matching XMP file to delete (as well as not having to scroll through the XMP files). – Zak J Mar 13 '12 at 03:58

2 Answers2

2

This script for Automator will delete all of the files that share the same prefix and whose name extension is listed in the grep command. You may add additional extensions as well. (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
adayzdone
  • 11,120
  • 2
  • 20
  • 37
1

OK, got it. You can use the AppleScript given below inside an Automator workflow like this:

Automator workflow

For every selected file in the Finder, if its extension is in ext_list, it will be moved to the Trash, and so will all other files of the same name in the same folder whose extension is one of those in also_these_extensions.

It can be useful, e.g. also for cleaning a folder with auxiliary LaTeX files: just put "tex" into ext_list and all the other extensions (such as "aux", "dvi", "log") into also_these_extensions.

The selected files don't need to be inside the same folder; you can also select several items in a Spotlight search results window.

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
fanaugen
  • 1,107
  • 2
  • 10
  • 22
  • I'm trying to use this to do something similar, except remove all nef/cr2 etc files while keeping the dng files. I took a look at script, is the only line I would need to change the "copy the_item to the end of delete_list"? – Eric Wolf Sep 14 '13 at 15:00