pause script while safari downloads a file - and test complete

Hi,

This forum is great and I have received much help from it’s members. I have a script that downloads an .ics file from a website. Due to differing internet connection speeds and network traffic on the server the time for the download can vary wildly. I am forced to use an arbitrary delay of 45. In the interest of keeping my script efficient I have been code-mining for a way to monitor the downloads folder and return to the script once the correct file has been completely downloaded.

I have tried curl but the file is hidden behind a password protected, re-directed website and I can’t seem to make curl work.

Here is what I have at the moment, I have changed the website and file downloads for security reasons. Unfortunately, the two different file sizes for testing (1GB and 5MB) don’t have the same creation date properties as my .ics file. This means this example code will always rerun false. Needless to say, my if/then statement in the “dropList” subroutine works in my script because the creation date of the downloaded .ics file is always the system date of my computer.


-- To benchmark against theFileDate query in the dropList subroutine.
set theSystemDate to (current date) - 2 * minutes

-- A selection of two arbitrary files, one large and one small to allow the delay time I have to set to stop the script for the large file but let the small file download.
set downloadURL to {"http://download.thinkbroadband.com/1GB.zip", "http://download.thinkbroadband.com/5MB.zip"}

set downloadURL to choose from list (downloadURL) with title "Roster Script" with prompt "Please select the File size you wish to download.." OK button name "Select" with empty selection allowed

tell application "Safari"
	activate
	open location downloadURL
end tell

--This delay is UGLY! I need it to delay the script long enough for the file to download. This is where I need a more suitable, flexible option.
delay 45

--This points the script to the default downloads folder and determines the most recent file in the folder.
set theFolder to POSIX path of (path to downloads folder)
set theFile to first item of (every paragraph of (do shell script "ls -t " & theFolder)) as text
set theFile to theFolder & theFile

dropList(theFile, theSystemDate) --This runs my subroutine which is meant to confirm the file "kind" and "creation date".. In my actual script this works on "kind"- "iCal file" and the "creation date" is the actual system date the file is added to the downloads folder.

set theFileReference to open for access theFile
set theFileContents to read theFileReference
close access theFileReference

-- As stated above, when this subroutine is run in my actual script the creation date is greater than theSystemDate which confirms the file has downloaded.
on dropList(theFile, theSystemDate)
	
	set typeF to info for theFile without size
	set theFileKind to kind of typeF
	set theFileDate to creation date of typeF --unfortunately, the creation date of the two files above aren't set as the systemdate as they are added, so this script will never return true... but you get the idea, right?
	
	if theFileKind is equal to "ZIP archive" and theFileDate is greater than theSystemDate then
		return true
	else
		display alert "Your file is unavailable." message "Please try again." buttons {"Cancel"} cancel button 1
		
	end if
end dropList


If anyone has a way of monitoring the downloads stack ( I know it bounces so at a system level something must be watching it) I don’t want to use Folder Actions because this script is being written for use by many people and I would like to limit the amount of extra “setup” required on a users part.

Thank you in advance.

Kev

Kev,

We seem to have similar problems at the same time. See my thread “Display Alert giving up after - Problem” I think my code will work for you. That is if you know the name of the file you are looking for. My script as written looks for a folder name, but it can be easily modified to look for a file name instead.

Rich

Hi Rich,

Your script is exactly what I need… is your first post an updated version of your script? if not, would you mind posting the final version online for me to use.

Thanks for your suggestion.

Kev