Dear Apple freinds ,
I am writing a folder action script for some automation work , in which the dropped files are copied in certain destination directory and moved to certain folder for archiving. The script checks if the same file already exist in the destination directory then it does not copy it and move the drop file out of drop box , and exit the repeat loop .
If I drop a single file , there is no problem . But if I drop more than one file and if file already exist on its destination directory , the same file is moved out of the drop box and I have to exit the repeat loop. If I do not exit from the loop the rest of the loop statements will be executed which cause the problem . I want as soon as it moves the current file out of the drop box , the script should ignore rest of the loop statements and take the next file,so that i can drop more than one file at a time . For an example I am a perl programmer I use the next statement in my loops eg :
while ( $i > 100 ) {
if ( $i % 5 == 0 ) { next ; }else { $i++ ; print “$i \n” }
}
Right now my script is as follow:
on adding folder items to this_folder after receiving added_items
try
tell application "Finder"
--CONFIGURABLE PARAMETERS
--mac path of the dept and resource directories
--first target directory
set resource_dir to "dggroups:dfs:002-resources:000027-run:"
--second target directory
set dept_dir to "dggroups:dfs:004-operations:004-bioinformatics:001-process:000027-run:"
set archive_dir to "dggroups:dfs:006-submission:000027-run:000000-archive:"
--path to archive folders and log file
--third target directory ,corresponding to archived folder
set log_file_path to "/Volumes/dggroups/dfs/006-submission/000027-run/000000-archive/log-001-001-xx-log.txt"
set drop_box_parent_dir to "dggroups:dfs:006-submission:000027-run:"
set archive_file_extension to "zip"
--doing action on the file
repeat with this_item in (get every item of added_items)
--getting the name of the owner of the file
set owner_name to owner of this_item
--getting the name of the file
set the file_name to the name of this_item
set file_name_first_char to first character of file_name
--ignoring the dot files
if file_name_first_char is not equal to "." then
--getting the extension of the file name
--whether it matches with archive file extension
set file_extension to my get_extension(file_name)
--setting the path of the file as alias
set save_folder to (folder this_folder) as alias
set source_path to ((save_folder as string) & file_name) as string
set source_path_alias to source_path as alias
--if the file has to be copied on both the directories
--means resources and dept
if ((length of resource_dir > 0) and (length of dept_dir > 0)) then
--case_dir_num_infile_name and case_dir_name has been got from subroutine
set case_dir_name to case_dir_num_in_file_name & "-" & case_dir_name
--copying to the resource case dir
set case_dir_alias to (resource_dir & case_dir_name & ":") as alias
set copy_status to my copy_file(file_name, case_dir_alias, source_path_alias, drop_box_parent_dir)
if (copy_status is equal to 0) then
? is there any option intead of exiting from the loop I can take the next file
tell me to exit repeat
end if
end if
--other actions of the script not mentioned here
end repeat
end tell
on error the error_message number the error_number
display dialog "Error: " & the error_number & ". " & the error_message
end try
end adding folder items to
----------------------------------------------------SUBROUTINES--------------------------------------------------------------------------------------------------------------------
--subroutine to copy the file
on copy_file(file_name, case_dir_name, source_path_alias, drop_box_parent_dir)
tell application "Finder"
set file_name_to_check to ((case_dir_name as string) & (file_name as string))
if exists file file_name_to_check then
set drop_box_parent_dir_alias to drop_box_parent_dir as alias
move source_path_alias to drop_box_parent_dir_alias
set copy_status to 0
else
set file_name to duplicate source_path_alias to case_dir_name
set copy_status to 1
end if
return copy_status
end tell
end copy_file
--subroutine to rename the file
on rename_file(file_name_to_change, changed_file_name)
tell application "Finder"
set name of the file_name_to_change to changed_file_name
end tell
end rename_file
Krishna: enclose your script in AppleScript tags by selecting it and clicking the AppleScript button at the top of the form. That way, clicking the link at the top of the script will download it to a reader’s Script Editor. (I’ve done it for you) - Adam Bell
Model: iMac
AppleScript: 1.10.6
Browser: Firefox 1.5
Operating System: Mac OS X (10.4)