2013-09-22 56 views
0

我正好有11個文件。這些被命名爲1.jpg,2.jpg到11.jpg。我已經提出了一半的工作。在AppleScript中批量重命名文件,僅更改每個其他文件的名稱

但問題是隻有一半的文件被處理。特別是只有奇數編號的文件被處理,即每跳第二個文件。有什麼想法嗎?

我已經注意到這個人在重命名文件夾時有類似的問題,但是我沒有足夠的知識將它應用到我的場景中。 Batch Renaming Folders in AppleScript, Will Only Change Names of Every Other Folder

display dialog "Person ID number. (numerals only)?" default answer "" 
tell application "Finder" 
set personid to text returned of result 
set home_path to home as text 
set file1name to "_front" 
set file2name to "_prof" 
set file3name to "_neck" 
set file4name to "_no_phon" 
set file5name to "_phon" 
set file6name to "_max" 
set file7name to "_prof_max" 
set file8name to "_relax" 
set file9name to "_prof_relax" 
set file10name to "_depress_no_phon" 
set file11name to "_depress_phon" 

tell folder (home_path & "Downloads:sagicphotos") 
    set name of file 1 to "PRN" & personid & file1name & "." & name extension of file 1 
    set name of file 2 to "PRN" & personid & file2name & "." & name extension of file 2 
    set name of file 3 to "PRN" & personid & file3name & "." & name extension of file 3 
    set name of file 4 to "PRN" & personid & file4name & "." & name extension of file 4 
    set name of file 5 to "PRN" & personid & file5name & "." & name extension of file 5 
    set name of file 6 to "PRN" & personid & file6name & "." & name extension of file 6 
    set name of file 7 to "PRN" & personid & file7name & "." & name extension of file 7 
    set name of file 8 to "PRN" & personid & file8name & "." & name extension of file 8 
    set name of file 9 to "PRN" & personid & file9name & "." & name extension of file 9 
    set name of file 10 to "PRN" & personid & file10name & "." & name extension of file 10 
    set name of file 11 to "PRN" & personid & file11name & "." & name extension of file 11 
end tell 
end tell 

回答

0

當你重命名這些文件,它們的順序變化,讓一些文件永遠不會改名。剛剛得到的文件列表重命名他們面前:

set personid to "1234" 

set filenames to paragraphs of "_front 
_prof 
_neck 
_no_phon 
_phon 
_max 
_prof_max 
_relax 
_prof_relax 
_depress_no_phon 
_depress_phon" 

set filelist to paragraphs of (do shell script "cd ~/Downloads/sagicphotos; ls | sort -n") 
tell application "Finder" 
    set i to 1 
    repeat with f in filelist 
     tell file ((home as text) & "Downloads:sagicphotos:" & f) 
      set name to "PRN" & personid & item i of filenames & "." & name extension 
     end tell 
     set i to i + 1 
    end repeat 
end tell 

或使用shell腳本:

personid=1234 

filenames=(_front 
_prof 
_neck 
_no_phon 
_phon 
_max 
_prof_max 
_relax 
_prof_relax 
_depress_no_phon 
_depress_phon) 

cd ~/Downloads/sagicphotos 
for f in $(ls | sort -n); do 
    mv $f PRN$personid${filenames[i++]}.${f##*.} 
done 
+0

我看到這個問題的一個問題是「文件名」的順序和文件的順序,即1,2,3,4 ... wil我不匹配。文件10將例如用_prof命名。 – markhunte

+0

@markhunte你是對的,我編輯了答案。 – user495470

+0

非常好,ls救援。我總是忘記那顆小寶石。知道有比我的糾結更好的方法。 – markhunte

0

略有變化給用戶:495470答案。

正如我在評論中指出的那樣。文件名「和文件的順序,即1,2,3,4 ...將不匹配,如果你沒有考慮到返回的文件列表的排序順序和你的文件名列表的順序

set theHomeF to ((path to home folder) & "Desktop:test:test1" as text) as alias 
set personid to "1234" 
set filenames to paragraphs of "_front 
_prof 
_neck 
_no_phon 
_phon 
_max 
_prof_max 
_relax 
_prof_relax 
_depress_no_phon 
_depress_phon" 

tell application "Finder" 
    set file_namesList to "" 
    set AppleScript's text item delimiters to {" 
"} 
    set thefiles to files of theHomeF 

    set theNames to (name of files of theHomeF) 

    set file_namesList to text items of theNames as string 
    set AppleScript's text item delimiters to {""} 
    set file_namesList to paragraphs of (do shell script " echo " & quoted form of file_namesList & "|sort -n") 
    repeat with i from 1 to number of items in file_namesList 
     set this_item to item i of file_namesList 
     set thisFile to (some file of theHomeF whose name is this_item) 
     set name of thisFile to "PRN" & personid & item i of filenames & "." & name extension of thisFile 
    end repeat 
end tell 

注意管線設備的AppleScript的文本項分隔符{「 」}有\ n(引號之間的新行)

我相信有這樣做的更好的辦法,但正如他們所說的時間是金錢...