• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag
 

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: print to PDF with applescript?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: print to PDF with applescript?


  • Subject: Re: print to PDF with applescript?
  • From: Simon Forster <email@hidden>
  • Date: Mon, 27 Apr 2015 16:42:15 +0100


On 27 Apr 2015, at 16:00, Dee Dee <email@hidden> wrote:

I am using Mavericks 10.9.5.

Oh. I use 10.10.x

I notice on my print dialog window, that when I click on the PDF button, the menu that drops down offers the option to edit menu (meaning that I can add printing workflows to my menu.)

So it does.

What I would like to do is add an option to output to PDF to a particular folder.
There are already options to send PDF to a couple of particular folders such as web receipts.  

I know Automator offers the ability to create a printer workflow, but I would like to avoid using Automator.
Can anyone point me in the correct direction to write an applescript that I can achieve my goal?

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.


————————
—— START
————————

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
————————
 _______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users

This email sent to email@hidden

References: 
 >print to PDF with applescript? (From: Dee Dee <email@hidden>)

  • Prev by Date: Re: print to PDF with applescript?
  • Next by Date: Re: Can't get static text
  • Previous by thread: Re: print to PDF with applescript?
  • Next by thread: Re: print to PDF with applescript?
  • Index(es):
    • Date
    • Thread