Canadian Mac Forums at ehMac banner

1 - 17 of 17 Posts

·
Registered
Joined
·
101 Posts
Discussion Starter · #1 ·
For a friend, I'm looking for the for or five lines of Applescript needed to tell a program like SAFARI to take the current window and "Print to PDF" using the standard PRINT procedure. Should be just a few lines of Applescript hopefully.

Can anyone help out with this? I've Googled around and keep finding everything BUT this... There's even an open question on precisely this topic at the Apple discussion forum site... Not answered in over a month... What's up?

If it means adding some extra Applescript commands via a "bundle" from some code guru, that's okay too.

I'm surprised it isn't apparent that you can just do this with Automator's included tasks.

Oh, and why is there a "RECORD" button in the Script Editor for Applescript but it doesn't record anything! How useless is that!

Anyway, any and all help will be much appreciated.
 

·
Premium Member
Joined
·
7,069 Posts
Going File-> Print and then choosing the PDF button and choosing "Save as PDF..." doesn't work?

That said, if an AppleScript is made you can enable the Scripts menu making it available within nearly any application.
 

·
Premium Member
Joined
·
1,481 Posts
Calgary Guru said:
Oh, and why is there a "RECORD" button in the Script Editor for Applescript but it doesn't record anything! How useless is that!

Anyway, any and all help will be much appreciated.
Software developers have to do extra work to have their AppleScript-aware applications be recordable. Most don't do the needed extra work to allow this to function properly.
 

·
Premium Member
Joined
·
5,247 Posts
You need to learn the syntax and write applescripts. The "record" function is really just a starting point; go ahead and record the script but you will have to edit it anyway. I don't think I've ever been able to just record and go with an AppleScript. But, record does do a lot of the basics for you, and even sometimes you finish a script and notice that not a line of the original recorded script remains, it still pointed you in the right direction when it comes to hinting at what needs to be modified.

If you have 10.4x try Automator; it has a lot of functions built in, and it can run AppleScripts as well as combine Automator actions with scripts.

Poke around here:
AppleScript Net and especially:
AppleScript Source Book
And perhaps Mac DevCentre at O'Reilly.
 

·
Registered
Joined
·
101 Posts
Discussion Starter · #6 ·
Lots of obvious suggestions that I've already tried... I'm looking for an example of the SPECIFIC Applescript syntax for this. I guess very few people out there do Applescript it seems.

Now why isn't there an included Automator task that says "print page as PDF" so one could automate programs such as Safari quite easily!
 

·
Indigent Academic
Joined
·
8,115 Posts
Calgary Guru said:
Lots of obvious suggestions that I've already tried... I'm looking for an example of the SPECIFIC Applescript syntax for this. I guess very few people out there do Applescript it seems.

Now why isn't there an included Automator task that says "print page as PDF" so one could automate programs such as Safari quite easily!
In a plain vanilla 10.4.8 install go to HD > Library > PDF Services > Save As PDF-X.workflow - might be a clue in that.
 

·
Registered
Joined
·
101 Posts
Discussion Starter · #8 ·
I guess there's always QuickKeys... Still available and now fully OS X... but $80US.

I was looking at Automator again... Apple has essentially neglected this feature of OS X... Where's the additional action scripts with each OS X update? For Safari, a major program, there's just a few... and none to do with printing.
 

·
Registered
Joined
·
101 Posts
Discussion Starter · #9 ·
rgray said:
In a plain vanilla 10.4.8 install go to HD > Library > PDF Services > Save As PDF-X.workflow - might be a clue in that.
Those are just Automator workflows to work with EXISTING PDF documents... Nothing to help you generate a PDF document...

Is there a web site out there with a bundle of "extra" Automator workflows?
 

·
Registered
Joined
·
4 Posts
Me Too

I too am looking for a similar capability. Obviously opening the print dialog is easy in Applescript; but how to select the "Save As PDF" in the Print dialog?

I can't seem to find any doc for the print dialog. Is it at all scriptable and if so, under what application?

Or is there some standard Applescript way to click a menu button?

Hope someone out there has some clue for how to proceed.

Thx, Steve
 

·
Registered
Joined
·
523 Posts
This should be simple with UI scripting, but turns out it isn't. The very long applescript in this post may work for you:

MacScripter BBS | Applescript Forums / Save as PDF

However, a different approach might be to use a command-line tool to do the job, like this one (it takes a URL and converts to pdf):

wkpdf - Trac

Automator could take the URL from the page you are looking at in Safari and send it to a shell script to do the conversion.

Good luck!
 

·
Registered
Joined
·
4 Posts
Found Solution

Thanks for the previous reply. I ended up figuring out a solution. Actually, spent a good deal longer trying to see if I could detect the presence of a modifier key so I can either choose to automatically save or allow me to choose a filename and/or location. But I sorta gave up on the later.

Here is the code to select the "Save as PDF..." option from the Print dialog:

Code:
# Saves current document open in Firefox as PDF
activate application "Firefox"
tell application "System Events"
	tell process "Firefox"
		# Open the print dialog
		keystroke "p" using command down

		# Wait until the Print dialog opens before proceeding
		repeat until exists window "Print"
		end repeat
		# Expand the "PDF" menu button (must be expanded before the menu is referencable)
		click menu button "PDF" of window "Print"

		# Wait until the Menu button menu is created before proceeding
		repeat until exists menu item "Save as PDF…" of menu 1 of menu button "PDF" of window "Print"
		end repeat
		# Select the "Save as PDF" menu item
		click menu item "Save as PDF…" of menu 1 of menu button "PDF" of window "Print"
		
		# Wait until the Save dialog opens before proceeding
		repeat until exists window "Save"
		end repeat
		
		# Paste the contents of the clipboard in and Save
		# This is sorta hack; Probably best to leave the 'Save As" dialog open and let the user finish it off but I have a special purpose
		if (get (the clipboard) is not "") then
			set value of text field 1 of window "Save" to get (the clipboard) & ".pdf"
		end if
		click button "Save" of window "Save"
		
	end tell
end tell
 

·
Registered
Joined
·
4 Posts
My last post only works for applications that use the print dialog window instead of the sheet and it was hard-coded to Firefox. I generalized to work with [I think] any app it and figured I'd post that as well:

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

# value to limit how long we wait for a given dialog or menu popup
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
 

·
Registered
Joined
·
2 Posts
Hi Steve,

I tried this code of yours but got the error "System Events got an error: NSReceiverEvaluationScriptError: 4" at instruction "click menu button "PDF" of prdlgref". That's about the same error I got before finding your script. What I ended up doing was to do a lot of "keystroke"s commands as clicking that elusive button wasn't possible.

I'm using too firefox and MacOSX 10.4.11 and enabled the check "Enable access for assistive devices". Sounds like this is a tricky button.
 

·
Registered
Joined
·
4 Posts
Alternative Solution

Make sure that the text of the button you pasted in has ellipses and not just three dots. Apparently you need to match the character exactly.

I learned of an alternative solution recently. Here is what I was told:

The latest issue of Macworld shows how to assign a keyboard shortcut to items in the PDF popup if you are running Leopard. Basically, you add one for all applications in the keyboard preference, and make sure to list the item name as it appears in the popup.
I ended up creating a keyboard shortcut to "Save as PDF..." to Cmd-P in the Apple Keyboard Prefs panel. So then I just need to type Cmd-P twice and I get the Save as PDF dialog :D
 

·
Registered
Joined
·
2 Posts
Yep, I made sure using ellipses and all. I also tried three dots and opt-semicolon, uppercase, lowercase, several letters, restarted my mac, restarted Firefox only, and not one worked from Applescript. Even if I do it myself, I have to position onto the PDF button (tab-ing all the way) and then hit the space bar and then cmd-p works. Also, I tried using UIElementInspector but couldn't decipher the correct clicking sequence.
Maybe is my Applescript version (1.10.7) or the printing version that doesn't understands Applescript very well.
 

·
Premium Member
Joined
·
17,780 Posts
For a friend, I'm looking for the for or five lines of Applescript needed to tell a program like SAFARI to take the current window and "Print to PDF" using the standard PRINT procedure. Should be just a few lines of Applescript hopefully.
Have you tried macosxhints.com - OS X tips and tricks!

Oh, and why is there a "RECORD" button in the Script Editor for Applescript but it doesn't record anything! How useless is that!
Hey it used to (and still does) work in OS 9. As far as I can recall it has never worked in OS X. Like the Macros in AppleWorks it is one of those things the Apple wizards didn't want to be bothered with.
 
1 - 17 of 17 Posts
Top