This script will save a page in Safari to a particular folder location in HTML format. From the AppleScript Users list.
OS version: OS X
set d to "1/1"
set f to paragraph 2 of "
" & (path to "cusr")&"Safari_" & ((current date)-(get date d)) &".html"
set my text item delimiters to {"http://"}
tell app "Safari" to set s to source of front document
open for access file f with write permission
write "" & s to file f
close access file f
tell app "Finder"
activate
reveal file f
end
It’s a bad script and was a bad script even when it was posted sixteen years years ago! (Sorry, Ray.) That’s the problem when trying to use scripts you find on the Internet. There’s a lot of bad code out there (even here on MacScripter sometimes!) and a lot of it’s out of date anyway. But it hangs around for years and is perpetuated by people borrowing it and reposting it in their own scripts. I and others spend of lot of time pointing out the same old mistakes and bad practices over and over again.
If I remember correctly, back in 2003, date “1/1” would return a date object representing the 1st of January in the year the script was run — if the machine was configured for dmy or mdy dates. So the script’s apparently meant to save the source of the current Safari document to a file in the user’s home folder (not a great idea) with the name “Safari_” followed by the number of seconds since the beginning of the year and the extension “.html”. I’ve no idea why it sets f to the second paragraph of a text beginning with a line ending or why it changes the text item delimiters.
Using today’s best practices — including saving the file to the user’s desktop instead of to their home folder:
set now to (current date)
copy now to Jan1
tell Jan1 to set {its day, its month, its time} to {1, January, 0}
set f to (path to desktop as text) & "Safari_" & (now - Jan1) & ".html"
tell application "Safari" to set s to source of front document
set fRef to (open for access file f with write permission)
try
set eof fRef to 0
write s as «class utf8» to fRef
close access fRef
on error msg
close access fRef
display dialog msg
end try
tell application "Finder"
activate
reveal file f
end tell
Even so, this only saves the page’s immediate source code and opening the file in Safari may not reproduce the original display exactly.