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
script stops at getting file path
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?
tell application "Safari" to activate
delay .2
tell application "System Events"
tell process "Safari"
keystroke "r" using command down
end tell
end tell
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
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
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