Problems creating web file downloader

Hi
I haven’t used AppleScript in a couple of years so am really rusty. What I’m trying to do it create a script that downloads PDFs from a web site. It bascially starts at number x and then counts down to 0, downloading any PDF with the name x. I can get all that to work fine - the problem I have is that I want it to automatically save the file rather than having to click the button each time to save it (there are a couple of hundred thousand PDFS to download). The code I have done so far is:


tell application "Finder"
	set theDestinationFolder to ((path to desktop) & "downloads") as string
	if exists theDestinationFolder then
	else
		make new folder at (path to desktop) with properties {name:"downloads" as string}
	end if
	display dialog "Start at number? " default answer ("173184") buttons {"Cancel", "Go"} default button 2
	set startvalue to text returned of the result
	
	repeat with theIncrementValue from startvalue to 1 by -1
		set theFileNumber to theIncrementValue as string
		set theURL to "http://www.example.com/?judgmentID=" & theFileNumber
		set theFileName to text -((offset of "/" in (reverse of characters of theURL) as text) - 13) thru -1 of theURL & ".pdf"
		set theFile to (choose file name default name theFileName default location (theDestinationFolder as alias))
		do shell script "/usr/bin/curl " & theURL & " -o " & quoted form of POSIX path of theFile
	end repeat
end tell

The line I’m having problems with is:
set theFile to (choose file name default name theFileName default location (theDestinationFolder as alias))

How do I get it to automatically save each file into the folder theDestinationFolder?
Thanks in advance!!

You can just construct the path in a string.

This is what I should do:

-- get download folder
set destinationFolder to (path to desktop as text) & "downloads"
if not itemExistsAtPath(destinationFolder) then mkdir(destinationFolder)

-- get start number
set startValue to displayDialogForNumber("Start at number?", "173184") as integer

-- process
repeat with i from startValue to 1 by -1
	set myURL to "http://www.example.com/?judgmentID=" & i
	set fileName to (i as text) & ".pdf"
	set destFile to (alias destinationFolder as text) & fileName
	do shell script "/usr/bin/curl " & quoted form of myURL & " -o " & quoted form of POSIX path of destFile
end repeat

(* ===== HANDLERS ===== *)
on itemExistsAtPath(aHSF)
	set aHSF to aHSF as text
	try
		aHSF as alias
		return true
	on error
		return false
	end try
end itemExistsAtPath

on mkdir(aHSF)
	do shell script "mkdir " & quoted form of (POSIX path of aHSF)
end mkdir

on displayDialogForNumber(prmpt, defansw)
	set myValue to text returned of (display dialog prmpt default answer defansw)
	try
		set myValue to myValue as number
	on error
		set myValue to displayDialogForNumber("Not a valid number", defansw)
	end try
	return myValue
end displayDialogForNumber

hope it helps,
ief2

Brilliant!! Thanks for that - the itemExistsAtPath handler didn’t work for me but I transferred in my if exists bit and it worked perfectly.
Thanks for your help!!

Strange, it does work for me.

Worked fine as is when I tried it again
thanks again