Monday, May 20, 2019

#1 2003-04-13 10:50:46 am

Ray Barber
Site Admin
From:: Lake Dallas, Texas
Registered: 2002-11-20
Posts: 352

Safari Save

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"
reveal file f

Ray Barber
Admin | MacScripter



#2 2019-03-05 01:23:15 am

Registered: 2019-03-03
Posts: 122

Re: Safari Save

Don't work for me, I get error measage in first code line:

error "Μη έγκυρη ημερομηνία και ώρα date 1/1 of «script»." number -30720

macOS Mojave -- version 10.14.4
Safari -- version 12.1



#3 2019-03-05 02:55:49 am

Nigel Garvey
From:: Warwickshire, England
Registered: 2002-11-20
Posts: 4868

Re: Safari Save

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)
   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"
   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.

Last edited by Nigel Garvey (2019-03-05 03:02:31 am)




Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)