Without using GUI scripting, how can I use AppleScript to tell Safari to save the current browser page to a file in webarchive format? The following produces an error "The document “...” could not be exported as “foo.webarchive”
(where "..." is the title of the current web page, whatever it may be):
set the_filepath to "/tmp/foo.webarchive"
tell application "Safari"
activate
save document 1 in the_filepath
end tell
I've tried variations on this, such as different file name extensions (e.g., .html
) to see if anything works, but an error always arises. I've tried different ways of specifying the file name, but that also produces errors (of a different kind). I've tried using save ... as ".webarchive"
instead of the simple save
above, but that produces a different error about "document 1 doesn't understand the save message".
The AppleScript dictionary for Safari 11 has this description:
save v : Save a document.
save specifier : The document(s) or window(s) to save.
[in file] : The file in which to save the document.
[as saveable file format] : The file format to use.
This implies that it should be possible to save the current web page in a specific format understood by Safari (such as, hopefully, webarchive), and that I simply haven't figured out the right syntax.
save
should accept an "as format" argument, so I keep hoping that it really can work and that I just haven't figured out the right syntax."webarchive"
and"Web Archive"
?as "formatname"
(whatever "formatname" I try), AppleScript Editor reportserror "Safari got an error: document 1 doesn’t understand the “save” message." number -1708 from document 1
.save
command is broken. I tried a baresave document 1
. While it did bring up the save dialog box, it appended .css to the document name instead of .webarchive. Even though the the format option at the bottom of the box was "Web Archive". Then when I hit save, I got the same error of "could not be exported" and error code 10000 (Apple Event Handler Failed). Moreover, after running the script, the Format dropdown menu in the save dialog box disappeared