getting downloaded image to image view

ok so i have this to download the image:

to download_items from URL_list to destFolder
	repeat with i in URL_list
		set i to quoted form of i
		set fileName to do shell script "basename " & i
		do shell script "curl -o " & quoted form of (destFolder & fileName) & space & i
	end repeat
end download_items

it works fine, then i use this to get the image and put it in an image view. However i dont know how to reference the image inside the folder bceause i can’t use fileName from the to handler, or it says it’s not defined

on getimage()
	--method to download images
	set t to (do shell script "/usr/bin/curl 'http://www.bungie.net/stats/halo3/CareerStats.aspx?player=" & gt & "&social=true&map=0' | grep 'identityStrip_hypRank' | cut -d '\"' -f 12")
	set imageurl to ("http://www.bungie.net" & t)
	set destFolder to POSIX path of (path to me) & "Contents/Resources/"
	set URL_list to {imageurl}
	download_items from URL_list to destFolder
	set tmpPath to (path to me) & "Contents/Resources/" & fileName
	set rankImage to load image tmpPath
	set image of image view "rankview" of window "main" to rankImage
end getimage

StefanK solved this one, thanks :smiley:

If your going to process only one file anyway,
I recommend to merge the two handlers and use a less expensive way for basename


on getimage()
	--method to download images
	set destFolder to POSIX path of (path to me) & "Contents/Resources/"
	set t to (do shell script "curl 'http://www.bungie.net/stats/halo3/CareerStats.aspx?player=AceHENDO13' | grep --only-matching 'src=\"/images/halo3stats/xp/[^\"]\\+\"' | cut -d \\\" -f 2")
	set imageurl to ("http://www.bungie.net" & t)
	set {TID, text item delimiters} to {text item delimiters, "/"}
	set fileName to last text item of t
	set text item delimiters to TID
	do shell script "curl -o " & quoted form of (destFolder & fileName) & space & quoted form of imageurl
	set rankImage to load image (destFolder & fileName)
	set image of image view "rankview" of window "main" to rankImage
end getimage

Note: Bruce’s way to extract the data is more reliable than mine

thanks:D Nice that I can cut the code down some more :smiley:

Just glancing at the script you have so far, I see a potential problem and inefficiency:

  1. I don’t think it’s a good idea to download images to the application bundle. This might cause many problems, such as when the application is run by someone who doesn’t have write access to the Applications folder (or wherever your app is). It might also be seen as a security risk by system or other software. I think programs like Skype failed to work at one time because they wrote to the application. So I suggest instead downloading to /tmp or to the Application Support folder of the current user.

  2. If I understand correctly, your script could download the same image each time its run. Furthermore, the “load image” command will reload that image into memory multiple times, using multiple memory chunks for the same image. So I suggest only downloading and loading the image if the name of the web file differs from the name of the currently displayed image, and when loading it to set the name of the image to the filename (for checking next time) and deleting the old image from memory.

So, maybe something like this:

on getimage()
	--method to download images
	set destFolder to "/tmp/"
	set t to (do shell script "curl 'http://www.bungie.net/stats/halo3/CareerStats.aspx?player=AceHENDO13' | grep --only-matching 'src=\"/images/halo3stats/xp/[^\"]\\+\"' | cut -d \\\" -f 2")
	set imageurl to ("http://www.bungie.net" & t)
	set {TID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, "/"}
	set fileName to last text item of t
	set AppleScript's text item delimiters to TID
	set oldRankImage to image of image view "rankview" of window "main"
	try -- an error getting the old image means it's not yet been loaded
		set oldRankImageName to name of oldRankImage
		set doLoadNewImage to oldRankImageName is not fileName
	on error
		set oldRankImageName to null
		set doLoadNewImage to true
	end try
	if doLoadNewImage then
		do shell script "curl -o " & quoted form of (destFolder & fileName) & space & quoted form of imageurl
		set rankImage to load image (destFolder & fileName)
		set name of rankImage to fileName -- so that we can check whether it's already loaded later
		set image of image view "rankview" of window "main" to rankImage
		if oldRankImageName is not null then delete oldRankImage
	end if
end getimage

Tom
BareFeet