Automate Saving Webpages

I save all my webpages as pdf. (I use Safari Browser)
To do this I have to press Cmd-P , then press Tab 4 times then the down arrow twice and then hit enter twice. I am a keyboard user and hardly touch the mouse.
Is there a way to automate this process using applescript?
Thanks in advance.

This should work, but for some reason it doesn’t to the final return. :confused:

set leftArrow to ASCII character 28
set rightArrow to ASCII character 29
set upArrow to ASCII character 30
set downarrow to ASCII character 31

tell application "Safari" to activate
tell application "System Events"
	delay 0.1
	keystroke "p" using command down
	delay 1
	repeat 5 times
		delay 0.1
		keystroke tab
	end repeat
	keystroke downarrow
	delay 1
	keystroke downarrow
	delay 1
	keystroke return
	delay 5
	keystroke return
end tell

sorry… i wanted to get this post before the last post …

i know nothing about apple script

and i typed this in script editor just to have fun with apple script natural english language

tell app "safari"
print current webpage as pdf
end tell

i got an error but the work “webpage” was highlighted…so I guessed that word must be the problem

so i tried this

tell app "safari"
print current web page as pdf
end tell

(just separated web and page)
got error again but the “web page” was highlighted

then i tried this

tell app "safari"
print current page as pdf
end tell

i got the dialog box for print

can some one plz help me from here

(applescript is fun)

set leftArrow to ASCII character 28
set rightArrow to ASCII character 29
set upArrow to ASCII character 30
set downarrow to ASCII character 31

tell application "Safari" to activate
tell application "System Events"
	delay 0.1
	keystroke "p" using command down
	delay 1
	repeat 4 times
		delay 0.1
		keystroke tab
	end repeat
	keystroke downarrow
	delay 1
	keystroke downarrow
	delay 1
	keystroke downarrow
	delay 1
	keystroke return
	delay 5
	keystroke return
end tell

thanks cwt…i modified it a little…i did not want preview…i wanted to “save as pdf”

You could also install the free cups-pdf package and directly print websites to PDF using Safari’s convenient print command, which also allows to print without displaying the print dialog:


tell application "Safari"
	print document 1 without print dialog
end tell

Of course you need to make sure that cups-pdf is the default printer when using this approach. But this can easily be done by applying AppleScript code shown in this article.

thanks for the help martin (i have no idea why google search (for “saving web pages as pdf” did not land me at cups-pdf)
though i have been able to use cups-pdf for saving as pdf, i have been unable to use ur applescript------it gives me an error “Error while printing” and in the script editor the error is “Appleevent handler failed”

You told me to setup cup-pdf as default printer. When i open “Print” from File menu in safari, i get this(http://rapidshare.com/files/144277337/Picture_5.png.html) by default. Does it not mean that i have correctly setup cup-pdf as default printer?
(Also, i do not have any other printer installed)
Please help.

The cups-pdf setup seems to be perfect, I have exactly the same setting. So which Mac OS X and Safari version are you running on your Mac? Because, not ALL versions of Safari feature the «print without print dialog» command. I am on Mac OS X 10.5.5 and Safari 3.1.2.

safari 3.0.4 (i will update it)
leopard 10.5.0 (will i have to update it?)