Pick of the Week - Nov 10 [Show all picks]
Path Finder 5 - A feature-laden Finder replacement
Submit Hint Search The Forums LinksStatsPollsFAQHeadlinesRSS
12,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 0.00 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (0 votes cast)
 
[13,516 views]  

An AppleScript to tidy up the Desktop | 8 comments | Create New Account
Click here to return to the 'An AppleScript to tidy up the Desktop' hint
The following comments are owned by whomever posted them. This site is not responsible for what they say.
An AppleScript to tidy up the Desktop
Authored by: rajanarora on Fri, Nov 10 2006 at 1:05PM PST
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 Fri, Nov 10 2006 at 4:51PM PST
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 Fri, Nov 10 2006 at 6:30PM PST
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 Fri, Nov 10 2006 at 6:46PM PST
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 Sat, Nov 11 2006 at 4:12AM PST
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 Sun, Nov 12 2006 at 8:15PM PST
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 Tue, Jan 30 2007 at 5:25PM PST
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 Thu, Feb 1 2007 at 12:20PM PST
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 | # ]