2013-08-27 69 views
0

我正在嘗試編寫一個腳本,用於重命名子文件夾中的文件夾。換句話說,該分類將走向:在AppleScript中批量重命名文件夾,將只更改每個其他文件夾的名稱

Directory/Company X/1 
Directory/Company X/2 
Directory/Company X/3 
Directory/Company X/4 
Directory/Company X/5 
Directory/Company X/6 
Directory/Company X/7 
Directory/Company X/8 
Directory/Company X/9 
Directory/Company X/10 
Directory/Company X/info 

Directory/Company X/Project 1_1 
Directory/Company X/Project 2_2 
Directory/Company X/Project 3_3 
etc.. 

到目前爲止,我有:

tell application "Finder" 

-- Defining Pro Folders 
set pro_folder to folder POSIX file "/Users/Jart/Desktop/test" 
set all_pro_folders to every folder of pro_folder 

-- Iterate through folders and check if folder name is greater than one character 
repeat with x in all_pro_folders 
    repeat with y in x 
     -- Convert the folder name to a string and check if that string is longer than one character 
     set incomingName to folder ((y as alias) as text) 
     set incomingNameString to name of incomingName 
     if length of incomingNameString = 1 then 
      -- Set old name (number) to a new variable 
      set oldname to folder ((y as alias) as text) 
      set oldnameString to name of oldname 
      -- Do it again for the second part 
      set oldname2 to folder ((y as alias) as text) 
      set oldnameString2 to name of oldname2 
      -- Rename here 
      set name of y to "Project" & " " & oldnameString & "_" & oldnameString 
     end if 
    end repeat 
end repeat 

end tell 

但是,文件夾現在看起來像:

Directory/Company X/3 
Directory/Company X/5 
Directory/Company X/7 
Directory/Company X/9 
Directory/Company X/10 
Directory/Company X/info 
Directory/Company X/Project 1_1 
Directory/Company X/Project 2_2 
Directory/Company X/Project 4_4 
Directory/Company X/Project 6_6 
Directory/Company X/Project 8_8 

它爲什麼這樣做?如何更改此代碼以便重命名所有文件?如果你願意,我可以轉儲事件/回覆日誌。

+0

是存在的,這是標記的原因[標籤:VBA] ?我沒有看到它(?) – enderland

+0

沒有applescript與vba有什麼關係? –

+0

jart,vba與applescript無關,最好不要在這些問題中使用該標籤。 – adamh

回答

2

Jart,主要問題是在第二個循環中,您需要循環遍歷x文件夾變量的每個文件夾。它似乎循環通過該文件夾的工作,但當你重命名它們導致索引問題導致只讀取每個第二個文件夾。

這裏有一點改進。

  • 將您的變量從y和x更名爲不太神祕。
  • 更改了長度測試以支持名稱> 9.
  • 我不確定新名稱末尾的項目,以及它是否應該是遞增數字。 (你可以輕鬆地恢復)
  • 留在供您參考日誌調用,這些都可以幫助調試東西
tell application "Finder" 
    -- Defining Pro Folders 
    set pro_folder to folder POSIX file "/Users/Jart/Desktop/test" 
    set all_pro_folders to every folder of pro_folder 

    -- Iterate through folders and check if folder name is greater than one character 
    repeat with parent_folder in all_pro_folders 
     set counter to 0 -- reset a counter variable we use for the end of the name 
     set child_folders to every folder of parent_folder 
     repeat with current_folder in child_folders 
      set fold_name to the name of current_folder 
      log (fold_name) 
      if (the length of fold_name < 3) then -- this will allow names > 9 
       set counter to counter + 1 -- increment the counter every time we find valid folder 
       set new_name to "Project" & " " & fold_name & "_" & counter 
       log (new_name) 
       set the name of current_folder to new_name 
      end if 
     end repeat 
    end repeat 
end tell 

HTH

+0

再次感謝!真的很感謝你幫助我。爲了將來的參考,哪裏會是瞭解更多關於AppleScript的好地方?它似乎非常有用。是否還有某種詞彙表? –

+0

不用擔心。我會說繼續做你的工作,試圖自動化和解決問題,並複製和閱讀其他applescripts。 Applescript語言指南是一個很好的參考:https://developer.apple.com/library/mac/documentation/AppleScript/Conceptual/AppleScriptLangGuide/AppleScriptLanguageGuide.pdf。或者查看亞馬遜的概述類型書籍,當然谷歌;-) – adamh

相關問題