Applescript to automate "Save as PDF" in print menu

Hello,

I’ve spent some time searching for a solution but haven’t found anything, hence this post. Here’s what I would like to do…I often find myself using the “save as PDF” option in the print menu to archive things that I do online (bank receipts, order confirmations, etc…) I save the PDFs to either of two places, a personal records folder for my personal transactions, and a business folder for my business transactions.

I am trying to write an applescript that I could select from the script menu that would:

1 - Open the print menu (cmd+p)
2 - Choose “Save as PDF.” from the print menu
3 - Prompt me to send it to either my personal or business folder
4 - Save the PDF to a subfolder for the current year (HD/users/username/docs/personal_records/2011)

I am new to applescripting, all I’ve been able to do so far is get the print menu of the current application to open. I did that with this code:

tell application "System Events"
keystroke "p" using {command down} -- activate print menu item
end tell

Any ideas on how to automate the selection of “Save as PDF” or how to dictate the save location based on the current year?

Your help is greatly appreciated!

This (still) works (from some apps) on 10.4.11, but more likely than not WON’T on later systems… and must be invoked from a keyboard command. Perhaps you can adapt it to your needs.

Peter B.



try
	
	tell application "System Events"
		
		set target_process to 1st application process whose frontmost is true and visible is true
		
		delay 1
		
		tell target_process
			
			keystroke "p" using command down
			
			set PDF_button to UI element 6 of UI element 4 of sheet 1 of window 1 of target_process
			
			set {ButtonLeft, ButtonTop} to position of PDF_button
			set {ButtonWidth, ButtonHeight} to size of PDF_button
			set clickX to ButtonLeft + (ButtonWidth div 2)
			set clickY to ButtonTop + (ButtonHeight div 2)
			
			click at {clickX, clickY}
			
			delay 1
			
			keystroke (ASCII character 31)
			keystroke return
			
			delay 1.4
			
			keystroke "d" using command down
			delay 0.2
			keystroke (return)
			
		end tell
	end tell
	
on error
	
	try
		
		tell application "System Events"
			
			set target_process to 1st application process whose frontmost is true and visible is true
			
			delay 1
			
			tell target_process
				
				keystroke "p" using command down
				
				set PDF_button to UI element 1 of UI element 3 of UI element 1 of sheet 1 of window 1 of target_process
				set {ButtonLeft, ButtonTop} to position of PDF_button
				set {ButtonWidth, ButtonHeight} to size of PDF_button
				set clickX to ButtonLeft + (ButtonWidth div 2)
				set clickY to ButtonTop + (ButtonHeight div 2)
				
				click at {clickX, clickY}
				
				delay 1
				
				keystroke (ASCII character 31)
				keystroke return
				
				delay 1.4
				
				keystroke "d" using command down
				delay 0.2
				keystroke (return)
				
			end tell
		end tell
		
	end try
	
end try

Thanks Peter,

I started with the script you provided and now have it so that it saves the current open item as a PDF to the desktop. Here is the script to do that:


try
	tell application "System Events"
		
		set target_process to 1st application process whose frontmost is true and visible is true
		
		delay 1
		
		tell target_process
			
			--open the print menu
			keystroke "p" using command down
			
			--identify the "PDF" drop down menu
			set PDF_button to UI element 4 of sheet 1 of window 1 of target_process
			
			--learn the coordinates of the PDF drop down menu and click it
			set {ButtonLeft, ButtonTop} to position of PDF_button
			set {ButtonWidth, ButtonHeight} to size of PDF_button
			set clickX to ButtonLeft + (ButtonWidth div 2)
			set clickY to ButtonTop + (ButtonHeight div 2)
			
			click at {clickX, clickY}
			
			--after the menu drops down, click the second option "Save as PDF."
			
			set click_a to clickX + 15
			set click_b to clickY + 40
			
			click at {click_a, click_b}
			
			--change save location to the desktop
			keystroke "d" using command down
			
			--place cursor after the default filename
			keystroke (ASCII character 29)
			
			--need to learn how to append the current date at the end of the filename here
			
			--press enter to save
			keystroke (return)
			
		end tell
	end tell
end try

Now, I am trying to figure out how to have the script append the filename with the current date, and how to set the save location to a specific folder i.e. (docs/archive/personal/2011).

Does anyone know how to script the logic that would:

prompt me to select, personal or business
grab the current date (the year specifically)
append the filename with the current date (MM/DD/YYY)
change the save location based on the date

I’m pretty sure I can figure out how to get the user prompt to work. I’ll post an update when I get it working. Anybody have any suggestions for working with the date?

Thanks!

Check back with MacRumors.

MacGrunt gave you a few pointers there.