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

Sort Downloads folder based on file type System
I have found several ways to sort your downloads folder based on the date the file(s) were downloaded, but none that sort it based on the files that are in it, so I decided to write an AppleScript to do just that (although it could easily be used on any folder). This allows me to easily find files even if I can't remember when I downloaded them (or even their names).

Here's the code; you'll need to edit the top section to reflect the proper path to your Downloads folder.
(*Downloads folder sorter – an applescript designed to help organise your downloads folder*)

-- If your downloads folder is in your user's home folder, comment out the first line and uncomment the second.
set downloadsFolder to ((path to desktop) & "Downloads" as string)
-- set downloadsFolder to ((path to home folder) & "Downloads" as string)

-- the list of folders that your files/folders will be sorted into, you may edit the names, but adding or removing items will require further customisation later on, this will be indicated
set foldersList to {"Applications", "Archives", "Folders", "Other"}

--these are the lists of extensions that correspond to the folder names above, N.b. for folders use "Folders" and for any file type (i.e. other) leave the list blank. If you have added or removed items from the list above you must also add or remove items from here
set extList1 to {"app"}
set extList2 to {"zip", "dmg", "sit", "hqx", "tar", "gz", "pkg", "bin", "sitx", "rar"}
set extList3 to {"Folders"}
set extList4 to {}
-------------------------------------------------------------------------------

set finalFolders to {}
set includeFolders to false
tell application "Finder"
  
  -------------------------------------------------------------------------------
  --This section creates the folders (if they are not already there) so, again, if you have added or removed items from the foldersList above you must also add or remove items from here
  if not (alias (downloadsFolder & ":" & (item 1 of foldersList) & ":") exists) then
    make new folder at (alias downloadsFolder) with properties {name:(item 1 of foldersList)}
  end if
  if not (alias (downloadsFolder & ":" & (item 2 of foldersList) & ":") exists) then
    make new folder at (alias downloadsFolder) with properties {name:(item 2 of foldersList)}
  end if
  if not (alias (downloadsFolder & ":" & (item 3 of foldersList) & ":") exists) then
    make new folder at (alias downloadsFolder) with properties {name:(item 3 of foldersList)}
  end if
  if not (alias (downloadsFolder & ":" & (item 4 of foldersList) & ":") exists) then
    make new folder at (alias downloadsFolder) with properties {name:(item 4 of foldersList)}
  end if
  -------------------------------------------------------------------------------
  
  try
    set allFiles to every file in alias downloadsFolder whose name does not end with ".part" and name does not end with ".download" and name does not end with ".savedSearch" and size is not 0
  on error
    set allFiles to {}
  end try
  
  -------------------------------------------------------------------------------
  --This section determines where to move any other folders in the downloads folder, so, again, if you have added or removed items from the foldersList above you must also add or remove items from here
  if (extList1 contains "Folders") or (extList2 contains "Folders") or (extList3 contains "Folders") or (extList4 contains "Folders") then
    if (extList1 contains "Folders") then
      set foldersFolder to (downloadsFolder & ":" & (item 1 of foldersList) as string)
    else if (extList2 contains "Folders") then
      set foldersFolder to (downloadsFolder & ":" & (item 2 of foldersList) as string)
    else if (extList3 contains "Folders") then
      set foldersFolder to (downloadsFolder & ":" & (item 3 of foldersList) as string)
    else if (extList4 contains "Folders") then
      set foldersFolder to (downloadsFolder & ":" & (item 4 of foldersList) as string)
    end if
    ---------------------------------
    try
      set allFolders to every folder in alias downloadsFolder
      set allFoldernames to the name of every folder in alias downloadsFolder
    on error
      set allFolders to {}
    end try
    
    repeat with i from 1 to (count allFolders)
      if item i of allFoldernames is not in foldersList then
        set end of finalFolders to (item i of allFolders)
      end if
    end repeat
    if (count finalFolders) is not equal to 0 then
      repeat with i from 1 to count finalFolders
        move (item i of finalFolders) to alias foldersFolder
      end repeat
    end if
  end if
  -------------------------------------------------------------------------------
  
  if (count allFiles) is not equal to 0 then
    --This section determines whether or not to include an "other" folder, so, again, if you have added or removed items from the foldersList above you must also add or remove items from here
    if extList1 is equal to {} then
      set useOtherFolder to true
      set otherFolder to (downloadsFolder & ":" & (item 1 of foldersList) as string)
    else if extList2 is equal to {} then
      set useOtherFolder to true
      set otherFolder to (downloadsFolder & ":" & (item 2 of foldersList) as string)
    else if extList3 is equal to {} then
      set useOtherFolder to true
      set otherFolder to (downloadsFolder & ":" & (item 3 of foldersList) as string)
    else if extList4 is equal to {} then
      set useOtherFolder to true
      set otherFolder to (downloadsFolder & ":" & (item 4 of foldersList) as string)
    end if
    ---------------------------------
    set myPath to (path to me as string)
    set myPath to reverse of (characters of myPath) as string
    set dlm to AppleScript's text item delimiters
    set AppleScript's text item delimiters to ":"
    set myName to (text item 1 of myPath)
    set AppleScript's text item delimiters to dlm
    set myName to reverse of (characters of myName) as string
    ---------------------------------
    repeat with i from 1 to (count allFiles)
      if (name of (item i of allFiles)) is not myName then
        set theFileInfo to info for ((item i of allFiles) as alias)
        set ext to name extension of theFileInfo as string
        --This section determines where to move the files, so, again, if you have added or removed items from the foldersList above you must also add or remove items from here
        if ext is in extList1 then
          move (item i of allFiles) to alias (downloadsFolder & ":" & (item 1 of foldersList) as string)
        else if ext is in extList2 then
          move (item i of allFiles) to alias (downloadsFolder & ":" & (item 2 of foldersList) as string)
        else if ext is in extList3 then
          move (item i of allFiles) to alias (downloadsFolder & ":" & (item 3 of foldersList) as string)
        else if ext is in extList4 then
          move (item i of allFiles) to alias (downloadsFolder & ":" & (item 4 of foldersList) as string)
        else if useOtherFolder is true then
          move (item i of allFiles) to alias otherFolder
        end if
      end if
    end repeat
  end if
end tell
I hope this helps someone else!

[robg adds: I tried this in 10.6, and it failed (error number -1700 from 0 to type class on the first set allFiles to... line. The author is anonymous, though, so I can't contact them for assistance. As the script seems useful, I thought I'd post it and ask for help -- AppleScript wizards, please let us know if you see a fix. I'll watch the comments and update the hint if a fix is found. Alternatively, if it works for you as-is, please post that as well; perhaps my system is unique in its failure.]
    •    
  • Currently 2.44 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (9 votes cast)
 
[9,602 views]  

Sort Downloads folder based on file type | 6 comments | Create New Account
Click here to return to the 'Sort Downloads folder based on file type' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Sort Downloads folder based on file type
Authored by: foresmac on Oct 07, '09 07:55:57AM

Is there any reason you couldn't just add the "kind" column in list view and sort it that way? That's what I do.

In icon view, you can do View > Keep Arranged By > Kind. There's even a keyboard short cut for that: ctrl-opt-cmd-5.



[ Reply to This | # ]
Sort Downloads folder based on file type
Authored by: Dr. T on Oct 07, '09 10:28:57AM

I also added a "Kind" column, though I don't really need to. I rarely have more than a dozen items in my Downloads folder. When I need to download numerous files of the same type (images, for example), I use my browser to create a new (temporary) download folder in the most useful location.

I've never understood people (such as my daughter) who leave hundreds of files in their Downloads folder. In my opinion, useful items should get filed appropriately (installers, documents, pictures, music, etc,) soon after downloading. Nonuseful items should get trashed. (My daughter "fixed" her Downloads folder clutter: she created a desktop folder called "Yet to be filed." Her approach is like cleaning up a messy room by throwing out trash, tossing most things into the closet, and leaving a few things on the floor.)



[ Reply to This | # ]
Sort Downloads folder based on file type
Authored by: freaktheclown on Oct 07, '09 08:15:56AM

There's an even easier way than this: Smart Folders.

I made a Downloads folder in my user Library folder and set Safari to download everything there.

Then I made smart folders (All, Archives, Documents, Images, Music, etc.) by searching that downloads folder, and simply saved them to the Download folder in my home directory.

It's a lot cleaner, it updates automatically, and it's a lot more customizable -- you can easily make smart folders for ANY criteria you want.



[ Reply to This | # ]
Sort Downloads folder based on file type
Authored by: Michelasso on Oct 14, '09 07:30:35AM

Could you be more specific? I tried to make a smart folder but it lists all files in the subdirectories as well! I just want so sort the files using the modification date and NOT the creation date. This thing abut OSX is retarded. very confusing.



[ Reply to This | # ]
Sort Downloads folder based on file type
Authored by: bhas on Oct 07, '09 08:31:10AM

It worked for me (no error messages)
I don't call this sorting. The script moves files to designated folders.

I prefer the smart folder solution suggested by freaktheclown



[ Reply to This | # ]
Sort Downloads folder based on file type
Authored by: Andrew J Freyer on Oct 07, '09 10:09:56AM
Look into Hazel - of www.noodlesoft.com

By far the most essential software on my computer. Create rules as you would for smart folders, but files are actually moved instead of aliased.

One sentence cannot do justice - Hazel is seriously powerful software. Look into it.

[ Reply to This | # ]