Alert you when your downloads have completed

Note: I made a bunch of optimizations in the below script and this is the latest version. The optimizations were based on some ideas from this website and some new things I learned. Every section of the script has changed… for the better I hope. Also, this script will now watch folders as well as files which the original script couldn’t handle.

I often have large files downloading and wanted a way for applescript to watch my downloads folder for me and tell me when they’re finished. So here’s a script that does just that. The script looks into the Downloads folder and calculates the most recent item (a file or folder). It knows this by checking the mod date of the items. It will watch that item for file size changes over time. When the item size stops changing it knows the item has stopped downloading. When that item is finished downloading it will again check the Downloads folder for the newest item. If there is a different “newest” item then the script knows that there are other items still being downloaded so it then monitors that item for size changes. The script will continue doing this until no item size changes are taking place, therefore the script knows that no more downloads are taking place. When no more downloads are taking place the script notifies you that your downloads are done by beeping twice and displaying a dialog notification. The dialog is set to auto-close after 10 seconds.

The 3 variables you can set are at the top of the script. 1) “downloadFolder” is the path to the folder you want to watch. 2) “time_delay” is the time in seconds your download folder is checked for changing item sizes. 3) “giveUp” is the time in seconds before the dialog boxes automatically go away without your input.


global time_delay

-- variables to change
set downloadFolder to "TimeMachine:Downloads:" -- the path to the folder you want to monitor, usually your Downloads folder
set time_delay to 3 -- the time in seconds of how often your downloads folder is checked if the files have finished downloading
set giveUp to 10 -- the time in seconds before the dialog boxes automatically go away

-- variables not to change
set newestItem to ""
set anItem to ""

repeat
	set newestItem to my findNewestItem(downloadFolder) -- find the newest file in the downloads folder
	if newestItem is not anItem then
		my checkStableSize(newestItem) -- watch the newest file and finish when the file size does not change any more
		delay 1
		set anItem to newestItem
	else
		exit repeat
	end if
end repeat
activate
beep 3
display dialog "The downloads are complete!" giving up after giveUp
--quit

----------------------------------- subroutines ------------------------------------
---------------------------------------------------------------------------------------
on findNewestItem(thefolder) -- find the newest file in the (folderPath) as measured by the modification date
	set thefolder to POSIX path of thefolder
	set newestItem to thefolder & (do shell script "ls -tF " & quoted form of thefolder & " |  grep -v -e'[%|]$' | head -n1")
	try
		set result to ((newestItem as POSIX file) as string)
	on error
		set result to (((text 1 thru -2 of newestItem as string) as POSIX file) as string)
	end try
end findNewestItem

on checkStableSize(theItem) -- loop until the size of (theItem) stops changing over time
	set F to quoted form of POSIX path of theItem
	set sizeThen to first word of (do shell script "du -d 0 " & F) as integer --get initial size
	repeat --if they don't equal then loop until they do
		try
			do shell script "sleep  " & time_delay
			set sizeNow to first word of (do shell script "du -d 0 " & F) as integer -- get new size
			if sizeNow - sizeThen = 0 then exit repeat
			set sizeThen to sizeNow
		on error
			exit repeat
		end try
	end repeat
end checkStableSize

did you know growl did this ?

If I used growl then how would I learn applescript?

Model: PM dual 2.0 GHz G5
AppleScript: AppleScript 1.10.7
Browser: Safari 419.3
Operating System: Mac OS X (10.4)