Assuming not too many differences from 10.9 to 10.10 (massive assumption), here’s something knocking around in my AppleScript menu items which prints PDFs from Safari and saves in a folder entitled "PDFs" on my desktop. It may help as a starting point.
Hmm. Looking at it now, it’s far from elegant and the structure could do with some tidying up — but it did what I needed it to do at the time.
For info, if you’re doing any UI scripting, the magical incantation “get every UI element” is your friend. Then you’ve just got to work out which UI element you need to be targeting.
————————
set saveFolder to ((path to desktop folder) as string) & "PDFs" -- Note that we make this folder if it doesn't exist
set saveName to (do shell script "date +%Y-%m-%d_%H.%M.%S") & ".pdf"
-- You need to open a page in Safari
-- This just makes sure the page is fully loaded
tell application "Safari"
tell document 1
-- Delay until page loaded
repeat until (do _javascript_ "document.readyState") is "complete"
delay 0.2
end repeat
end tell
end tell
-- Print to PDF
tell application "System Events"
-- Make saveFolder if it doesn't exist
if not (exists folder saveFolder) then
make new folder at end of (path to desktop) with properties {name:"PDFs"}
end if
-- Set saveFolder to an alias for later use
tell me to set saveFolder to saveFolder as alias
set currentProcess to name of first application process whose frontmost is true
set frontmost of process "Safari" to true
tell process "Safari"
keystroke "p" using command down
tell front window
repeat until exists sheet 1
delay 0.02
end repeat
tell sheet 1 -- Print dialog
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"
repeat until exists sheet 1
delay 0.2
end repeat
tell sheet 1 -- "Save as…" dialog
keystroke "g" using {command down, shift down} -- Open "Go to folder" dialog
repeat until exists sheet 1
delay 0.2
end repeat
tell sheet 1 -- "Go to folder:" dialog
set value of text field 1 to (POSIX path of saveFolder) as string -- Set folder path
click button "Go"
repeat while exists sheet 1
delay 0.2
end repeat
end tell -- End of "Go to folder:" dialog
set value of text field 1 to saveName -- Set file name
click button "Save"
end tell -- End of "Save as…" dialog
end tell
end tell
end tell
set frontmost of process currentProcess to true
end tell
————————
—— END