Download an image from a web page

Hi,

welcome to MacScripter

alitaliano <> temporaire ? Do you come from valle d’aosta ? ;):wink:

There is no need for GUI scripting. Since Safari 4 tabs are easily scriptable


set destFolder to POSIX path of (path to pictures folder as Unicode text) & "TEMPORAIRE:"
set {TID, text item delimiters} to {text item delimiters, "/"}
tell application "Safari"
	repeat with aTab from 1 to (count tabs of window 1)
		tell window 1 to set current tab to tab aTab -- go to next tab
		set numberOfPictures to do JavaScript "document.images.length" in document 1 -- document 1 is always the visible tab
		repeat with aPicture from 1 to numberOfPictures
			set picURL to do JavaScript "document.images[" & ((aPicture - 1) as string) & "].src" in document 1
			if picURL ends with "jpg" then
				set fName to last text item of picURL
				do shell script "curl -o " & quoted form of (destFolder & fName) & space & picURL
			end if
		end repeat
	end repeat
end tell
set text item delimiters to TID

Note: Without any error handling images with same file name will overwrite existing ones

Hey StefanK, the legend ! :stuck_out_tongue:
No, I’m french :rolleyes:
Thank you so much for your reply !
But something is still wrong : the pictures save in “pictures folder” but not in the “Temporaire” folder.
I tried to add the possibility to close tab whent it’s been downloaded without result.
Can you help me again ? Thank you !

set destFolder to POSIX path of ((path to desktop as Unicode text) & "TEMPORAIRE:")
set {TID, text item delimiters} to {text item delimiters, "/"}
tell application "Safari"
	repeat with aTab from 1 to (count tabs of window 1)
		tell window 1 to set current tab to tab aTab -- go to next tab
		set numberOfPictures to do JavaScript "document.images.length" in document 1 -- document 1 is always the visible tab
		repeat with aPicture from 1 to numberOfPictures
			set picURL to do JavaScript "document.images[" & ((aPicture - 1) as string) & "].src" in document 1
			if picURL ends with "jpg" then
				set fName to last text item of picURL
				do shell script "curl -o " & quoted form of (destFolder & fName) & space & picURL
			end if
		end repeat
	end repeat
end tell
set text item delimiters to TID

sorry, as we deal with POSIX paths the last character must be a slash.
The script closes every tab which doesn’t contain any images


set destFolder to POSIX path of (path to pictures folder as Unicode text) & "TEMPORAIRE/"
set {TID, text item delimiters} to {text item delimiters, "/"}
tell application "Safari"
	repeat with aTab from 1 to (count tabs of window 1)
		tell window 1 to set current tab to tab aTab -- go to next tab
		set numberOfPictures to do JavaScript "document.images.length" in document 1 -- document 1 is always the visible tab
		if numberOfPictures = 0 then tell window 1 to close current tab
		repeat with aPicture from 1 to numberOfPictures
			set picURL to do JavaScript "document.images[" & ((aPicture - 1) as string) & "].src" in document 1
			if picURL ends with "jpg" then
				set fName to last text item of picURL
				do shell script "curl -o " & quoted form of (destFolder & fName) & space & picURL
			end if
		end repeat
	end repeat
end tell
set text item delimiters to TID


I get an error, could you tell me why ?
Thanks again for your help ! :cool:

try


.
do shell script "curl -o " & quoted form of (destFolder & fName) & space & quoted form of picURL
.

Nope, it doesn’t work and I have the same error.
Pictures still save in the “pictures folder” in home though.
If you have time and patience to tell me why…
Anyways thank you so much :wink: to pay attention for a newbie like me !!

Of course the script assumes that the folder TEMPORAIRE exists in your pictures folder

:stuck_out_tongue: Ok it works, I’m a little bit stupid… The folder name was “*TEMPORAIRE” and not just “TEMPORAIRE”…

:rolleyes: :rolleyes:

The script DOES work.
Thank you again, and see you !!