Applescript to invoke specific Print to PDF option?

Hello,

Have any of you come across or written a script that would make it possible to invoke a specific custom Print to PDF menu option?

I have a custom print to pdf option called “ACTION”, which simply puts the pdf in a folder with that name. In order to send a document to it, I type Command + p, click on PDF and then click on ACTION. I would like to be able to accomplish this only using the keyboard, e.g. typing Command + Shift + p or Command + p and then Command + Shift + p.

I have tried Keyboard Shortcuts in Preferences but could not accomplish my goals. Perhaps an Applescript with GUI Scripting could accomplish this? The script below, in conjunction with FastScripts or a similar application, accomplishes something along the same lines: it allows one to have Mail Load Images using a keystroke shortcut.

Any thoughts?

Thank you,
Erik


(*

Click the Load Images button in an open Mail message.
This is to work around Mail's idiotic inability to Load Images via keyboard shortcut.
Attach this to a keystroke using FastScripts, Keyboard Maestro, or some other utility.

Written May 2013 by Michael A. Alderete
Project URL: http://aldoblog.com/link/load-images-applescript

Version History
1.0 -- Initial release, 2013-05-06
1.0.1 -- Consolidate reference to button to a single line, instead of two separate references (never released)
1.1 -- Add OS version detection, use right reference for Mail.app version (never released)
1.2 -- Refactor the OS detection and button reference (never released)
1.3 -- Use a cascade of statements to get various Mail version/config permutations, release 2013-05-27

Use as you please, give credit if you like.

*)

-- You can comment this out after you run it once, for slightly faster performance.
-- It just makes sure you really have GUI Scripting enabled.
GUIScripting_status()

tell application "System Events"
	tell process "Mail"
		try
			-- Verify there is a window where it's possible to load images
			-- From <https://discussions.apple.com/message/11234439#11234439>
			if not (window 1 exists) then return beep 1
			
			set loadImagesButton to my getLoadImagesButtonReference()
			
			-- Make sure there's a [Load Images] button in the window
			if not (loadImagesButton exists) then return beep 1
			
			-- Actually click the button
			click loadImagesButton
		end try
	end tell
end tell

-- Check to see if assistive devices is enabled
-- From <http://www.macosxautomation.com/applescript/uiscripting/>
on GUIScripting_status()
	tell application "System Events"
		set UI_enabled to UI elements enabled
	end tell
	if UI_enabled is false then
		tell application "System Preferences"
			activate
			set current pane to pane id "com.apple.preference.universalaccess"
			display dialog "This script uses the Graphic User Interface Scripting architecture of Mac OS X, which is currently disabled." & return & return & "Enable GUI Scripting by checking \"Enable access for assistive devices\" in the Accessibility preference pane." with icon 1 buttons {"OK"} default button 1
		end tell
	end if
end GUIScripting_status

-- Get a reference to the [Load Images] button
-- The UI specifier path to the button changes depending on which version of Mail.app is running,
-- whether the message open in a preview pane or independent message window, and
-- whether you have the "classic" layout enabled (luddites unite!), or use the new sideways layout (like an animal).
on getLoadImagesButtonReference()
	
	tell application "System Events"
		tell process "Mail"
			
			if (my isBeforeLionMail()) then
				try
					-- Older versions of Mail.app use a simpler UI specifier
					-- From: https://discussions.apple.com/thread/2405934
					set loadImagesButtonRef to a reference to button "Load Images" of front window
				end try
			else if (my isLionMail()) then
				-- Cascade through a series of attempts to get the button reference
				try
					-- The [Load Images] button in an independent message window
					-- Found with [UI Browser](http://pfiddlesoft.com/uibrowser/).
					-- Also described here, but with a different scripting intent: 
					-- http://superuser.com/questions/421215/mail-app-showing-remote-images-automatically-for-specific-senders
					set loadImagesButtonRef to a reference to button ¬
						"Load Images" of UI element 1 of row 1 of table 1 of scroll area 1 of front window
				end try
				if not (loadImagesButtonRef exists) then
					try
						-- The [Load Images] button in a mailbox preview pane when in "classic layout"
						-- Found with [UI Browser](http://pfiddlesoft.com/uibrowser/).
						set loadImagesButtonRef to a reference to button ¬
							"Load Images" of UI element 1 of row 1 of table 1 of scroll area 2 of splitter group 2 of splitter group 1 of front window
					end try
				end if
				if not (loadImagesButtonRef exists) then
					try
						-- The [Load Images] button in a message displayed in a mailbox preview pane when in new (non-"classic") layout
						-- [Suggested by John Waers](http://aldoblog.com/2013/05/load-images-applescript-for-apple-mail/#comment-26329)
						-- and [David Horne](http://aldoblog.com/2013/05/load-images-applescript-for-apple-mail/#comment-26335)
						set loadImagesButtonRef to a reference to button ¬
							"Load Images" of UI element 1 of row 1 of table 1 of scroll area 1 of splitter group 2 of splitter group 1 of front window
						
					end try
				end if
			else
				-- Unsupported version of Mail.app
				return beep 1
			end if
			
			return loadImagesButtonRef
			
		end tell
	end tell
end getLoadImagesButtonReference

-- Mac OS X version detection functions
-- We're not going to attempt to support anything before Tiger (10.4), impossible to test
-- For now, don't worry about next version of Mac OS X (10.9)
on isBeforeLionMail()
	-- From: http://stackoverflow.com/questions/498323/find-mac-osx-version-installed-using-applescript
	set osVersion to system version of (system info)
	if ((osVersion begins with "10.4") or (osVersion begins with "10.5") or (osVersion begins with "10.6")) then
		return true
	end if
	return false
end isBeforeLionMail

-- Lion (10.7) changed the UI specifier for the [Load Images] button
on isLionMail()
	set osVersion to system version of (system info)
	if ((osVersion begins with "10.7") or (osVersion begins with "10.8")) then
		return true
	end if
	return false
end isLionMail


I have written quite a few scripts that involve saving something to PDF. I haven’t distilled it into an all-in-one function yet, but maybe I’ll give it a go if I can get the bugs out of my current project. Anyway, here’s a segment from a recent script that does what you’re talking about:

tell application "System Events" to tell process "Safari"
	keystroke "p" using command down
	delay 1
	tell menu button "PDF" of sheet 1 of front window
		click
		click menu item "Save as PDF." of menu "PDF"
	end tell
	tell window "Save"
		delay 2
		tell splitter group 1 of group 2
			tell outline 1 of scroll area 1
				set rowCount to count of rows
				set clickWorked to false
				repeat with i from 1 to rowCount
					try
						tell row i
							if value of static text 1 is "Desktop" then
								set selected to true
								set clickWorked to true
								exit repeat
							end if
						end tell
					end try
				end repeat
				if clickWorked is false then error
			end tell
			tell scroll area 1 of browser 1
				set selected of static text "Tim" of list 1 of scroll area 1 to true
				delay 0.2
				set selected of static text "no invoice" of list 1 of scroll area 2 to true
			end tell
		end tell
		set value of text field 1 to custName
		click button "Save"
		set end of _processed to custName
	end tell
end tell

Usually, I would run the clickWorked check every time I script a click in the save window. It looks like I forgot to on this one, but I got away with it. The gist of this is to automate the GUI to use the built-in save to PDF function. The catch is, once your script is clicking windows instead of sending commands directly to the application, you lose the ability to provide a POSIX path to where you want the file saved to. My solution is to click through the hierarchy in the window, starting with the lowest level folder in the leftmost column. (Apple has a name for that group, but I forget it. You can drag your destination folder into the list (from a Finder window, not the save window) to expedite things if you like. In my example, the script clicks “Desktop,” then “Tim,” and then “no invoice” and saves the Safari page as custName.pdf. Change those variables to match your needs and give it a go.

Hi,

a convenient way to choose a custom folder in a Save dialog box is to press ⇧⌘G and insert the folder path

In Safari 6 there is a Save sheet instead of a separate window


activate application "Safari"
tell application "System Events" to tell process "Safari"
	keystroke "p" using command down
	repeat until exists sheet 1 of window 1
		delay 0.2
	end repeat
	tell menu button "PDF" of sheet 1 of front window
		click
		delay 0.2
		click menu item "Save as PDF." of menu "PDF"
	end tell
	repeat until exists sheet 1 of sheet 1 of front window
		delay 0.2
	end repeat
	tell sheet 1 of sheet 1 of front window
		keystroke "g" using {command down, shift down}
		repeat until exists sheet 1
			delay 0.2
		end repeat
		set value of text field 1 of sheet 1 to "~/Desktop/Tim"
		click button "Go" of sheet 1
		
		set value of text field 1 to custName
		click button "Save"
		
	end tell
	set end of _processed to custName
end tell