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

Use folder actions to manage downloaded images System

I had heard a lot about Folder Actions, but after searching a lot of the script archives, I couldn't find anything really useful. I thought a good use for Folder Actions would be to decrease Desktop clutter, so I wrote this script to maneuver any image files (based on the extension) into an "unfiled" folder in the ~/Pictures directory.

All that you have to do is copy and paste the following text into ScriptEditor, save it as a .scpt file, then apply a folder action to your downloads directory, and voila!

on adding folder items to this_folder after receiving added_items
  tell application "Finder"
    repeat with aFile in added_items
      if the name of aFile contains {".jpg"} 
       or the name of aFile contains {".gif"} 
       or the name of aFile contains {".png"} 
       or the name of aFile contains {".pict"} 
       or the name of aFile contains {".tiff"} 
       or the name of aFile contains {".gif"} 
       or the name of aFile contains {".psd"} 
       or the name of aFile contains {".bmp"} then
      tell application "Finder"
        try
          move aFile to folder "Mac_name:Users:User_name:Pictures:unfiled:"
          on error err
            display dialog err
         end try
       end tell
       display dialog 
       "Image files have been moved to the Unfiled folder in your Pictures folder."
     end if
   end repeat
  end tell
end adding folder items to
Hope you enjoy!

[robg adds: Folder actions were originally discussed in this hint, and there have been a number of hints since then which put them to use. In Panther, the interface for Folder Actions has been greatly improved (I found the 10.2 version very confusing!), so hopefully we'll see even more examples putting them to good use.]
    •    
  • Currently 3.00 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (2 votes cast)
 
[17,381 views]  

Use folder actions to manage downloaded images | 16 comments | Create New Account
Click here to return to the 'Use folder actions to manage downloaded images' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Error for me...
Authored by: jctull on Oct 30, '03 12:53:18PM

When I attach this and move an image file into the folder Downloads, I get this error:

Finder got an error: Can't get item 1 of [akuas "My Disk:Users:username:Desktop:Downloads:filename.jpg"}.

Any ideas?



[ Reply to This | # ]
Error for me...
Authored by: ehgoodwin on Oct 30, '03 02:05:39PM

replace the user_name part of the path with your actual username



[ Reply to This | # ]
Error for me...
Authored by: jctull on Oct 30, '03 04:58:12PM

No. The actual error has my username correct. Something else is going on here. It happens with the improved scripts below as well.



[ Reply to This | # ]
try this
Authored by: applepal on Oct 30, '03 02:50:47PM

on adding folder items to this_folder after receiving added_items
	
	set compName to (do shell script "hostname | awk -F. '{print $1}'") as string
	set UserN to (system attribute "HOME") as string
	set userpath to compName & UserN
	set OldDelims to AppleScript's text item delimiters
	set AppleScript's text item delimiters to "/"
	set getuserpath to text items of userpath
	set AppleScript's text item delimiters to ":"
	set resultpath to getuserpath as text
	set AppleScript's text item delimiters to OldDelims
	set unfiledpictures to (resultpath & ":Pictures:unfiled:") as text
	
	repeat with aFile in added_items
		tell application "Finder"
			if the name of aFile contains {".jpg"} 
				or the name of aFile contains {".gif"} 
				or the name of aFile contains {".png"} 
				or the name of aFile contains {".pict"} 
				or the name of aFile contains {".tiff"} 
				or the name of aFile contains {".gif"} 
				or the name of aFile contains {".psd"} 
				or the name of aFile contains {".bmp"} then
				tell application "Finder"
					try
						move aFile to folder unfiledpictures
					on error err
						display dialog err
					end try
				end tell
				display dialog 
					"Image files have been moved to the Unfiled folder in your Pictures folder."
			end if
		end tell
	end repeat
end adding folder items to


[ Reply to This | # ]
try this
Authored by: Graff on Oct 30, '03 03:10:59PM

That looks like it will work but there is an easier way. Try adding this line instead of what you did:

set folderLocation to (container of (path to desktop folder) as string) & "Pictures:unfiled:"

So your script will end up like this:

on adding folder items to this_folder after receiving added_items
  tell application "Finder"

-- Here is the magic statement we added
    set folderLocation to (container of (path to desktop folder) as string) & "Pictures:unfiled:"

    repeat with aFile in added_items
      if the name of aFile contains {".jpg"} 
       or the name of aFile contains {".gif"} 
       or the name of aFile contains {".png"} 
       or the name of aFile contains {".pict"} 
       or the name of aFile contains {".tiff"} 
       or the name of aFile contains {".gif"} 
       or the name of aFile contains {".psd"} 
       or the name of aFile contains {".bmp"} then
      tell application "Finder"
        try

-- This next line is also changed
          move aFile to folder folderLocation

          on error err
            display dialog err
         end try
       end tell
       display dialog 
       "Image files have been moved to the Unfiled folder in your Pictures folder."
     end if
   end repeat
  end tell
end adding folder items to


[ Reply to This | # ]
that is MUCH better... :)
Authored by: applepal on Oct 30, '03 03:18:15PM

thanx!



[ Reply to This | # ]
try this
Authored by: Graff on Oct 30, '03 03:37:28PM

By the way, I believe that you can change this complicated (and possibly slower) code:

      if the name of aFile contains {".jpg"} 
       or the name of aFile contains {".gif"} 
       or the name of aFile contains {".png"} 
       or the name of aFile contains {".pict"} 
       or the name of aFile contains {".tiff"} 
       or the name of aFile contains {".gif"} 
       or the name of aFile contains {".psd"} 
       or the name of aFile contains {".bmp"} then

To this much simpler (and possibly faster) code:

      if the name extension of aFile is in {"jpg", "gif", "png", "pict", 
                                            "tiff", "gif", "psd", "bmp"} 
      then

I'm pretty sure that filtering using a list produces a faster script than using a complicated if...then statement.



[ Reply to This | # ]
try this
Authored by: adrick42 on Oct 30, '03 09:58:49PM

I tried to write something like this but couldn't get it to work.

---
cd ~/ ; %20out



[ Reply to This | # ]
try this
Authored by: Graff on Oct 30, '03 11:35:28PM

I just tested all of the modifications listed above and here is a script that works fine:

on adding folder items to this_folder after receiving added_items
  tell application "Finder"
    set homeFolder to (container of (path to desktop folder) as string)
    set folderLocation to homeFolder & "Pictures:unfiled:"
    repeat with aFile in added_items
      if the name extension of aFile is in 
        {"jpg", "gif", "png", "pict", "tiff", "gif", "psd", "bmp"} then
        tell application "Finder"
          try
            move aFile to folder folderLocation
          on error err
            display dialog err
          end try
        end tell
      end if
    end repeat
    display dialog 
      "Image files have been moved to the Unfiled" & return & 
      "folder in your Pictures folder."
  end tell
end adding folder items to

If this doesn't work for you then make sure that you have run the "Enable Folder Actions" script and have successfully attached a folder action to the folder you want this script to run from. For a good reference on how to get it all to work see Apple's web page on Folder Actions.



[ Reply to This | # ]
try this
Authored by: Shippley on Oct 19, '05 08:23:30PM
I moved the dialoge box telling me that image files had been moved because it was saying it no matter what I added..
on adding folder items to this_folder after receiving added_items
	tell application "Finder"
		set homeFolder to (container of (path to desktop folder) as string)
		set folderLocation to homeFolder & "Pictures:unfiled:"
		repeat with aFile in added_items
			if the name extension of aFile is in 
				{"jpg", "gif", "png", "pict", "tiff", "gif", "psd", "bmp"} then
				tell application "Finder"
					try
						move aFile to folder folderLocation
					on error err
						display dialog err
					end try
					display dialog 
						"Image files have been moved to the Unfiled" & return & 
						"folder in your Pictures folder."
				end tell
			end if
		end repeat
	end tell
end adding folder items to


[ Reply to This | # ]
try this
Authored by: Graff on Oct 31, '03 12:07:49AM

Heh, whups. I thought you meant that you couldn't get my modifications to work. I see now that you were just saying that you had problems with writing something similar to what I used.

The key difference between these this statement:

if the name of aFile contains {".jpg"} then

and this one:

if the name extension of aFile is in {"jpg", "gif", "png"} then

are the keywords contains and is in.

When you use the keyword contains with a list it wants to match ALL of the items in the list (a logical AND), so if you did:

if the name of aFile contains {".jpg", ".gif", ".png"} then

The only thing that would match would be a file named "myfile.jpg.gif.png", or any file with all of those somewhere in it.

The keyword is in gives a match if one or more of the items in the list match (a logical OR). Since you are looking for a .jpg OR a .png OR a .gif and so on, this is what you want to use.

I also used name extension since that strips off all the file name except for the extension (even the dot). You have to do this because is in looks for exactly that item in the list. If I didn't do this then an item named "myfile.jpg" wouldn't be found in the list {"jpg, "png", "gif"}. However just "jpg" would be found in that list.

For a file like myfile.jpg.zip it gets more complicated. Basically then I would use the text item delimiters statement to chunk out all the stuff seperated by dots and then compare to those. You can read about how to do that in this MacCentral AppleScript Primer article, but it's probably not worth doing.



[ Reply to This | # ]
try this
Authored by: jimhoyt on Oct 30, '03 03:29:00PM
The entire first block of "set" statements in the code above can now be replaced by:

set unfiledpictures to ((path to pictures folder as text) & "unfiled:")

From the release notes: "The path to command now has the following new folder constants: applications folder, documents folder, favorites folder, home folder, library folder, movies folder, music folder, pictures folder, public folder, shared documents, shared documents folder, sites folder, utilities folder."

[ Reply to This | # ]
try this
Authored by: Graff on Oct 30, '03 03:44:48PM

You know, I have been wanting a

path to home folder
statement for so long! The statement:
(container of (path to desktop folder)
works but it is a lot more cumbersome and less obvious.

I'm glad they expanded the options that the path command gives you, it really helps out.



[ Reply to This | # ]
Error for me...
Authored by: saltydog4791 on Oct 30, '03 03:00:27PM

I have made the appropriate edits, activated Folder Actions and attached the script and no go. It just behaves like usual, cluttering my desktop. Is anyone else having any luck? I can't get either one to work. I would really love this. By the end of each day my Desktop looks like a warzone.

saltydog4791



[ Reply to This | # ]
Why not use folder actions to organize too?
Authored by: infoblink on Oct 30, '03 05:01:32PM

on adding folder items to this_folder after receiving added_items
	tell application "Finder"
		set folderLocation to (container of (path to desktop folder) as string) & "Pictures:unfiled:"
		
		repeat with aFile in added_items
			if the name of aFile contains {".jpg"} then
				set folderLocation to (container of (path to desktop folder) as string) & "Pictures:JPG:"
			else if the name of aFile contains {".png"} then
				set folderLocation to (container of (path to desktop folder) as string) & "Pictures:PNG:"
			else if the name of aFile contains {".pict"} then
				set folderLocation to (container of (path to desktop folder) as string) & "Pictures:PICT:"
			else if the name of aFile contains {".tiff"} then
				set folderLocation to (container of (path to desktop folder) as string) & "Pictures:TIFF:"
			else if the name of aFile contains {".gif"} then
				set folderLocation to (container of (path to desktop folder) as string) & "Pictures:GIF:"
			else if the name of aFile contains {".psd"} then
				set folderLocation to (container of (path to desktop folder) as string) & "Pictures:PSD:"
			else if the name of aFile contains {".bmp"} then
				set folderLocation to (container of (path to desktop folder) as string) & "Pictures:BMP:"
			else
				set folderLocation to (container of (path to desktop folder) as string) & "Pictures:unfiled:"
			end if
			tell application "Finder"
				try
					
					move aFile to folder folderLocation
					
				on error err
					display dialog err
				end try
			end tell
			display dialog 
				"Image files have been moved to the Unfiled folder in your Pictures folder."
		end repeat
	end tell
end adding folder items to
That organizes the files too.

[ Reply to This | # ]
automating image organization
Authored by: poultryfarm on Oct 30, '03 05:17:39PM
although a bit more complex i use this folder action script to organize my images. i find it extremely useful. hopefully this will help others out as well.
it's titled 'automating file organization' on the macsripters bbs... many thanks to the people on macsripters bbs who helped with the script. check out the full script att the end of the thread here:
http://bbs.applescript.net/viewtopic.php?t=4676&start=15&postdays=0&postorder=asc&highlight=&sid=15763caf2b2d0a6b3516a6a9446b10c5

[ Reply to This | # ]