Sort Downloads folder based on file type

Oct 07, '09 07:30:01AM

Contributed by: Anonymous

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

Comments (6)


Mac OS X Hints
http://hints.macworld.com/article.php?story=20090915142918794