Folder Action help please - moving files...

Hi

I’ve searched the forum but haven’t been able to find a match for my problem so I post instead.

This is the idea:
I have a Downloadfolder to which I download everything from the web. I want to create a script that when a file is added to the Downloadfolder, first checkes if there is a folder within it with the current date as foldertitle. If so the added items should be moved to the dated folder. If not then a new folder should be created and the files moved to this one. I’ve come this far:

on adding folder items to this_folder after receiving these_items
	
	tell application "Finder"
		tell application "Finder"
			set the_date to current date
			set wd to weekday of the_date as string
			set m to month of the_date as string
			set d to day of the_date as string
			set y to year of the_date as string
			set dated_folder to wd & " " & m & " " & d & " " & y as string
		end tell
		if not (exists folder dated_folder of the_date of folder "DLTest" of folder "Desktop" of home) then
			make new folder at folder "DLTest" of folder "Desktop" of home with properties {name:dated_folder}
		end if
	
		
	end tell
end adding folder items to 

This part works very well but what I haven’t been able to figure out is how to move the added files into the dated folder.

Any ideas?

Hi, adding these rows your script should be able to do the job:

a) create another variable in your existing if-block in order ho have a reference to the “dated folder” where to store your downloaded files:

(...)
 if not (exists folder dated_folder of the_date of folder "DLTest" of folder "Desktop" of home) then 
         make new folder at folder "DLTest" of folder "Desktop" of home with properties {name:dated_folder} 
set myFolder to the result
else
set myFolder to (folder dated_folder of the_date of folder "DLTest" of folder "Desktop" of home)
end if

b) now that you have defined the folder to use you can move your files in:

set c to count of these_items
repeat with i from 1 thru c
move item i of these_items to myFolder
end repeat

I’m not sure however if downloading a file into the targetfolder will trigger a folderaction. I’m afraid it works only with files added by drag and drop but I might be wrong…

Good scripting
Farid

Here’s a completeish solution for something similar to what you’re looking for. Does the job and it’s easy to extend so should be helpful.

See http://forums.macosxhints.com/showthread.php?t=30441 for the original thread.

(*This folder action script is designed to keep your downloads folder organised *)

property image_extension_list : {"tif", "tiff", "gif", "png", "pict", "pct", "jpg"}
property media_extension_list : {"mp3", "avi", "mov", "mpg"}
property archive_extension_list : {"zip", "sit", "dmg", "tar", "hqx", "toast"}

property image_foldername : "images"
property media_foldername : "media"
property archive_foldername : "archives"



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 media_extension_list) then
				
				my makeamove(this_item, this_folder, media_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
			
		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