Extract all Webpage's Images & Save to the Desktop's folder


use AppleScript version "2.4"
use scripting additions
use framework "Foundation"
property destinationFolderName : "ExtractedImages"
property destinationFolderPath : ((path to desktop folder) as text) & destinationFolderName

tell application "Finder"
	if folder destinationFolderPath exists then
		set destinationFolder to destinationFolderPath as alias
		tell me to set theResult to display dialog "Folder \"ExtractedImages\" exists on the desktop. 
		Do you want delete old images or Backup Them?" buttons {"CANCEL", "DELETE", "BACKUP"}
		set theAnswer to button returned of theResult
		if theAnswer is "Cancel" then
			tell me to display notification "USER CANCELLED." & linefeed & "QUITTING THE APPLICATION." with title "Extract Webpage Images.app"
			return
		else if theAnswer is "DELETE" then
			delete items of destinationFolder
			tell me to display notification "Folder \"ExtractedImages\" CLEANED"
		else
			set backupFolder to choose folder with prompt "CHOOSE FOLDER TO BACKUP OLD IMAGES TO IT"
			move items of destinationFolder to backupFolder
			tell me to display notification "OLD IMAGES BACKUPED." & linefeed & (backupFolder as text) with title "Extract Webpage Images.app"
			delay 5
			tell me to display notification "Folder \"ExtractedImages\" CLEANED" with title "Extract Webpage Images.app"
		end if
	else
		-- make destination folder on the desktop
		make new folder at desktop with properties {name:destinationFolderName}
		tell me to display notification "FOLDER \"ExtractedImages\" CREATED on the desktop." with title "Extract Webpage Images.app"
	end if
end tell

tell application "Safari" to activate
-- extract image URLs from webpage
set imageURLsList to extractImagesURLs() of me
if imageURLsList is {} then return

-- save extracted images to folder "ExtractedImages" of the desktop
tell me to display notification "EXTRACTING THE IMAGES BEGUN" with title "Extract Webpage Images.app"
repeat with thePicURL in imageURLsList
	try
		set NSPicURL to (current application's |NSURL|'s URLWithString:thePicURL)
		set picData to (current application's NSData's dataWithContentsOfURL:NSPicURL)
		set theFileName to (NSPicURL's lastPathComponent()) as Unicode text
		set thePath to (POSIX path of (destinationFolderPath & ":" & theFileName))
		(picData's writeToFile:thePath atomically:true)
	end try
end repeat
tell me to display notification "DONE!" & linefeed & "SEE RESULT in the FOLDER \"ExtractedImages\" of the desktop" with title "Extract Webpage Images.app"

--==================================== Handlers ======================================
on extractImagesURLs()
	set extractImages to "

var z = '';
for (i = 0; i < top.document.images.length; i++) processImg(top.document.images[i].src);
for (i = 0; i < top.frames.length; i++) mf(top.frames[i]);
z = z.split('%%%').join('');
z

function mf(obj){
	if (obj.frames.length == 0) { // extract images from this page
		try {
			obj.document.images; throw 'OK';
		} catch (e) {
			if (e=='OK') for (q=0;q<obj.document.images.length;q++) processImg(obj.document.images[q].src);
		}
	} else { // rotate again
		for (q=0;q<obj.frames.length;q++) mf(obj.frames[q]);
	}
}

function processImg(img){
	petabyte = '\\r' + img + '%%%';
	if (z.indexOf(petabyte) == -1) z += petabyte;
}

"
	
	-- tell application "Safari" to rest of paragraphs of (do JavaScript extractImages in document 1)
	tell application "Safari" to (do JavaScript extractImages in document 1)
	if ((count result) is 0) then
		return {}
	else
		return rest of paragraphs of result
	end if
end extractImagesURLs