Safari download images - fixes naming conflicts, stored based on site

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 : ""
property SuffixOptions : {".com", ".net", ".org", ".info", ".us", ".biz", ".tv", ".mobi", ".cc", ".ws", ".bz", ".tc", ".vg", ".ms", ".gs", ".name", "", ".de", ".be", ".eu", ".at", "", "", "", "", "", "", ".cn", ".tw"}

set ImageList to {}
set NameList to {}
set UniqueList to {}
set DoList to {}
set ExtensionList to {".jpg", ".png", ".gif"}

tell application "Safari"
	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
		--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
				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
				repeat with i from 1 to 666 --evil lives
					if TheExtension = TestNameDo then
						set NewName to PureName & i
						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"
		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. :slight_smile:


this is really useful. i would like to modify it slightly, if possible, to achieve the following:

  1. the url that the images come from is in the script. i.e. safari doesnt need to be open (i have an example where a script collects text when safari is closed, but not images). so replacing ‘document 1’ with something? if this can’t be done it’s not a massive problem, but i would ideally like to source images from maybe 5 urls, so can it be altered to include ‘document 2’ etc?

  2. the script runs every 5 minutes or so, so that if a page is updated with new images, it can keep collecting them automatically. i’d probably have the script as an application that is always open in the background.

  3. for what i need to achieve, i’d like the images to be saved to a designated folder on the desktop (called ‘images’ for example), and rather than creating a new folder for each time it collects the images, i was hoping the images could just be added to that ‘images’ folder each time, without sub-folder etc? i know that would cause a headache with repeating file names for those images that aren’t updated on the url, so i was wondering if applescript could delete the files already in the folder a few seconds before it adds the new ones? i would then use automator as a folder action to number the jpegs as 0.jpg, 1.jpg 2.jpg etc for what i need to do with them.

i dare say a lot of this can’t be done simply, and as i’m inexperienced with applescript, some tips would be great (im not expecting anyone to post the whole code, im not that cheeky!)!! im kind of half way to achieving an interesting new media installation piece whereby news headlines/stories are re-presented as a kind of filmic narrative - i have got the script working to source the headlines from rss feeds into the programming software i’m using, i’m now hoping that i can figure out how to download the images and use those!!

thanks very much,