Download Webpage Linked Files to specific folder (selectively)

I wrote here for myself (and other users) a simple script - to download files from web page links. It also works with webarchives stored on your own drive.

The script downloads the files you specified in the list of extensions. Also, note that the script downloads links starting with “http”. For users who only want to download files from “safe” links: simply replace the string “http” with "https:" in the on findURLsIn:theString handler.

Acknowledgments:
The main parts of the script were written in the past by users @Shane Stanley and @StefanK to solve various problems. I myself color=blue[/color] only own the implementation of the idea of combining their experience into this useful script.


use AppleScript version "2.4" -- Yosemite (10.10) or later
use framework "Foundation"
use scripting additions


-- edit settings here
set extensionsList to {"txt", "php", "js", "doc", "jpg", "gif", "png", "tif", "bmp", "zip", "dmg", "pdf", "svg"}
set aURL to "https://www.macscripter.net/viewtopic.php?id=22987"
set destinationFolder to POSIX path of (choose folder)

-- my script works with webarchives stored on local disk as well
-- following commented URL is for one of webarchives, stored on MY local disk
-- set aURL to "file:///Users/123/MacScripter%20Topics/21288-47983/22987.%20Firefox%20or%20Safari:%20Download%20All%20Linked%20Files%20To%20Specific%20Folder%20.webarchive"


-- download files from webpage (selectively)
my downloadFilesSelectively(aURL, destinationFolder, extensionsList)


--------------------------------------------------  HANDLERS  ------------------------------------------------------
on downloadFilesSelectively(aURL, destinationFolder, extensionsList)
	set HTMLContents to do shell script ("curl " & quoted form of aURL)
	set theURLs to my findURLsIn:HTMLContents
	set errorList to ""
	repeat with this_link in theURLs
		set fName to my getDocumentName(contents of this_link)
		set needToDownload to false
		repeat with anExtension in extensionsList
			if fName ends with ("." & anExtension) then
				try
					do shell script "curl -L -o " & quoted form of (destinationFolder & fName) & space & this_link
				on error
					set errorList to errorList & fName & return
				end try
				exit repeat
			end if
		end repeat
	end repeat
	if not (errorList is "") then display dialog errorList
end downloadFilesSelectively

on findURLsIn:theString
	set theString to current application's NSString's stringWithString:theString
	set theDD to current application's NSDataDetector's dataDetectorWithTypes:(current application's NSTextCheckingTypeLink) |error|:(missing value)
	set theURLs to theDD's matchesInString:theString options:0 range:{0, theString's |length|()}
	set thePred to current application's NSPredicate's predicateWithFormat:"SELF BEGINSWITH 'http'"
	set newArray to (theURLs's valueForKeyPath:"URL.absoluteString")'s filteredArrayUsingPredicate:thePred
	return newArray as list
end findURLsIn:

on getDocumentName(this_link)
	set {ATID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, "/"}
	set fName to last text item of this_link
	set AppleScript's text item delimiters to "%20"
	set fName to text items of fName
	set AppleScript's text item delimiters to space
	set fName to fName as text
	set AppleScript's text item delimiters to ATID
	return fName
end getDocumentName