Save Current Page in Safari to PDF

I’m trying to save the page I’m on in Safari to a specific filename in a specific folder. The work so far looks like this:

set inset_number to text returned of (display dialog "Enter the Inset Number" default answer "000") as string
set SaveFolder to choose folder with prompt "Select Folder to Save PDF Files"
set filename to "filename"
--set the clipboard to the result as text

tell application "Safari" to activate
tell application "System Events"
	tell process "Safari"
		keystroke "p" using command down
		tell front window
			tell window "Print"
				tell UI element 1
					tell UI element 4
						keystroke return
						keystroke "v" using command down
						keystroke return
					end tell
				end tell
			end tell
		end tell
	end tell
end tell
--tell active document
set save_name to (inset_number & ".pdf") as string
set dest_path to SaveFolder as string
set myFileName to (dest_path & ":" & save_name)
save myFileName
--end tell

The file just doesn’t appear in the folder (or at all). Would someone offer a suggestion? Thank you.

Try this.

set inset_number to text returned of (display dialog "Enter the Inset Number" default answer "000") as string
set SaveFolder to choose folder with prompt "Select Folder to Save PDF Files"
set filename to "filename"
--set the clipboard to the result as text

tell application "Safari" to activate
tell application "System Events"
	tell process "Safari"
		keystroke "p" using command down
		tell front window
			repeat until exists sheet 1
				delay 0.02
			end repeat
			tell sheet 1
				click menu button "PDF"
				repeat until exists menu 1 of menu button "PDF"
					delay 0.02
				end repeat
				click menu item "Save as PDF." of menu 1 of menu button "PDF"
			end tell
		end tell
		repeat until exists window "Save"
			delay 0.2
		end repeat
		
		tell window "Save"
			keystroke "g" using {command down, shift down}
			repeat until exists sheet 1
				delay 0.2
			end repeat
			tell sheet 1
				--Subject_Added Peter to test how to pass file name
				set value of text field 1 to POSIX path of SaveFolder
				click button "Go"
			end tell
			repeat while exists sheet 1
				delay 0.2
			end repeat
			click button "Save"
		end tell
	end tell
end tell

--tell active document
set save_name to (inset_number & ".pdf") as string
set dest_path to SaveFolder as string
set myFileName to (dest_path & ":" & save_name)
save myFileName
--end tell

Have a good day! :slight_smile:

theway…

The code you posted is much closer, but it is giving the file the name of the current page in Safari. I’m wanting to have the name changed to something I specify using the inset_parameter variable. That part seems to be ignored (it presents the dialog to specify a name, but in the final output, the pdf is named for the current title of the window opened in safari.

I’ll play around with your code to see how I need to change that.

Thank you for your help so far.

You just need to set the value of the name field. Here’s the corrected code.

set inset_number to text returned of (display dialog "Enter the Inset Number" default answer "000") as string
set SaveFolder to choose folder with prompt "Select Folder to Save PDF Files"
set filename to "filename"

--set the clipboard to the result as text

tell application "Safari" to activate
tell application "System Events"
	tell process "Safari"
		keystroke "p" using command down
		tell front window
			repeat until exists sheet 1
				delay 0.02
			end repeat
			tell sheet 1
				click menu button "PDF"
				repeat until exists menu 1 of menu button "PDF"
					delay 0.02
				end repeat
				click menu item "Save as PDF." of menu 1 of menu button "PDF"
			end tell
		end tell
		repeat until exists window "Save"
			delay 0.2
		end repeat
		
		tell window "Save"
			keystroke "g" using {command down, shift down}
			repeat until exists sheet 1
				delay 0.2
			end repeat
			tell sheet 1
				
				set value of text field 1 to POSIX path of SaveFolder
				click button "Go"
			end tell
			repeat while exists sheet 1
				delay 0.2
			end repeat
			set value of text field 1 to inset_number
			click button "Save"
		end tell
	end tell
end tell

--tell active document
set save_name to (inset_number & ".pdf") as string
set dest_path to SaveFolder as string
set myFileName to (dest_path & ":" & save_name)
save myFileName
--end tell

Was this what you wanted?

Here’s the final working as expected: (Thanks, thewaytoapplescript!)

set myFileName to text returned of (display dialog "Enter the File Name" default answer "MyPDF") as string
set SaveFolder to choose folder with prompt "Select Folder to Save PDF Files"


tell application "Safari" to activate
tell application "System Events"
	tell process "Safari"
		keystroke "p" using command down
		tell front window
			repeat until exists sheet 1
				delay 0.02
			end repeat
			tell sheet 1
				click menu button "PDF"
				repeat until exists menu 1 of menu button "PDF"
					delay 0.02
				end repeat
				click menu item "Save as PDF." of menu 1 of menu button "PDF"
			end tell
		end tell
		repeat until exists window "Save"
			delay 0.2
		end repeat
		
		tell window "Save"
			keystroke "g" using {command down, shift down}
			repeat until exists sheet 1
				delay 0.2
			end repeat
			tell sheet 1
				--Subject_Added Peter to test how to pass file name
				set value of text field 1 to POSIX path of SaveFolder
				click button "Go"
			end tell
			repeat while exists sheet 1
				delay 0.2
			end repeat
			set value of text field 1 to (myFileName & ".pdf") as string
			click button "Save"
		end tell
	end tell
end tell


Hey thewaytoapplescript:

In the realm of “teach a man to fish…”

Would you be willing to help me understand the necessity of the repeat and delay sections?

Thanks,

Of course!!

You see, GUI scripting is a “time-dependant” thing. You never know how long it will take for a window or a sheet to appear. If you don’t put repeat statements or you don’t even put delays in your script, your script won’t function. This is because the script moves faster than the Interface.

By putting repeat statements, the script then waits until the specified sheet or window appears or disappears before going on.

Think of it this way“ Look at the following script:


tell application "System Events"
tell process "Safari"
keystroke "p" using {command down}
click button "Print" of sheet 1 of window 1
end tell
end tell

This might not work. Why? Because the sheet takes a second or two to appear, and the code moves on to the click statement. When it does not find a sheet 1 (because the sheet is still appearing) it will return an error.

This could work:


tell application "System Events"
tell process "Safari"
keystroke "p" using {command down}
delay 2
click button "Print" of sheet 1 of window 1
end tell
end tell

I say could because you don’t know whether the sheet will open in under two seconds or not. It depends on the speed of your commuter, the number of applications running, ect.

So with a repeat statement the script waits until the sheet appears, then presses the “Print” button.


tell application "System Events"
tell process "Safari"
keystroke "p" using {command down}
repeat until exists sheet 1 of window 1
delay 0.02
end repeat
click button "Print" of sheet 1 of window 1
end tell
end tell

As for the delay statements inside the repeat loops, those aren’t necessary. They just make the application use less memory…

I hope this answers your question. If it doesn’t, please let me know! :slight_smile:

Great answer! With enough detail to teach me.

I can tell as I go forward with my scripting, those delays and repeats will be useful in making sure my end results are as expected.

Thanks so much.

BTW, I didn’t know how quickly you’d respond (turns out quite quickly!) but I played with the code you originally sent to see if I could see why it was saving with the page name. I love to learn!

Have a great one!

Hi,
Just a small hint not about AppleScript in particular, but to save wonderful pdf (and using YOUR html document) …
you should reach http://www.princexml.com and have a look.
It’s a free CLI utility for personal use, having in this case a small Prince logo at the beginning of the PDF.
Could be useful for somebody.