Help Downloading Image from a site w/out Using Browser

ok I’ve been searching around in here and I’m kind of a newb w/ scripting. I’ve found some helpful bits, but nothing yet that does what I’m trying to do.
I’ve been trying to hack together a script that will download an image containing specific key words from a page, w/out having to open up a web browser. I’ve got this so far:


set destFolder to POSIX path of ((path to desktop as Unicode text) & "News:")

tell application "Safari" to set numberOfPictures to do JavaScript "document.images.length" in document 1
repeat with i from 1 to numberOfPictures
	tell application "Safari" to set picURL to do JavaScript "document.images[" & ((i - 1) as string) & "].src" in document 1
	if picURL contains "CA_SFE" then
		set {TID, text item delimiters} to {text item delimiters, "/"}
		set fName to last text item of picURL
		set text item delimiters to TID
		do shell script "curl -o " & quoted form of (destFolder & fName) & space & picURL
	end if
end repeat

This will does exactly what I want, except this requiures the page to be open in the browser. I’d like to not have to open up anything.

Thanks in advance,

Mat

Hi Mat,

welcome to MacScripter.

Without knowing the exact URL it’s hard to offer a working solution,
but you can filter the line of the link with

do shell script "curl http://www.myDomain.com | grep CA_SFE"

then it might be quite easy to extract the URL and download the image

Hey Stefan,

Thanks for getting back to me.

I actually have several urls I’m hitting up and grabbing images from.

here are a couple examples:
http://www.newseum.org/todaysfrontpages/hr.asp?fpVname=CA_SFE&ref_pge=gal&b_pge=1
http://www.newseum.org/todaysfrontpages/hr.asp?fpVname=CA_LAT&ref_pge=gal&b_pge=1
http://www.newseum.org/todaysfrontpages/hr.asp?fpVname=CA_SDUT&ref_pge=gal&b_pge=1

so what I want to do is have it download the image contained in each page, i.e. CA_SFE in the first link, CA_LAT in the second, and CA_SDUT in the third. (they’re all .jpgs) The way i have it set up now is just repeating the script over and over and over changing out the (if picURL contains “CA_SFE” then) part. Maybe you know a cleaner way to do this.

Also, where would i stick that line of code you mentioned?

Thanks,

Mat

unfortunately this site has dynamic contents using javascript,
as far as I know it’s not possible to get the data with curl

sooo…there’s no way to get the images w/out opening the page?

here’s what I’ve got, and this works…just would rather not have it open the page every time.


tell application "Safari" to open location "http://www.newseum.org/todaysfrontpages/hr.asp?fpVname=CA_SFE&ref_pge=gal&b_pge=1"
delay 1
set destFolder to POSIX path of ((path to desktop as Unicode text) & "News:")

tell application "Safari" to set numberOfPictures to do JavaScript "document.images.length" in document 1
repeat with i from 1 to numberOfPictures
	tell application "Safari" to set picURL to do JavaScript "document.images[" & ((i - 1) as string) & "].src" in document 1
	if picURL contains "CA_SFE" then
		set {TID, text item delimiters} to {text item delimiters, "/"}
		set fName to last text item of picURL
		set text item delimiters to TID
		do shell script "curl -o " & quoted form of (destFolder & fName) & space & picURL
	end if
end repeat
tell application "Safari"
	close document 1
end tell

---------------

tell application "Safari" to open location "http://www.newseum.org/todaysfrontpages/hr.asp?fpVname=CA_DN&ref_pge=gal&b_pge=1"
delay 1
set destFolder to POSIX path of ((path to desktop as Unicode text) & "News:")

tell application "Safari" to set numberOfPictures to do JavaScript "document.images.length" in document 1
repeat with i from 1 to numberOfPictures
	tell application "Safari" to set picURL to do JavaScript "document.images[" & ((i - 1) as string) & "].src" in document 1
	if picURL contains "CA_DN" then
		set {TID, text item delimiters} to {text item delimiters, "/"}
		set fName to last text item of picURL
		set text item delimiters to TID
		do shell script "curl -o " & quoted form of (destFolder & fName) & space & picURL
	end if
end repeat
tell application "Safari"
	close document 1
end tell

----------

tell application "Safari" to open location "http://www.newseum.org/todaysfrontpages/hr.asp?fpVname=CA_LAT&ref_pge=gal&b_pge=1"
delay 1
set destFolder to POSIX path of ((path to desktop as Unicode text) & "News:")

tell application "Safari" to set numberOfPictures to do JavaScript "document.images.length" in document 1
repeat with i from 1 to numberOfPictures
	tell application "Safari" to set picURL to do JavaScript "document.images[" & ((i - 1) as string) & "].src" in document 1
	if picURL contains "CA_LAT" then
		set {TID, text item delimiters} to {text item delimiters, "/"}
		set fName to last text item of picURL
		set text item delimiters to TID
		do shell script "curl -o " & quoted form of (destFolder & fName) & space & picURL
	end if
end repeat
tell application "Safari"
	close document 1
end tell



as you see, its basically the same script repeating over and over w/ different pages. this is just a section, it does this 34 times.

Thanks
Mat