Sort Downloads

This is a script I’ve had for a while. It was originally broken up into a few scripts, but recently, I decided to create one script which would handle it all. This script takes all the files in my Downloads folder and moves each file type to their appropriate folder.

The wallpaper part may not make sense, but let me explain. I enjoy collection desktop wallpapers, but I hand-pick them by downloading them to my Downloads folder first. And if the wallpaper already exists in my collection, the script will rename the new one and keep both.

Feel free to take this script and modify it to suit your needs. The script should automatically detect your startup disk and home folder.

-- current user path
tell application "Finder" to set startupDisk to startup disk as text
set currentUser to (short user name of (system info))


-- sort handler
on sortDownloads(theExt, moveTo)
	-- current user path
	tell application "Finder" to set startupDisk to startup disk as text
	set currentUser to (short user name of (system info))
	try
		tell application "Finder"
			set downloads to get every file of DLfolder whose name contains theExt
			set moveDLs to move downloads to folder ("" & startupDisk & "Users:" & currentUser & ":" & moveTo & "") with replacing
		end tell
	end try
end sortDownloads

sortDownloads(".dmg", "Downloads:Saved Downloads:")
sortDownloads(".pkg", "Downloads:Saved Downloads:")
sortDownloads("docksd3", "Downloads:Saved Downloads:Dock Downloads:")
sortDownloads(".zip", "Downloads:Saved Downloads:")

-- sort wallpapers
try
	tell application "Finder"
		set DLfolder to folder ("" & startupDisk & "Users:" & currentUser & ":Downloads:")
		set originalWP to files of DLfolder whose name extension is "jpg"
		set moveWP to move originalWP to folder ("" & startupDisk & "Users:" & currentUser & ":Pictures:Wallpaper:")
	end tell
	-- if wallpaper exists
on error number -15267
	tell application "Finder"
		repeat until (files of DLfolder whose name extension is "jpg") = {}
			repeat with itemNum from 1 to (number of (files of DLfolder whose name extension is "jpg"))
				set originalName to name of (item itemNum of originalWP as alias)
				set nameOffset to offset of "." in originalName
				set justName to characters beginning through (nameOffset - 1) of originalName as string
				set newName to (justName & " II" & ".jpg")
				set name of (item itemNum of originalWP as alias) to (newName) as text
				set newWP to ("" & startupDisk & "Users:" & currentUser & ":Downloads:") & newName & ""
				set finalWP to move newWP to folder ("" & startupDisk & "Users:" & currentUser & ":Pictures:Wallpaper:") with replacing
			end repeat
		end repeat
	end tell
end try

sortDownloads(".doc", "Documents:")