2015-06-22 33 views
1

我從來沒有使用過AppleScript,所以我對語言很不熟悉,但我盡我所能。多個條件嵌套重複,而在蘋果腳本

以下是我正在嘗試完成的操作: 在選擇充滿.ARW和.JPG文件的文件夾時運行腳本。遍歷文件夾中的項目。如果當前項目是.ARW,則重新從頭開始遍歷文件夾。如果此嵌套迭代着陸在具有相同文件名和JPG擴展名的文件上,請使用紅色標記原始ARW文件。

TLDR:如果在一個文件夾的共享文件ARW相同的文件名的文件夾中的JPG文件,以紅色突出顯示的ARW文件,否則什麼也不做。

這裏是到目前爲止,我寫的代碼:

tell application "Finder" 
    set totalAlias to the entire contents of (selection as alias) 
    set totalCount to the count of items of totalAlias 
    set firstName to name of item 1 of totalAlias 
    set firstExtension to name extension of item 1 of totalAlias 
    set c to 1 

repeat while c ≤ totalCount 
    set currentAlias to item c of totalAlias 
    set currentName to name of currentAlias 
    set currentExtension to name extension of currentAlias 
    if currentExtension is "ARW" then 
     set d to 1 
     set compareFile to currentAlias 
     set findName to currentName 
     set findExtension to currentExtension 

     repeat while d ≤ totalCount 
      if (name of item d of totalAlias = findName) and (name extension of item d of totalAlias is "JPG") then 
       tell application "Finder" to set label index of compareFile to 2 

      end if 
      set d to (d + 1) 
     end repeat 
    end if 
    set c to (c + 1) 
end repeat 
end tell 

在什麼地方出了錯有什麼想法?我相信這與我的IF和條件有關。

回答

0

試試這個腳本:

tell application "Finder" 
    --Just get all the filenames of the target types: 
    set allJPG to the name of every file of (entire contents of (selection as alias)) whose name extension = "JPG" 
    set allARW to the name of every file of (entire contents of (selection as alias)) whose name extension = "ARW" 
    --Send the two lists to a handler to find all the common names 
    set targetJPGFiles to my CompareNames(allJPG, allARW) 
    --Loop through the common names, find the files, set the tags 
    repeat with eachTarget in targetJPGFiles 
     set fileToTag to (item 1 of (get every file of (entire contents of (selection as alias)) whose name is (eachTarget as text))) 
     set label index of fileToTag to 2 
    end repeat 
end tell 

targetJPGFiles -- This allows you to see the filenames that SHOULD have been tagged 

to CompareNames(jp, pn) 
    --First, get rid of all the extensions in the ARW files 
    set cleanARWNames to {} 
    set neededJPGNames to {} 
    repeat with eachARWName in pn 
     set end of cleanARWNames to characters 1 thru -5 of (eachARWName as text) as text 
    end repeat 
    --Now, loop through JPG names to find a match 
    repeat with eachjpgName in jp 
     set searchName to characters 1 thru -5 of (eachjpgName as text) as text 
     if cleanARWNames contains searchName then 
      set end of neededJPGNames to (eachjpgName as text) 
     end if 
    end repeat 
    return neededJPGNames 
end CompareNames 

它需要一個稍微不同的方式,因爲它只是比較文件名的兩個列表而已,然後返回,找到你想要的名稱的文件,並執行標記。

它基於我爲另一個項目編寫的腳本,所以我希望它適用於您。

我之前從未在Finder中使用過label index屬性,並且通過一些測試發現我無法看到標籤,直到在腳本運行後單擊該文件夾。儘管如此,所有的目標文件都有正確的標籤。

+0

嘿克雷格,非常感謝!我修改了腳本以突出顯示ARW而不是JPG,並且完美地工作。我想我唯一剩下的問題是爲什麼腳本執行需要這麼長時間(對於40個ARW,3Ghz i7 MBPro需要約3分鐘)?在Applescript中是否有內在的緩慢? – ameeps

+0

這種情況下的緩慢來自「全部內容」命令。這是專門用於深入查看所選文件夾中的所有子文件夾。如果你沒有子文件夾,你可以使用'(作爲別名選擇)'的每個文件,而應該加快速度。其他腳本編制者認爲,只要您使用「告訴應用程序」Finder「塊,您需要在運行時做一個三明治。 –