AppleScript to save current Safari window in webarchive format?
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.