8

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.

9
  • If you can't find a way other then through UI Scripting, here's a nice example of it. AppleScript: Safari - Save As Web Archive Commented Dec 9, 2017 at 2:57
  • @user3439894 Thanks. Unfortunately, I'm trying to automate a more complicated workflow, and need to control things like where it writes the file. The AppleScript dictionary for Safari implies that 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.
    – mhucka
    Commented Dec 11, 2017 at 20:16
  • Have you tried variations such as "webarchive" and "Web Archive"? Commented Dec 11, 2017 at 20:28
  • @BallpointBen Yes; as soon as I add as "formatname" (whatever "formatname" I try), AppleScript Editor reports error "Safari got an error: document 1 doesn’t understand the “save” message." number -1708 from document 1.
    – mhucka
    Commented Dec 11, 2017 at 20:33
  • 1
    I think Safari's AppleScript save command is broken. I tried a bare save 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
    – Bruce
    Commented Dec 12, 2017 at 7:36

1 Answer 1

3
+50

I have created a bundled script that saves the current tab to the desktop as a webarchive. Credit to newzealandpaul for his webarchiver shell command that powers this script. You can tweak the code to fit your specific needs, but this should do what that code in your question is trying to do.

Here is the bundled script: SaveWebarchive

This works with the webarchiver command bundled;

set fileName to "foo"

tell application "Safari" to set targetURL to (URL of document 1) as string
set commandPath to POSIX path of (path to resource "webarchiver.command")
do shell script "" & commandPath & " -url " & targetURL & " -output ~/Desktop/" & fileName & ".webarchive"

Note that the downloaded script may have the Script Editor interface showing the Log over the entire window.

7
  • So I download the source code for webarchiver and go to build it and find I had already done it before in September. I prefer to read the source code and build the executable vs. a pre-built executable when I don't really know the source. Anyway, I have to admit after reading the OP I focussed on using the save command and had forgotten about webarchiver. It works really nice and while it's not my question I'll still give a +1 for the answer! Commented Dec 12, 2017 at 0:16
  • I understand that you would want to compile it yourself! Just be sure to add .command to the end of the file and put it in the same location as the one I provided.
    – Jake3231
    Commented Dec 12, 2017 at 0:17
  • It actually shouldn't need the .command extension so long as webarchiver is set as executable. Haven't test it in AppleScript because I used it back in Sept from Terminal in a bash script just as webarchiver and that's how I know it works nice. Commented Dec 12, 2017 at 0:23
  • Okay. If that works for you, then that sounds great! I have tested the bundled script that I included, but if you can use webarchiver in an even better way I'd love to hear how you do it!
    – Jake3231
    Commented Dec 12, 2017 at 0:29
  • It wasn't anything special per se, I just had a bunch of URLs in a file and used is as in the example at the authors GitHub project page, e.g. webarchiver -url http://www.google.com -output google.webarchive, but tokenized the command so when the script read the file it looped through the URLs. Just really basic stuff. I had forgotten all about it until you posted your answer to muck's question. Commented Dec 12, 2017 at 0:53

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .