Safari pdf export

There are few exists here, but I want to use the safari’s new export option rather than going through several steps via print.


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 "R" using {command down, shift down}
		delay 0.02
		click menu item "Export as PDF." of menu "File" of menu bar 1
		repeat until exists sheet 1 of window 1
			delay 0.02
		end repeat
		keystroke "g" using {command down, shift down}
		repeat until exists sheet 1 of sheet 1 of window 1
			delay 0.02
		end repeat
		log "passed delay"
		tell sheet 1 of sheet 1 of window 1
			set value of text field 1 to POSIX path of SaveFolder
			click button "Go" of sheet 1 of sheet 1 of window 1
		end tell
		click button "save" of sheet 1 of window 1
	end tell
end tell

I got couple of issues here

  1. script stops at getting file path
  2. I have delay of 0.02 for keystroke “R” using {command down, shift down}, but this may not be sufficient with multiple pages. How to improve this?

Hi chittu,

Nothing happens when I manually press “r” with command + shift.

Also, getting what file path? Or did you mean folder path?

gl,
kel

Hi,

This works for me to reload the page:

tell application "Safari" to activate
delay .2
tell application "System Events"
	tell process "Safari"
		keystroke "r" using command down
	end tell
end tell

It’s a reader view of safari, atm I removed

keystroke “R” using {command down, shift down}

Note: This can work only on safari 7, could any of you help me to fix the script, thanks

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"
		click menu item "Export as PDF." of menu "File" of menu bar 1
		repeat until exists sheet 1 of window 1
			delay 0.02
		end repeat
		keystroke "g" using {command down, shift down}
		repeat until exists sheet 1 of sheet 1 of window 1
			delay 0.02
		end repeat
		log "passed delay"
		tell sheet 1 of sheet 1 of window 1
			set value of text field 1 to POSIX path of SaveFolder
			click button "Go" of sheet 1 of sheet 1 of window 1
		end tell
		click button "save" of sheet 1 of window 1
	end tell
end tell

Hi.

In the ‘tell sheet 1 of sheet 1 of window 1’ statement, it should just be ‘click button “Go”’. The 'of’s are implied by it being in the ‘tell’ block.

		tell sheet 1 of sheet 1 of window 1
			set value of text field 1 to POSIX path of SaveFolder
			click button "Go"
		end tell
		click button "save" of sheet 1 of window 1
	end tell
end tell

thanks, but got a new error “You don’t have permission.” to save file in my downloads folder :frowning:


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"
		click menu item "Export as PDF." of menu "File" of menu bar 1
		repeat until exists sheet 1 of window 1
			delay 0.02
		end repeat
		keystroke "g" using {command down, shift down}
		repeat until exists sheet 1 of sheet 1 of window 1
			delay 0.02
		end repeat
		log "passed delay"
		tell sheet 1 of sheet 1 of window 1
			set value of text field 1 to POSIX path of SaveFolder
			click button "Go"
		end tell
		click button "save" of sheet 1 of window 1
	end tell
end tell

Where can I learn more about GUI, I can’t get why it has be just "click button “Go”’ rather than “click button “Go” of sheet 1 of sheet 1 of window 1”:mad:

I don’t know the answer to that offhand. It sounds like one of the new security features in Mavericks.

It’s because .


tell application "System Events"
	tell process "Safari"
		tell sheet 1 of sheet 1 of window 1
			click button "Go"
		end tell
	end tell
end tell

. means the same as .

click button "Go" of sheet 1 of sheet 1 of window 1 of process "Safari" of application "System Events"

If you make that middle line ˜click button go of sheet 1 of sheet 1 of window 1’, the whole reference then becomes .

click button "Go" of sheet 1 of sheet 1 of window 1 of sheet 1 of sheet 1 of window 1 of process "Safari" of application "System Events"

. which, as the original error message was trying to tell you, is trying to click a button in a place which doesn’t exist. It’s AppleScript syntax generally rather than anything specific to GUI Scripting.

Addendum: I’ve not had any permissions problems with the script saving to my own Downloads folder, but the ˜POSIX path of SaveFolder’ bit doesn’t work in the ˜tell sheet 1 of sheet 1 of window 1’ environment. It’s better to do the conversion outside any ˜tell’ blocks, as soon as you get the folder alias:


set SaveFolderPath to POSIX path of (choose folder with prompt "Select Folder to Save PDF Files")

tell application "Safari" to activate
tell application "System Events"
	tell process "Safari"
		click menu item "Export as PDF." of menu "File" of menu bar 1
		repeat until exists sheet 1 of window 1
			delay 0.02
		end repeat
		keystroke "g" using {command down, shift down}
		repeat until exists sheet 1 of sheet 1 of window 1
			delay 0.02
		end repeat
		log "passed delay"
		tell sheet 1 of sheet 1 of window 1
			set value of text field 1 to SaveFolderPath
			click button "Go"
		end tell
		click button "save" of sheet 1 of window 1
	end tell
end tell

now it works great, thanks for the help.

nice explanation on tell ;), this clearly says how bad am I :rolleyes: