Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!


Click here to return to the 'An AppleScript to tidy up the Desktop' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
An AppleScript to tidy up the Desktop
Authored by: myccroft on Oct 17, '11 01:49:52PM
Thanks for all the great inspiration. I've made a few changes to make it easier to update the filters. Sorry, but the names are in Swedish. Hope you can decipher anyways.


(* Organization script *)

property image_extension_list : {"tif", "tiff", "gif", "png", "pict", "pct", "jpg", "bmp", "psd", "eps", "CR2"}
property music_extension_list : {"mp3", "aac", "m4a", "m4b", "ogg", "mid"}
property video_extension_list : {"avi", "mov", "mpg", "mkv", "264", "flv", "wmv", "mp4"}
property archive_extension_list : {"zip", "sit", "sitx", "tar", "hqx", "gz", "bz2", "tgz"}
property ios_extension_list : {"ipa", "ipsw"}
property application_extension_list : {"dmg", "toast", "bin", "img", "app", "xpi", "pkg", "mpkg", "iso"}
property html_extension_list : {"js", "htm", "html"}
property text_extension_list : {"doc", "txt", "rtf", "odt", "docx", "pages", "tex", "xls", "pptx", "ppt", "keynote", "key", "csv", "nfo"}
property pdf_extension_list : {"pdf"}
property code_extension_list : {"c", "h"}
property script_extension_list : {"scpt", "sh", "php"}
property torrent_extension_list : {"torrent"}

property filandelser_list : {image_extension_list, torrent_extension_list, music_extension_list, video_extension_list, archive_extension_list, ios_extension_list, application_extension_list, html_extension_list, text_extension_list, pdf_extension_list, code_extension_list, script_extension_list}

property image_foldername : "Bilder"
property music_foldername : "Musik"
property video_foldername : "Video"
property archive_foldername : "Packade filer"
property ios_foldername : "iOS Program"
property application_foldername : "Program"
property html_foldername : "Webfiler"
property text_foldername : "Document"
property pdf_foldername : "PDFer"
property code_foldername : "Kod"
property script_foldername : "Script"
property torrent_foldername : "Torrents"

property mappnamn_list : {image_foldername, torrent_foldername, music_foldername, video_foldername, archive_foldername, ios_foldername, application_foldername, html_foldername, text_foldername, pdf_foldername, code_foldername, script_foldername}

property listToIterateThrough : {Mappnamn:mappnamn_list, Filandelser:filandelser_list}

on adding folder items to this_folder after receiving added_items
	set antalMappar to number of items in Mappnamn of listToIterateThrough
	
	(*tell application "Finder"
	set this_folder to (choose folder)
	set added_items to (every file in this_folder) as alias list
end tell*)
	
	try
		repeat with this_item in added_items
			tell application "Finder"
				
				set name_extension to the name extension of the this_item
				set Mapp to 1
				repeat until Mapp is greater than antalMappar
					set dennaMapp to item Mapp of Mappnamn of listToIterateThrough
					if name_extension is in item Mapp of Filandelser of listToIterateThrough then
						my makeamove(this_item, this_folder, dennaMapp)
						if dennaMapp is in {archive_foldername, application_foldername} then
							my openItem(this_item, this_folder, dennaMapp)
						end if
						exit repeat
					end if
					set Mapp to Mapp + 1
				end repeat
			end tell
		end repeat
		
	on error error_message
		display dialog error_message buttons {"OK"} default button 1
	end try
	
end adding folder items to

to 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

to openItem(thisItem, rootFolder, targetFolderName)
	tell application "Finder"
		display dialog ¬
			"Ska jag öppna " & name of thisItem & ¬
			"?" buttons {"Ja", "Håll käften"} ¬
			default button ¬
			"Ja" cancel button ¬
			"Håll käften" giving up after 4 ¬
			with title "Öppningsbar fil"
		if button returned of result is not "Håll käften" then
			set the targetFolder to folder targetFolderName of rootFolder
			set the fileName to the name of thisItem
			--		display dialog (fileName of targetFolder) as string
			if exists document file fileName of targetFolder then
				open document file fileName of targetFolder
			end if
		end if
	end tell
	
end openItem

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


[ Reply to This | # ]