1. Welcome to the new MacRumors forums. See our announcement and read our FAQ

Basic Folder Action Script Help

Discussion in 'Mac Programming' started by terriyaki, Nov 1, 2007.

  1. macrumors 6502a

    #1
    I'm trying to make a Folder Action script that moves .torrent and .mp3 files from one folder two sub folders upon files being added to the original folder. I whipped up this script and I can get the torrent files to move but not the mp3s.

    Can someone please help me? :)

    Code:
    on adding folder items to this_folder after receiving added_items
    	repeat with item_ in added_items
    		tell application "Finder"
    			
    			if name extension of item_ is "torrent" then
    				try -- in case of name conflict in destination
    					move item_ to folder "Zoidberg:Users:terriyaki:Desktop:DropBox:Torrents:"
    				end try
    				
    			else if name extension of item_ is "mp3" then
    				try
    					move item_ to folder "Zoidberg:Users:terriyaki:Desktop:DropBox:Songs:"
    				end try
    			end if
    			
    		end tell
    	end repeat
    end adding folder items to
    
     
  2. macrumors member

    #2
    try this

    HTML:
    property image_extension_list : {"tif", "tiff", "gif", "png", "pict", "pct", "jpg", "jpeg"}
    property video_extension_list : {"avi", "mov", "mpg", "mpeg", "wmv"}
    property audio_extension_list : {"mp3", "wav", "mp4", "aac", "m4a", "m4b"}
    property archive_extension_list : {"zip", "sit", "tar", "tgz", "gz", "bzip2"}
    property documents_extension_list : {"doc", "pdf", "txt", "xls", "ppt"}
    property application_extension_list : {"dmg", "hqx", "toast"}
    
    property image_foldername : "Images"
    property video_foldername : "Video"
    property audio_foldername : "Audio"
    property archive_foldername : "Archives"
    property documents_foldername : "Documents"
    property application_foldername : "Applications"
    
    
    on adding folder items to this_folder after receiving added_items
    	
    	repeat with this_item in added_items
    		tell application "Finder"
    			
    			if (the name extension of the this_item is in the image_extension_list) then
    				my makeamove(this_item, this_folder, image_foldername)
    			end if
    			
    			if (the name extension of the this_item is in the video_extension_list) then
    				my makeamove(this_item, this_folder, video_foldername)
    			end if
    			
    			if (the name extension of the this_item is in the audio_extension_list) then
    				my makeamove(this_item, this_folder, audio_foldername)
    			end if
    			
    			if (the name extension of the this_item is in the archive_extension_list) then
    				my makeamove(this_item, this_folder, archive_foldername)
    			end if
    			
    			if (the name extension of the this_item is in the documents_extension_list) then
    				my makeamove(this_item, this_folder, documents_foldername)
    			end if
    			
    			if (the name extension of the this_item is in the application_extension_list) then
    				my makeamove(this_item, this_folder, application_foldername)
    			end if
    			
    		end tell
    	end repeat
    end adding folder items to
    
    on makeamove(this_item, root_folder, target_foldername)
    	
    	tell application "Finder"
    		
    		if not (exists folder target_foldername of root_folder) then
    			make new folder at root_folder with properties {name:target_foldername}
    		end if
    		
    		set the target_folder to folder target_foldername of root_folder
    		my resolve_conflicts(this_item, root_folder, target_folder)
    		move file this_item to the target_folder
    		
    	end tell
    	
    end makeamove
    
    on resolve_conflicts(this_item, root_folder, target_folder) --renames the item if dublicate exists in target folder
    	
    	tell application "Finder"
    		
    		set file_extension to the name extension of this_item
    		set the file_name to the name of this_item
    		
    		if the file_extension is "" then
    			set the trimmed_name to the file_name
    		else
    			set the trimmed_name to text 1 thru -((length of file_extension) + 2) of the file_name
    		end if
    		
    		if (exists document file file_name of target_folder) then
    			
    			set the name_increment to 1
    			
    			repeat
    				set the new_name to (the trimmed_name & "_" & (name_increment as string) & "." & file_extension) as string
    				
    				if not (exists document file new_name of the target_folder) then
    					
    					set the name of document file file_name of folder root_folder to the new_name
    					exit repeat
    				else
    					
    					set the name_increment to the name_increment + 1
    					
    				end if
    			end repeat
    		end if
    	end tell
    	
    	return the file_name
    	
    end resolve_conflicts
     
  3. macrumors newbie

    #3
    similiar method but don’t know how to do

    i want to try similar method but don’t know how to do?
    i want to move all my pdf files from documents to pdf folder..
    i want to replace if same file is there….

    pls help.
     
  4. macrumors newbie

    #4
    How do you get this to work?
     

Share This Page