Safari save webarchive pages to chest

Its save as webarchive format, in a chest, in a folder named after the site, named site dash time. It also add the URL to spotlights comments so you know where it come from, getting the URL in the Metadata is tricky. Webarchive’s add keywords automatically to the metadata. I think its a good way to save you web pages, I got it on a control a keystroke in Safari with FastScripts

property TheURL : "http://www.macscripter.net/"
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"}

tell application "Safari"
	activate
	if document 1 exists then
		set TheURL to URL of document 1
	else
		make new document
		delay 5
		set TheURL to URL of document 1
	end if
end tell
set tempPath to path to desktop as string
set tempName to "a.webarchive"
set tempFile to tempPath & tempName
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:saved:" & TheHost & ":"
set TheName to TheHost & "-" & TimeStamp & ".webarchive"
do shell script "mkdir -p " & (POSIX path of thepath)

set theFile to thepath & TheName

tell application "Safari"
	activate
	if document 1 exists then
		tell application "System Events"
			tell process "Safari"
				keystroke "s" using command down
				keystroke "a" using command down
				keystroke tempName
				delay 1
				keystroke "d" using command down
				click (pop up button 1 of group 1 of sheet 1 of window 1)
				delay 1
				click (menu item 2 of menu 1 of pop up button 1 of group 1 of sheet 1 of window 1)
				delay 1
				click (button "Save" of sheet 1 of window 1)
				delay 2
			end tell
		end tell
	end if
end tell
do shell script "mv " & (POSIX path of tempFile) & " " & (POSIX path of theFile)
tell application "Finder"
	set comment of file theFile to TheURL
end tell

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
		try
			set JustDomain to (characters (PointOffSet + 1) thru (length of JustDomain) of JustDomain as string) & CurrentSuffix
		on error
			set JustDomain to "Misc"
		end try
		return JustDomain
	end if
end FindDomainName

In Safari 3 Beta you still can’t save as webarchive by a non GUI method. But it can save as HTML where Safari 2 does .downloads files

I use this a lot so I hope people enjoy this.

How would I modify this to simply open an html page and save it as a webarchive? I don’t need any of the metadata stuff, and I have no idea what a chest is. I just want it to open “a.html” and save it in the same place as “a.webarchive”

Model: MacBook Pro 2.33GHz
AppleScript: 1.10.7
Browser: Safari 419.3
Operating System: Mac OS X (10.4)

Hello.

This script is for saving all the tabs in the front window to a folder as web archive files, for easy reference on that subject/ or project.

This is a version of the script of the original poster, compiled with a script of Craig and the idea of having macscripter pages show up with the full title of the subject when saved. :slight_smile:

It works for Safari 5.0, and you must choose a folder to store all the tabs of the front window in.

Notes:
You must change the hardcoded path to something suitable for you in the choose folder dialog.

Enjoy :slight_smile:


” Please don't post it elsewhere, just refer to it in this post.
property SuffixOptions : {".com", ".net", ".org", ".info", ".edu", ".no", ".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 TheURL to "http://macscripter.net/viewtopic.php?id=21584"

-- Safari window to webarchieves: Works for safari 5.0 english version.

set theFolder to (choose folder default location alias "Macintosh HD:Users:McUsr:Documents:CurrentProjects:McUsr:" with prompt "Folder To Store Webarchieves In") as text
set tempPath to path to desktop as string
set tempName to "a.webarchive"
tell application "Safari"
	activate
	set tabCount to (get count of tabs of its front window)
	log tabCount
	repeat with i from 1 to tabCount
		tell tab i of its front window to do JavaScript "self.focus()"
		-- create filenename
		set tabname to item 1 of (get name of its document)
		if (offset of "Macscripter /" in tabname) = 1 then
			set tabname to characters 15 thru -1 of tabname as text
			-- display dialog tabname
		end if
		set TheURL to item 1 of (get URL of its document)
		set theDomain to my FindDomainName(TheURL)
		set tabname to my strip_char(tabname, true, " ")
		set theFileName to tabname & "_" & theDomain & ".webarchive"
		-- 	activate application "Safari"
		tell application "System Events"
			tell process "Safari"
				click menu item "Save As." of menu 1 of menu bar item "File" of menu bar 1
				delay 0.2
				keystroke "d" using command down
				delay 0.2
				set value of text field 1 of sheet 1 of window 1 to tempName
				click button "Save" of sheet 1 of window 1
			end tell
		end tell
		set theFile to theFolder & theFileName
		do shell script "/bin/mv " & quoted form of POSIX path of (tempPath & tempName) & " " & quoted form of POSIX path of theFile
		tell application "Finder"
			set comment of file theFile to TheURL
		end tell
		
	end repeat
end tell

to strip_char(the_name, use_replace_char, replace_char)
	-- for use in stripping characters out of mac file names that are illegal in Windows
	-- must be run within a Finder "tell" block
	
	set special_char to {"/", ",", ";", ":", "?", "*", "<", ">", "¢", "Æ’", ".", ".", " ", "-"}
	set temp_name to ""
	set name_length to (the number of characters of (the_name as text))
	-- 	set the_name to (the name of this_item as text)
	repeat with my_char from 1 to name_length
		if character my_char of the_name is not in special_char then
			set temp_name to temp_name & character my_char of the_name
		else
			if use_replace_char then set temp_name to temp_name & replace_char
		end if
	end repeat
	return temp_name
end strip_char


log theDomain
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
		try
			set JustDomain to (characters (PointOffSet + 1) thru (length of JustDomain) of JustDomain as string) & CurrentSuffix
		on error
			set JustDomain to "Misc"
		end try
		return JustDomain
	end if
end FindDomainName