Download first image of website

Why won’t this script work?

set ChosenFolder to choose folder with prompt "Where do you want the downloaded pictures to be stored?"



tell application "Safari"
	set ImageSource to do JavaScript "document.images[0].src" in document 1
end tell

tell application "URL Access Scripting"
	download ImageSource to ChosenFolder
end tell

I want it to download the first image of the first website to the chosen folder.

I am not a fan of URL Access Scripting so I would recommend using CURL, but in this case download to is expecting a file reference. So for example this works.

set x to path to desktop as string
set y to x & "test.jpg"


tell application "Safari"
	set ImageSource to do JavaScript "document.images[0].src" in document 1
end tell

tell application "URL Access Scripting"
	download ImageSource to file y
end tell

Ideally though you would parse out the image name and use that in your file specifier.