• 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
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Print to PDF


  • Subject: Re: Print to PDF
  • From: Steve McMillen <email@hidden>
  • Date: Tue, 18 Mar 2008 13:55:06 -0700

There was a thread on this list about automating the "Save as PDF" option available in the print dialog of all applications. I original poster suggested maybe using GUI Scripting which is how I did it. But there were some difficult challenges such as selecting the sub-item of the menu button and working with sheets as well as dialogs.

# Saves current document open in foreground app as PDF using the Print dialog "Save as PDF" option

set maxloops to 100

# Open a log file - only used for debugging
# Log file is written to the root of the hard disk
# set fid to open for access "mylog.txt" with write permission

### Set process name based on the frontmost app ###
# This allows the script to run with whatever app is in foreground
# Note: Some apps use a property sheet and some use a window for the print
# Dialog so depending on which we need to take different actions
set process_name to displayed name of (info for (path to frontmost application))
# If needed, you can hard-code the app name by uncommenting the following:
# set process_name "Firefox"
# If we hard-code the name, we should active the app
# activate application process_name


tell application "System Events"
tell process process_name

# Open the print dialog
keystroke "p" using command down

# Wait until the Print dialog opens before proceeding
set prdlg to 0 # Initialize loop variable
set lcnt to 0 # Counter to prevent infinit loops
repeat until prdlg is not 0
# Determine if the app is using a dialog or a sheet for print options and then create reference to dialog or sheet for use later
if exists window "Print" then
# Current App uses the Print dialog (not sheet)
set prdlg to 1
set prdlgref to a reference to window "Print"
set wtype to "dlg"
else if exists sheet 1 of window 1 then
# Current App uses the Print sheet (not dialog)
set prdlg to 1
set prdlgref to a reference to sheet 1 of window 1
set wtype to "sht"
end if
set lcnt to lcnt + 1
if (lcnt) > maxloops then
display dialog "Reached Max Loops waiting for print dialog to open."
return
end if
end repeat


# Expand the "PDF" menu button (must be expanded before the menu is referencable)
click menu button "PDF" of prdlgref


# Wait until the Menu button menu is created before proceeding
set lcnt to 0
repeat until exists menu item "Save as PDF…" of menu 1 of menu button "PDF" of prdlgref
set lcnt to lcnt + 1
if (lcnt) > maxloops then
display dialog "Reached Max Loops waiting for Save as PDF dropdown to open."
return
end if
end repeat


# Select the "Save as PDF" menu item
click menu item "Save as PDF…" of menu 1 of menu button "PDF" of prdlgref

# Wait until the Save dialog opens before proceeding
set lcnt to 0
repeat until exists window "Save"
set lcnt to lcnt + 1
if (lcnt) > maxloops then
display dialog "Reached Max Loops waiting for save dialog to open."
return
end if
end repeat

end tell
end tell
_______________________________________________
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
  • Follow-Ups:
    • Re: Print to PDF
      • From: "Enrique Terrazas" <email@hidden>
  • Prev by Date: Re: InDesign CS2, XML, and graphics
  • Next by Date: How to open application (Preview) at a specific size and location on the screen
  • Previous by thread: Re: extract URL from general text
  • Next by thread: Re: Print to PDF
  • Index(es):
    • Date
    • Thread