Deals with name conflicts, e.g. home/image.gif and book/image.gif one will be renamed to image1.gif. Deals with the problem of 31 characters or more. Nicely Store them in a Chest Folder in The Site Domain Name and then a time stamped folder.
You can set the extensions you support or want just change ExtensionList.
I have a more advanced one that you can choose above or below area size, choose from list extensions you want, goto folder once download is complete or continue surfing. Put URL in spotlight comment, and page URL on the time stamp folder so in the future you know where they come from. Email me on left If you want that.
property TheURL : "http://www.applescript.co.nz/"
property SuffixOptions : {".com", ".net", ".org", ".info", ".us", ".biz", ".tv", ".mobi", ".cc", ".ws", ".bz", ".tc", ".vg", ".ms", ".gs", ".name", ".co.uk", ".de", ".be", ".eu", ".at", ".com.mx", "org.uk", ".me.uk", ".co.nz", ".net.nz", ".org.nz", ".cn", ".tw"}
set ImageList to {}
set NameList to {}
set UniqueList to {}
set DoList to {}
set ExtensionList to {".jpg", ".png", ".gif"}
tell application "Safari"
activate
set TheURL to URL of document 1
--Get Images of current page
set ImageCount to do JavaScript "document.images.length" in document 1
repeat with TheItem from 1 to ImageCount
set ImageURL to do JavaScript "document.images[" & ((TheItem - 1) as string) & "].src" in document 1
copy ImageURL to end of ImageList
end repeat
end tell
if not ExtensionList is false then
if ImageList = {} then
display dialog "There is no images on this page" buttons {"Cancel"} default button "Cancel" with icon stop
else
--Make a Name List
set AppleScript's text item delimiters to {"/"}
repeat with GetName in ImageList
set TheName to last text item of GetName
copy TheName to end of NameList
end repeat
--Make Names Unique
repeat with TestName in NameList
set TestNameDo to TestName as string
if TestNameDo = "" then set TestNameDo to "Image"
set AppleScript's text item delimiters to {"."}
set TheExtension to ("." & last text item of TestName)
--Only Download Images in Extension List
set AppleScript's text item delimiters to {""}
if ExtensionList contains TheExtension then
copy true to end of DoList
else
copy false to end of DoList
end if
set PureName to characters 1 thru ((length of TestNameDo) - (length of TheExtension)) of TestNameDo as string
if length of PureName > 15 then
set PureName to characters 1 thru 15 of PureName as string
set TestNameDo to PureName & TheExtension
end if
if UniqueList does not contain TestNameDo then
copy TestNameDo to end of UniqueList
else
repeat with i from 1 to 666 --evil lives
if TheExtension = TestNameDo then
set NewName to PureName & i
else
set NewName to PureName & i & TheExtension
end if
if UniqueList does not contain NewName then
copy NewName to end of UniqueList
exit repeat
end if
end repeat
end if
end repeat
--Make Distination Folder
set TheHost to FindDomainName(TheURL)
set TimeStamp to do shell script "date -n +%Y-%m-%d-%H-%M-%S"
set thepath to ((path to desktop) as string) & "chest:downloads:" & TheHost & ":" & TimeStamp & ":"
do shell script "mkdir -p " & (POSIX path of thepath)
--Download Images
repeat with i from 1 to (count of ImageList)
if (item i of DoList) then
tell application "URL Access Scripting"
download (item i of ImageList) to file (thepath & (item i of UniqueList)) replacing yes
end tell
end if
end repeat
end if
end if
--Handler to work out domain name
on FindDomainName(TheURL)
set TheSuffix to ""
repeat with CurrentSuffix in SuffixOptions
if TheURL contains (CurrentSuffix & "/") then
set TheSuffix to CurrentSuffix
exit repeat
end if
end repeat
if TheSuffix = "" then
return "Misc"
else
set SuffixOffset to offset of (CurrentSuffix & "/") in TheURL
set JustDomain to (characters 1 thru (SuffixOffset - 1) of TheURL) as string
set PointOffSet to 0
repeat with NegOffSet from (length of JustDomain) to 1 by -1
if character NegOffSet of JustDomain is "." or character NegOffSet of JustDomain is "/" then
set PointOffSet to NegOffSet
exit repeat
end if
end repeat
set JustDomain to (characters (PointOffSet + 1) thru (length of JustDomain) of JustDomain as string) & CurrentSuffix
return JustDomain
end if
end FindDomainName
I hope you enjoy.