2013-10-03 15 views
0

我已經寫了一個Applescript,做我想做的事情。即:錯誤處理在Applescript根據文件名將文件移動到目標文件夾

根據文件名將文件從源文件夾移動到目標文件夾中的子文件夾的子文件夾。

但我的問題是腳本中沒有錯誤處理。

舉例: 文件 「130299_1833_9_Actierondje Cantomobili菲戈400mm_LR.jpg」獲取目標文件夾放置在子文件夾「00009」的子目錄「0001833」

不過,我想腳本檢查文件名的第二和第三部分實際上是數字/整數。如果它們不是數字/整數,則文件將移至錯誤文件夾,並且腳本將繼續處理源文件夾的下一個文件。

舉例: 文件 「130299_9_Actierondje Cantomobili菲戈400mm_LR.jpg」 以及 「」 130299_1833_Actierondje Cantomobili菲戈400mm_LR.jpg」獲得放置在錯誤的文件夾

目前,我有以下的AppleScript:

set source_folder to alias "Macintosh HD:Users:Roy:Desktop:_LR JPG's" --as alias 
set destination_folder to "Macintosh HD:Users:Roy:Desktop:Produktcommunicatie test" as alias 
set org_delimit to text item delimiters of AppleScript 

tell application "Finder" 

set source_files to files of source_folder 

--display a dialog if there are no files to move 
if number of source_files is 0 then 
    display dialog "There are no files to move" buttons {"OK"} default button 1 
end if 

repeat with this_file in source_files 
    --set the leveranciersnummer en modelnummer 
    set text item delimiters of AppleScript to {"_"} 
    set mgDocname to name of this_file 
    set mgLeveranciersnummer to text -7 thru -1 of ("0000000" & text item 2 of mgDocname) 
    set mgModelnummer to text -5 thru -1 of ("00000" & text item 3 of mgDocname) 

    --check if the folders already exist in the destination, if not, create the folders 
    if (exists folder mgLeveranciersnummer of folder destination_folder) is false then 
     make new folder at destination_folder with properties {name:mgLeveranciersnummer} 
    end if 

    set text item delimiters of AppleScript to org_delimit 
    set model_folder to destination_folder & mgLeveranciersnummer as string 
    --set model_folder to alias model_folder 

    if (exists folder mgModelnummer of folder model_folder) is false then 
     make new folder at model_folder with properties {name:mgModelnummer} 
    end if 

    set final_path to model_folder & ":" & mgModelnummer as string 
    move this_file to final_path with replacing 
end repeat 

--display a dialog if the all the files are moved 
display dialog "All the files are moved to " & destination_folder buttons {"Thanks!"} default button 1 

end tell 
+0

所以基本上你只需要看看他們是數字或不...看看這個帖子http://stackoverflow.com/questions/918493/how-do-i-check-if-text -can-be-converted-to-a-number-in-applescript – mcgrailm

+0

這確實是我想要做的,但是如果無法將變量設置爲數。 – sirbaxx

+0

我在你的腳本中沒有看到你正在測試的數字 – mcgrailm

回答

0

您可以使用一個shell腳本,這樣反而:

cd ~/Desktop/_LR\ JPG\'s 
for f in *; do 
    f2=$(cut -d_ -f2 <<< "$f") 
    f3=$(cut -d_ -f3 <<< "$f") 
    if [[ $f2 =~ ^[0-9]+$ && $f3 =~ ^[0-9]+$ ]]; then 
    d=~/Desktop/Produktcommunicatie\ test/$(printf %07d $f2)/$(printf %07d $f3) 
    else 
    d=~/Desktop/error 
    fi 
    mkdir -p "$d" 
    mv "$f" "$d" 
done 
+0

如果您要用與標籤不同的語言回答問題,則應對腳本發表評論。 – adayzdone

+0

我對shell腳本還不夠熟悉,所以我想在Applescript中完成這一切。這也是一個同事經常使用的腳本。因此,對於我而言,Applescript隨後可以在需要時輕鬆更新。謝謝你的回覆! – sirbaxx

0

我已經設法拿到劇本的第一個工作日處理所有與錯誤文件名的文件,並將這些文件移動到一個單獨的文件夾(文件夾錯誤)

,然後可在源文件夾中留下應有的文件正確的文件名並進一步處理。

這裏的最後工作腳本:

這是一個很長時間的,因爲附加的錯誤處理,我已經修改了它與我們的服務器上的文件夾。

set drive_name to "Communicatie" 
set network_drive to "afp://10.1.96.1/" & drive_name 
set lr_folder to "__LR JPG's" 
set error_folder to "__LR Error Files" 

set drive_name_beeldenbank to "beeldenbank$" 
set beeldenbank_folder to "Produktcommunicatie" 
set beeldenbank_drive to "smb://10.1.11.3/" & drive_name_beeldenbank 


set org_delimit to text item delimiters of AppleScript 


-- check if al the folders exist, otherwise create them 

tell application "Finder" 

-------- check the 1st network drive and folder 

try 
    mount volume network_drive 
on error 
    display dialog "The network drive" & network_drive & " could not be found or connected" buttons {"Owkee"} default button 1 
    error number -128 --cancel the script 
end try 

set drive_name to "Volumes:" & drive_name as alias 

--- check the source folder 
if (exists folder lr_folder of folder drive_name) is false then 
    make new folder at drive_name with properties {name:lr_folder} 
end if 

--- check the error folder 
if (exists folder error_folder of folder drive_name) is false then 
    make new folder at drive_name with properties {name:error_folder} 
end if 


set source_folder to folder lr_folder of drive_name 
set error_folder to folder error_folder of drive_name 

-------- check the 2nd network drive and folder 

try 
    mount volume beeldenbank_drive 
on error 
    display dialog "The network drive" & beeldenbank_drive & " could not be found or connected" buttons {"Owkee"} default button 1 
    error number -128 --cancel the script 
end try 

set beeldenbank_drive to "Volumes:" & drive_name_beeldenbank as alias 

set destination_folder to folder beeldenbank_folder of beeldenbank_drive as alias 


end tell 


tell application "Finder" 

----------- First routine to check for files that are not named correctly 

set source_files to files of source_folder 

--display a dialog if there are no files to move 
if number of source_files is 0 then 
    display dialog "There are no files to move" buttons {"OK"} default button 1 
    error number -128 --cancel the script 
end if 


--tell application "Finder" 
repeat with this_file in source_files 
    --set the leveranciersnummer en modelnummer 
    set text item delimiters of AppleScript to {"_"} 
    set mgDocname to name of this_file 
    try 
     set mgLeveranciersnummer to text -7 thru -1 of ("0000000" & text item 2 of mgDocname) 
     set mgModelnummer to text -5 thru -1 of ("00000" & text item 3 of mgDocname) 
    end try 
    try 
     set mgCheck to mgLeveranciersnummer as number 
    on error 
     set label index of this_file to 2 
    end try 

    try 
     set mgCheck to mgModelnummer as number 
    on error 
     try 
      set label index of this_file to 2 
     end try 
    end try 
end repeat 
--end tell 

move (every file of source_folder whose label index is 2) to error_folder 

set source_files to files of source_folder 
--display a dialog if there are no files to move 
if number of source_files is 0 then 
    display dialog "There are no files with correct file names to move" buttons {"OK"} default button 1 

    if (count of files in error_folder) is greater than 0 then 
     display dialog "the following files are not named correctly:" buttons {"OK, I'll see what I can do about that!"} default button 1 
     open error_folder 
    end if 

    error number -128 --cancel the script 
end if 

---------------------------- second routine to process the correctly named files 

repeat with this_file in source_files 
    --set the leveranciersnummer en modelnummer 
    set text item delimiters of AppleScript to {"_"} 
    set mgDocname to name of this_file 
    set mgLeveranciersnummer to text -7 thru -1 of ("0000000" & text item 2 of mgDocname) 
    set mgModelnummer to text -5 thru -1 of ("00000" & text item 3 of mgDocname) 

    --check if the folders already exist in the destination, if not, create the folders 
    if (exists folder mgLeveranciersnummer of folder destination_folder) is false then 
     make new folder at destination_folder with properties {name:mgLeveranciersnummer} 
    end if 

    set text item delimiters of AppleScript to org_delimit 
    set model_folder to destination_folder & mgLeveranciersnummer as string 
    --set model_folder to alias model_folder 

    if (exists folder mgModelnummer of folder model_folder) is false then 
     make new folder at model_folder with properties {name:mgModelnummer} 
    end if 

    set final_path to model_folder & ":" & mgModelnummer as string 
    move this_file to final_path with replacing 
    delete this_file 
end repeat 

----- display a dialog if the all the files are moved 
display dialog "All the files are moved to¬ 
" & destination_folder buttons {"Thanks!"} default button 1 


----- open and display the error files in the error folder 
if (count of files in error_folder) is greater than 0 then 
    display dialog "the following files are not named correctly:" buttons {"OK, I'll see what I can do about that!"} default button 1 
    open error_folder 
end if 

end tell 
相關問題