Help with my first applescript

Well i was looking around on the web for an applescript that would move files into separate folders when they were added to the folder.

i found a script on macosxhints but i wanted to modify it to move the files extensions not associated with any folder to be put in the “Other” folder

right now i have it set up very weirdly.
right now if i were to drop in a pdf it would copy it to the other folder then instantly copy it to the pdf folder. there has to be a better way to do this.

Thanks,
joe

property image_extension_list : {"tif", "tiff", "gif", "png", "pict", "pct", "jpg", "bmp", "psd"}
property media_extension_list : {"mp3", "avi", "mov", "mpg", "ram", "m4v", "wav"}
property archive_extension_list : {"zip", "sit", "sitx", "dmg", "tar", "hqx", "toast", "bin", "gz", "img", "iso", "tgz"}
property html_extension_list : {"js", "htm", "html"}
property text_extension_list : {"doc", "txt", "rtf"}
property pdf_extension_list : {"pdf"}
property script_extension_list : {"scpt"}
property app_extension_list : {"app"}


property image_foldername : "Images"
property media_foldername : "Media"
property archive_foldername : "Archives"
property text_foldername : "Text"
property html_foldername : "Html"
property pdf_foldername : "Pdf"
property script_foldername : "Script"
property app_foldername : "Apps"
property other_foldername : "Other"






on adding folder items to this_folder after receiving added_items
	try
		repeat with this_item in added_items
			tell application "Finder"
				
				if (the name extension of the this_item is not (image_extension_list is not (media_extension_list is not (archive_extension_list is not (html_extension_list is not (text_extension_list is not (pdf_extension_list is not (script_extension_list is not app_extension_list)))))))) then
					
					my makeamove(this_item, this_folder, other_foldername)
				end if
				
				if (the name extension of the this_item is in the app_extension_list) then
					
					my makeamove(this_item, this_folder, app_foldername)
					
				end if
				
				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 script_extension_list) then
					
					my makeamove(this_item, this_folder, script_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
				
				if (the name extension of the this_item is in the text_extension_list) then
					
					my makeamove(this_item, this_folder, text_foldername)
					
				end if
				
				
				if (the name extension of the this_item is in the html_extension_list) then
					
					my makeamove(this_item, this_folder, html_foldername)
					
				end if
				if (the name extension of the this_item is in the pdf_extension_list) then
					
					my makeamove(this_item, this_folder, pdf_foldername)
					
				end if
				
				
				
				
			end tell
		end repeat
	on error error_message
		display dialog error_message buttons {"OK"} default button 1
	end try
	
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

Hi Joe,

You can start with this. Suppose the variables a, b, c, … have boolean values (true, false). You can use a statement like this:

if a then
– do something
else if b then
– do something
else if c then
– do something
else – it’s none of the above
– do something else
end if

The script will execute the first one it encounters that is true.

gl,

thanks got it to work perfectly

Not tested (and only includes the first portion) but something like this should be able to help you cut back on your code a bit.

property image_list : {"Images", {"tif", "tiff", "gif", "png", "pict", "pct", "jpg", "bmp", "psd"}}
property media_list : {"Media", {"mp3", "avi", "mov", "mpg", "ram", "m4v", "wav"}}
property archive_list : {"Archives", {"zip", "sit", "sitx", "dmg", "tar", "hqx", "toast", "bin", "gz", "img", "iso", "tgz"}}
property html_list : {"Html", {"js", "htm", "html"}}
property text_list : {"Text", {"doc", "txt", "rtf"}}
property pdf_list : {"Pdf", {"pdf"}}
property script_list : {"Script", {"scpt"}}
property app_list : {"Apps", {"app"}}

property MasterList : {image_list, media_list, archive_list, html_list, text_list, pdf_list, script_list}

on adding folder items to this_folder after receiving added_items
	tell application "Finder"
		repeat with this_item in added_items
			repeat with aList in MasterList
				if item 2 of aList contains (the name extension of the this_item) then
					my makeamove(this_item, this_folder, item 1 of aList)
					return
				end if
			end repeat
		end repeat
	end tell
end adding folder items to


Neat script James.

Another way might be to name the folder with the extension type. Then you get the extension of the item and place it in the folder with that name. For eample, suppose the extension is “tiff”. Place that file in folder named “tiff”.

Edited: or maybe place the file in the folder whose name begins with “tiff”. That way it would catch “tif” also.

James,

Nice job thats what I was gonna do but you beet me too it.

so I maid a slight mod to your code


property image_list : {"Images", {"tif", "tiff", "gif", "png", "pict", "pct", "jpg", "bmp", "psd"}}
property media_list : {"Media", {"mp3", "avi", "mov", "mpg", "ram", "m4v", "wav"}}
property archive_list : {"Archives", {"zip", "sit", "sitx", "dmg", "tar", "hqx", "toast", "bin", "gz", "img", "iso", "tgz"}}
property html_list : {"Html", {"js", "htm", "html"}}
property text_list : {"Text", {"doc", "txt", "rtf"}}
property pdf_list : {"Pdf", {"pdf"}}
property script_list : {"Script", {"scpt"}}
property app_list : {"Apps", {"app"}}

property MasterList : {image_list, media_list, archive_list, html_list, text_list, pdf_list, script_list}

property mFlage : false

on adding folder items to this_folder after receiving added_items
	tell application "Finder"
		repeat with this_item in added_items
			repeat with aList in MasterList
				if item 2 of aList contains (the name extension of the this_item) then
					my makeamove(this_item, this_folder, item 1 of aList)
					set mflag to true
					return
				else
					set mflag to fales
				end if
			end repeat
		end repeat
		if mflag is false then
			my makeamove(this_item, this_folder, "other")
		end if
	end tell
end adding folder items to

mm

Doh I knew I forgot something, account for “other” LOL

Thanks Kel, I had thought about something like that, but then we would end up with a bunch of images folders rather then one.