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

An AppleScript to tidy up the Desktop Desktop
I often let a lot of junk accumulate on my desktop, making it hard to find things. In order to keep the clutter under control, I made folders for each of the kinds of things that end up on the Desktop (music, video, documents, etc.), and then wrote a simple AppleScript to sort files on the Desktop into the correct folders. The script recognizes file types by their extension. Though the file types and destination folders are hard coded, they are set at the top of the script and should be trivial to change.

I run this script from Quicksilver when I feel my Desktop is getting out of control, but there's no reason you couldn't run it automatically as a folder action instead.
    •    
  • Currently 2.20 / 5
  You rated: 1 / 5 (5 votes cast)
 
[21,664 views]  

An AppleScript to tidy up the Desktop | 9 comments | Create New Account
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: rajanarora on Nov 10, '06 01:05:13PM

This is fantastic! Here is a slightly modified folder action version which works 'on open' for the folder 'Desktop'.

on opening folder desktop
tell application "Finder"
set myDesktop to alias ((path to desktop folder as text))

-- folders to sort into
set myMusic to alias ((path to desktop folder as text) & "Music")
set myPrograms to alias ((path to desktop folder as text) & "Programs")
set myPics to alias ((path to desktop folder as text) & "Pics")
set myTorrents to alias ((path to desktop folder as text) & "Torrents")
set myVideos to alias ((path to desktop folder as text) & "Video")
set myDocs to alias ((path to desktop folder as text) & "Documents")

--extension lists
set musicExt to {".mp3", ".aac"}
set programsExt to {".dmg", ".sit", ".app"}
set picsExt to {".jpg", ".gif", ".tif", ".png", ".psd"}
set torrentsExt to {".torrent"}
set videosExt to {".avi", ".mpg", ".mov"}
set docsExt to {".pdf", ".txt", ".php", ".doc", ".xls", ".sav", ".key", ".html", ".htm", ".pages", ".jmp"}

set allFiles to files of myDesktop
repeat with theFile in allFiles
copy name of theFile as string to FileName

repeat with ext in musicExt
if FileName ends with ext then
move theFile to myMusic
end if
end repeat

repeat with ext in programsExt
if FileName ends with ext then
move theFile to myPrograms
end if
end repeat

repeat with ext in picsExt
if FileName ends with ext then
move theFile to myPics
end if
end repeat

repeat with ext in torrentsExt
if FileName ends with ext then
move theFile to myTorrents
end if
end repeat

repeat with ext in docsExt
if FileName ends with ext then
move theFile to myDocs
end if
end repeat

repeat with ext in videosExt
if FileName ends with ext then
move theFile to myVideos
end if
end repeat

end repeat
end tell
end opening folder



[ Reply to This | # ]
A tighter script
Authored by: djr on Nov 10, '06 04:51:51PM

This is a folder action script, watching my download folder; does the same thing, but more elegantly!

on adding folder items to this_folder after receiving these_items
  set filters to {¬
    {"_To Install", {".smi", ".dmg", ".img", ".pkg", "Installer", "Updater"}}, ¬
    {"_Videos", {".mpg", ".wmv", ".rm", ".rmvb", ".avi", ".mov"}}, ¬
    {"_Bittorrent", {".torrent"}} ¬
      }
  tell application "Finder"
    repeat with added_file in these_items
      repeat with destinations from 1 to (count of items in filters)
        repeat with strings_to_match from 1 to (count of items in item 2 of item destinations in filters)
          if name of added_file contains (item strings_to_match of item 2 of item destinations of filters) then
            move added_file to folder (item 1 of item destinations of filters) of this_folder
            exit repeat
          end if
        end repeat
      end repeat
    end repeat
  end tell
end adding folder items to



[ Reply to This | # ]
An AppleScript to tidy up the Desktop
Authored by: michaelnp on Nov 10, '06 06:30:52PM

Alright, I'm not getting this to work. I assume I need to replace the ((path to Desktop as text)) with something, but I'm not sure exactly what.
Or am I doing something else wrong?



[ Reply to This | # ]
An AppleScript to tidy up the Desktop
Authored by: yoel on Nov 10, '06 06:46:39PM

No, it should just work, assuming you made those folders on your desktop. What error message are you getting?



[ Reply to This | # ]
An AppleScript to tidy up the Desktop
Authored by: mark hunte on Nov 11, '06 04:12:12AM
What missing is the folder creation in all these scripts.
If some one New to AS uses them they may not realise they
have to Manually make the folders first.
I supsect that that is the issue that 'michaelnp' or its a filevault issue.

This is a script that I put together a couple of years ago, will create the folder needed if it does not exist

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


property image_extension_list : {"tif", "tiff", "gif", "png", "pict", "pct", "jpg", "bmp", "psd"}
property media_extension_list : {"mp3", "avi", "mov", "mpg", "ram"}
property archive_extension_list : {"zip", "sit", "sitx", "dmg", "tar", "hqx", "toast", "bin", "gz", "img"}
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 image_foldername : "images-DeskTP"
property media_foldername : "media-DeskTP"
property archive_foldername : "archives-DeskTP"
property text_foldername : "Text-DeskTP"
property html_foldername : "html-DeskTP"
property pdf_foldername : "pdf-DeskTP"
property script_foldername : "Script-DeskTP"



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 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


---
mh

[ Reply to This | # ]

An AppleScript to tidy up the Desktop
Authored by: m021478 on Nov 12, '06 08:15:48PM
I was looking for functionality like this a month or two ago and came across this awesome software utility that was just released to the public in Sep '06.

http://www.noodlesoft.com/

Highly configurable. Totally sweet!!

[ Reply to This | # ]
An AppleScript to tidy up the Desktop
Authored by: svoida on Jan 30, '07 05:25:46PM

If you've got FileVault turned on for your account, it won't work. AppleScript can't deal with FileVault-mounted volumes, and the script will just fail.



[ Reply to This | # ]
An AppleScript to tidy up the Desktop
Authored by: morespace54 on Feb 01, '07 12:20:20PM

Reminder -- True story

I was using a similar script to clean up my Desktop a few months ago. The problem is I forgot that I already had a few folders on my Desktop (like games, applications, misc). What happened is that the script did not only clean up my Desktop (folder) but the Desktop sub-folders as well... :(

I ended up with 1350 mp3 files in my MP3 folder... Most of them belonging to games I had on my Desktop...

so be careful! ;)



[ Reply to This | # ]
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 | # ]