Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

AppleScript script to save a Safari page as the web-archive fails

I worked out the following script:


tell application "Safari"
	set theWebContent to document 1
	set theName to name of current tab of window 1
	set theExtension to ".webarchive"
	save theWebContent in POSIX file (POSIX path of file (((path to desktop) as text) & theName & theExtension))
end tell


It worked only couple of times then started choking with Safari pulling a drop-down alert dialog telling it was unable to save the contents of the tab as a web-archive. What do I miss?

There're some complex examples of saving as the web-archive involving scripting objects. Could we do without such complexity?

Posted on Jun 7, 2019 12:37 PM

Reply

Similar questions

3 replies

Jun 7, 2019 2:37 PM in response to scrutinizer82

This will save the current Safari window/tab name to a .webarchive in the last saved folder from Safari. You may want to do a Save As and set it to the Desktop, and then cancel the Save As first. If you cannot find the .webarchive, you can use the following in the Finder window's search field:


name:.webarchive


This is a GUI script that is subject to the whims of macOS Safari development team. It could break in the future due to changes in the underlying frameworks organization. Tested on macOS Mojave 10.14.5 and using Safari 12.1.1.


-- GUI script to save Safari's current window (tab)'s name to a webarchive
-- in the last saved to folder (e.g. Desktop). Tested: macOS 10.14.5/Safari 12.1.1
-- use name:.webarchive in Finder search window to find lost files

tell application "Safari"
	activate
	tell application "System Events"
		tell application process "Safari"
			set frontmost to true
			keystroke "S" using {command down} -- Save As…
			tell last pop up button of its sheet
				select menu item "Web Archive"
			end tell
			key code 36 -- to accept Save button
		end tell
	end tell
end tell
return





Jun 8, 2019 10:48 AM in response to scrutinizer82

The Safari AppleScript dictionary does not offer a .webarchive save format ( save foo in bar as web archive), or this scripting solution would be far simpler than using GUI scripting. As it stands, all of the Safari AppleScript solutions searched on the web use GUI scripting to consistently save web content into a .webarchive. Piping a website into textutil and converting to webarchive only converts the website text, and misses most of the content — so not an alternative solution.


There is webarchiver, which can be installed by homebrew or MacPorts, and requires Xcode for either of these package managers to build it. Webarchiver is a command-line utility based on the Cocoa WebArchive implementation in Objective-C.


webarchiver -url <some url> -output ~/Desktop/some.webarchive


I already had Xcode 10.2.1 installed on macOS Mojave 10.14.5 and the brew install webarchiver silently built the tool correctly without any Xcode hands on.

AppleScript script to save a Safari page as the web-archive fails

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.