Go Back   The macosxhints Forums > Working with OS X > OS X Developer



Reply
 
Thread Tools Rating: Thread Rating: 2 votes, 4.50 average. Display Modes
Old 11-09-2004, 07:36 PM   #1
MikeBell
Prospect
 
Join Date: May 2003
Posts: 26
Folder Actions - Automatically sorting Safari's downloads folder?

Hi all,

I'm wondering if someone could help me with a folder action... I'd like to automate sorting of files that get downloaded to Safari's downloads folder (to keep it tidy). For example, if I download an Image, that image should be moved to a folder I specify. I guess the simplest thing to do is to look at the file extension and sort it based on that.

Could someone quickly show us a piece of code that moves a file based on file extension?

Thanks!!

Mike
MikeBell is offline   Reply With Quote
Old 11-10-2004, 01:07 AM   #2
robJ
Major Leaguer
 
Join Date: Aug 2003
Posts: 429
This might work.

Code:
 on adding folder items to this_folder after receiving added_items
  repeat with item_ in added_items
   tell application "Finder"
    if name extension of item_ is "jpg" then
     try -- in case of name conflict in destination
      move item_ to folder "path:to:images:"
     end try
    end if
    if name extension of item_ is "mp3" then
     try
      move item_ to folder "path:to:music:"
     end try
    end if
   end tell
  end repeat
 end adding folder items to
-- Rob
robJ is offline   Reply With Quote
Old 11-10-2004, 05:12 AM   #3
helloworld
Prospect
 
Join Date: Nov 2004
Posts: 21
completeish solution

Just what I needed for my messy downloads folder, brilliant, I love applescript.

Combined robJ's solution with some Library's Folder Actions script and came up with this.

I'm using a lists for file extensions and there's also a handler which renames the file if a dublicate exists.

Maybe not the tidiest piece of script but does what it's told and it should be easy enough to extend.

:::
Code:
(*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

Last edited by helloworld; 11-10-2004 at 05:43 AM.
helloworld is offline   Reply With Quote
Old 11-10-2004, 01:01 PM   #4
MikeBell
Prospect
 
Join Date: May 2003
Posts: 26
Thanks guys! Both of the scripts work really well! Awesome stuff
MikeBell is offline   Reply With Quote
Old 11-10-2004, 03:30 PM   #5
Jacob Spindel
Triple-A Player
 
Join Date: Oct 2004
Posts: 70
You don't even need a script. Go to the downloads folder, hit command-J, choose "This folder only," and choose "Keep arranged by " and then select your sorting criteria.
__________________
Want Gmail? PM me!
Jacob Spindel is offline   Reply With Quote
Old 11-10-2004, 10:37 PM   #6
MikeBell
Prospect
 
Join Date: May 2003
Posts: 26
Quote:
Originally Posted by Jacob Spindel
You don't even need a script. Go to the downloads folder, hit command-J, choose "This folder only," and choose "Keep arranged by " and then select your sorting criteria.

I don't think you understand what we're talking about. Re-read this thread. We're talking about moving files.
MikeBell is offline   Reply With Quote
Old 02-22-2006, 03:03 PM   #7
kupietz
Prospect
 
Join Date: Jun 2005
Posts: 43
Quote:
Originally Posted by MikeBell
I don't think you understand what we're talking about. Re-read this thread. We're talking about moving files.

I think what Jacob is suggesting is that the needs that the original poster is looking to fulfill might be fulfilled without having to move files at all.
kupietz is offline   Reply With Quote
Old 03-25-2006, 07:37 AM   #8
retrac2
Registered User
 
Join Date: Mar 2006
Posts: 1
Hiya, very useful script,

I've modified it to sort .torrent files (see below) but how do I get it to open a specific filetype when all this is done. Ie open the torrent file so downloading torrents becomes a one click process. Ideally it would open the file after it has been moved/renamed so as not to confuse the torrent app. I had a go but I haven't tried applescript in such a long time I'm a little bit unsure.

Thanks P.S added rar to the archives extension list.
Code:
(*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", "rar"}
property torrent_extension_list : {"torrent"}

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



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
			
			if (the name extension of the this_item is in the torrent_extension_list) then
				
				my makeamove(this_item, this_folder, torrent_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
retrac2 is offline   Reply With Quote
Old 03-01-2008, 04:55 PM   #9
m021478
Major Leaguer
 
Join Date: Jul 2005
Posts: 364
Not sure if anyone is still watching this thread, but in case someone is, check out a program called Hazel - www.noodlesoft.com

It does everything mentioned above, and a whole lot more...
m021478 is offline   Reply With Quote
Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump



All times are GMT -5. The time now is 10:25 AM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.
Site design © IDG Consumer & SMB; individuals retain copyright of their postings
but consent to the possible use of their material in other areas of IDG Consumer & SMB.