Jump to content

Recommended Posts

Posted

Hey everyone! I've built an AppleScript workflow that goes like this:

  1. Keyword: "pdf" (or Hotkey: CMD + SHIFT + P)
  2. Current Safari page renders in Safari Reader
  3. File > Export as PDF
  4. 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
Posted

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

Posted (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 by dfay
Posted

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. :)

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...