2012-03-12 92 views
1

我想創建一個automator操作來幫助管理DNG/XMP文件。我希望能夠將DNG(或多個)拖動到將把DNG和匹配的XMP文件發送到垃圾桶的操作上。除擴展名外,這些文件具有相同的名稱,它們位於相同的目錄中。例如,IMGP1361.DNG和IMGP1361.xmpAutomator/AppleScript在同一目錄中查找具有不同擴展名的相同文件名

這對於shell腳本來說是一個簡單的任務,但是有沒有Automator的方法來完成它(學習更多關於Automator的知識)?有沒有辦法獲得輸入查找程序項目的文件名,將其更改爲變量並將其用作另一個操作的輸入?

謝謝。

+0

可以很容易地編寫一個AppleScript(它可以但不需要,被嵌入的Automator工作流),但我真的不明白這一點:爲什麼不只是按文件名對Finder視圖進行排序,選擇所有你想擺脫的文件,然後點擊cmd + baskspace? – fanaugen 2012-03-12 08:42:15

+0

好問題 - 我希望能夠滾動瀏覽通過EyeFi卡上傳的完整圖像的目錄,只需拖出那些我不想保存的圖像,而不必擔心找到要刪除的匹配XMP文件(以及不必滾動瀏覽XMP文件)。 – 2012-03-13 03:58:08

回答

1

好的,明白了。您可以使用下面給出的的Automator工作流內像這樣的AppleScript:

Automator workflow

選擇文件中查找,如果它的擴展名是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 
+0

我試圖用這個來做類似的事情,除了刪除所有nef/cr2等文件的同時保留dng文件。我看了腳本,是我需要更改「將the_item複製到delete_list結尾」的唯一行嗎? – 2013-09-14 15:00:55

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 
+0

其名稱中包含短名稱是一個危險的過濾器,並且會導致太多文件被刪除。所以我不會建議使用上面的代碼 – 2012-03-12 17:26:30

+0

你是否更喜歡這個dj? – adayzdone 2012-03-12 20:38:31

+0

我無法做得更好:D(投票) – 2012-03-12 22:37:58

相關問題