3This tool creates [
Safari webarchive files](
https://en.wikipedia.org/wiki/Webarchive) on the command line.
4This gives you an offline archive of web pages, which can be stored and backed up independently of any cloud services.
7$ save_safari_webarchive
"https://example.com" "example.webarchive"
10These archives are the same as those created by the
`File > Save As…` menu item, but now you can create them programatically and in bulk.
14It opens the given URL in a
`WKWebView`, calls
`createWebArchiveData` to create a webarchive file, and saves it to disk.
15That's the core workflow, and then there's a bunch of error handling around that.
17For a more detailed explanation of this code, see <https://alexwlchan.net/2024/creating-a-safari-webarchive/>
21### Install from source
231. Install the Xcode Command Line Tools
242. Download the
`save_safari_webarchive.swift` script from this repo
253. Compile the script into a binary:
28 $ swiftc save_safari_webarchive.swift
314. Copy the compiled binary
`save_safari_webarchive` to somewhere in your PATH.
33### Install a compiled binary
351. Find the latest [
GitHub release](
https://github.com/alexwlchan/safari-webarchiver/releases)
362. Download the zip file which is appropriate for your system (Intel =
`x86_64`, Apple Silion =
`aarch64`)
373. Open the zip file, and add the
`save_safari_webarchive` app to your PATH
39The app is just a compiled version of the Swift script.
40It isn't notarised, so when you run it, you may get a warning that this app is from an unidentified developer.
41You can get around this by right-clicking the app icon in Finder, and choosing
`Open` from the shortcut menu.
45Run the script passing two arguments: the URL you want to archive, and the path where you want to save the webarchive file.
47For example, this command will save the URL to this GitHub repo to
`safari-webarchiver.webarchive`:
50$ save_safari_webarchive
"https://github.com/alexwlchan/safari-webarchiver" "safari-webarchiver.webarchive"
55This is partially inspired by [
a similar script](
https://github.com/newzealandpaul/webarchiver) written in 2008 by newzealandpaul.