Pennyworth Posted October 10, 2014 Posted October 10, 2014 Hey everyone! I've built an AppleScript workflow that goes like this: Keyword: "pdf" (or Hotkey: CMD + SHIFT + P) Current Safari page renders in Safari Reader File > Export as PDF Auto-save The problem I'm having is that the 'Export as PDF' menu item doesn't work exactly like "Print to PDF." The PDF output has almost zero margins, no header/footer to identify a source, and it's just not as desirable as printing-to-PDF from the print dialogue (though it is more efficient code-wise). Can anyone here tweak the following script to produce the same result through the Print-to-PDF function? I gave it a shot but I'm new to AppleScript and couldn't get it to work. Thanks! on alfred_script(q) tell application "System Events" tell application process "Safari" set frontmost to true # Render page in Safari Reader keystroke "r" using {command down, shift down} # Delay for smoothness delay 0.02 # Export as PDF click menu item "Export as PDF…" of menu "File" of menu bar 1 # Save PDF click button "save" of sheet 1 of window 1 end tell end tell end alfred_script
dfay Posted October 10, 2014 Posted October 10, 2014 You're on the right track here with UI scripting - just need to figure out what parts of the print dialog you need to click. I did this for Keynote, here: https://github.com/derickfay/keynote-to-pdf You might be able to re-use some of the code - look at the Keynote 5.3 version - as I recall that print dialog was the standard OS X print dialog as it would also appear in Safari....they customized it in Keynote 6. It takes some patience and trial-and-error, but it's a task I had to do a couple times a day and it was worth it. These two tools are very helpful for UI scripting: applescript - How to know the name of UI elements using Accessibility inspector (or any other tool) - Ask Different Finding Control and Menu Items for use in AppleScript User Interface Scripting - Mac OS X Hints
dfay Posted October 10, 2014 Posted October 10, 2014 (edited) Yeah, I just tested it and the Keynote 5.3 version works in Safari with one line (selecting Individual Slides in Keynote) commented out. Here's the script (I also deleted some if statements that would never be true in Safari, just to clean it up, and added your code to render in Reader): -- Safari tell application "System Events" tell application process "Safari" set frontmost to true repeat until window 1 exists end repeat # Render page in Safari Reader keystroke "r" using {command down, shift down} delay 0.02 -- Print; wait until the sheet is visible click menu item "Print…" of menu 1 of menu bar item "File" of menu bar 1 repeat until sheet 1 of window 1 exists end repeat set thePopUp to first pop up button of sheet 1 of window 1 whose description is "Presets" click thePopUp click menu item "Default Settings" of menu 1 of thePopUp --replace if desired with your preferred preset click menu button "PDF" of sheet 1 of window 1 click menu item "Save as PDF…" of menu 1 of menu button "PDF" of sheet 1 of window 1 -- Save as PDF... end tell end tell Edited October 10, 2014 by dfay
Pennyworth Posted October 10, 2014 Author Posted October 10, 2014 Thanks Derick! This works well, except sometimes the print dialogue comes in before the page can render in Safari Reader, despite the delay code.
Vero Posted October 11, 2014 Posted October 11, 2014 Thanks Derick! This works well, except sometimes the print dialogue comes in before the page can render in Safari Reader, despite the delay code. I'm moving this discussion to the "Workflow Help & Questions" section as "Share your workflows" is specifically for complete and functional workflows.
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now