2013-01-15 47 views
0

客戶端有一個大型的WordPress上傳文件夾,每個文件有7或8個大小版本。尋找符合規則的文件名與Applescript

我正在尋找篩選出所有具有-NNNxNNN作爲文件名稱的一部分的圖像 - 「NNN」是任何數字。對於如:

  • 最初上傳的文件:7Metropolis711.jpg
  • 實例大小相同文件的版本:7Metropolis711-792x373.jpg

我使用的Automator這一點,並且我只是在尋找的AppleScript過濾出從文件夾輸入IE ..這些文件:

enter image description here

+0

你說你希望每個組在同一時間對工作流的單次運行排序尺寸。或在工作流程的單次運行中設置單個尺寸 – markhunte

+0

我只想要原件。所以假設任何帶有「-NNNxNNN」的圖像都是一個調整大小的圖像,可以跳過。 – Mark

回答

1

這裏是另一種方法:

on run {input} 
    set newList to {} 
    repeat with aFile in input 
     tell application "System Events" to set fileName to name of aFile 
     try 
      set variableName to do shell script "echo " & quoted form of fileName & " | grep -Eo [0-9]{3}x[0-9]{3}" 
     on error 
      set end of newList to (aFile's contents) 
     end try 
    end repeat 
    return newList 
end run 

enter image description here

+0

我們有一個贏家! :)非常感謝你adayzdone。 我想再次運行它,但這次在文件名中找到NNxNN,所以我假設我只是將您的grep內容從{3}更改爲{2}? – Mark

+0

另外,你知道什麼數據「複製Finder項目」正在尋找你的腳本運行後? HTTP://f.markb。me/mXaJ – Mark

+0

是的,您可以將{3}更改爲{2}以查找2個數字。您還可以將正則表達式更改爲[0-9] * x [0-9] *以捕獲任意數量的數字,其後跟隨x以及任意數量的數字。 – adayzdone

1

試試這個。你可以看到一個處理程序「isFormatNNNNNNN(fileName)」,它測試你的格式的文件名。顯然刪除代碼的前兩行。他們被使用,所以我可以在AppleScript編輯器中進行測試。它們應該等於Automator中的輸入變量。

編輯:根據您的意見,我修改劇本要佔一多「 - 」的文件名。現在我開始查看文件擴展名前面的文本,因爲我假設您的格式是文件名中的最後一個字符。

它在Automator中不起作用,因爲您必須在代碼周圍放置「on run {input,parameters}」。我現在已經完成了,只需將其複製/粘貼到automator即可。

on run {input, parameters} 
    set newList to {} 
    repeat with aFile in input 
     if not (my isFormatNNNxNNN(aFile)) then set end of newList to (contents of aFile) 
    end repeat 
    return newList 
end run 

on isFormatNNNxNNN(theFile) 
    set theBool to false 
    try 
     tell application "System Events" 
      set fileName to name of theFile 
      set fileExt to name extension of theFile 
     end tell 

     set endIndex to (count of fileExt) + 2 
     set nameText to text -(endIndex + 7) thru -endIndex of fileName 
     if nameText starts with "-" then 
      if character 5 of nameText is "x" then 
       -- test for numbers 
       text 2 thru 4 of nameText as number 
       text 6 thru 8 of nameText as number 
       set theBool to true 
      end if 
     end if 
    end try 
    return theBool 
end isFormatNNNxNNN 
+0

謝謝軒轅。它表示變量輸入未定義 - 假設因爲此腳本上方的操作不是將文件作爲「輸入」發送,而是作爲其他變量發送。 請看這裏:http://f.markb.me/ozN6-任何想法它應該改變? – Mark

+0

其實,通過留在前兩行,它似乎運行良好!忙於處理1.sgthing的圖像現在的圖像... 再次感謝! – Mark

+0

實際上沒有,沒有快樂......結果似乎發回了一切...看到這裏:http://f.markb.me/Tf7A 它應該返回黃色突出顯示的項目,並忽略其他一切... – Mark