printing mail messages to PDF workflow

I am trying to create a Mail Rule that will do two things:

  1. Move any new messages to a specific mailbox
  2. Run an Applescript that will look for all unread messages in that mailbox, print them to a PDF workflow, and then mark the messages as read.

I am having trouble getting my Applescript to work properly. When I run it, it fails in a couple of places. First, it does not open up the print dialog when prompted on line 11. However, if I manually open the print dialog and then run the script, it will then work partially. It will print the first message, but will not print any more if there is more than one unprinted message. I put the display dialog in at line 21 to see what was happening and it shows messageCounter has a value of 1 even if there are more messages. I put a display dialog in between line 20 and 21 that was “display dialog messageCounter” and it returned a value of 2 when there was 2 messages. Can someone provide some insight into what I am doing wrong? Thanks!

tell application "Mail"
	set selected mailboxes of message viewer 1 to {mailbox "Receipts" of account "x"}
	set firstMessage to message 1 of mailbox "Receipts" of account "x"
	set read status of firstMessage to false
	set newMessages to (messages of mailbox "Receipts" of account "x" whose read status is false)
	set messageCounter to number of items in newMessages as string
	repeat with i from 1 to messageCounter
		set currentMessage to item i of newMessages
		tell application "System Events"
			tell process "Mail"
				click menu item "Print." of menu "File" of menu bar item "File" of menu bar 1
				repeat until exists window 1
				end repeat
				set pdfWindow to sheet 1 of window 1
				click menu button "PDF" of pdfWindow
				repeat until exists menu item "Send out" of menu 1 of menu button "PDF" of pdfWindow
				end repeat
				click menu item "Send out" of menu 1 of menu button "PDF" of pdfWindow
			end tell
		end tell
		display dialog "Message #" & i & " of " & (count of messageCounter) & " complete!"
		set i to i + 1
	end repeat
 end tell

Stripping the line numbers out of your script would be most helpful.

Sorry, I new to this. I thought I was being helpful by putting line numbers in. Thanks.

I just showed someone how to print to pdf in mail. You can find that in post #7 here. You should be able to adjust that for your needs. After trying that, let us know if you have further questions. Good luck.

Ok, so I reworked the script and now it is printing the correct number of messages that apply to the rule. The problem is that it is not printing the messages that apply to the rule. Instead, it is printing the message that is selected in Mail before the rule was applied for the count of the messages that apply to the rule.

Here is the script as it now stands:

using terms from application "Mail"
	on perform mail action with messages newMessages
		tell application "Mail"
			set messageCounter to (count of newMessages)
			repeat with i from 1 to messageCounter
				set currentMessage to item i of newMessages
				tell application "System Events"
					tell process "Mail"
						click menu item "Print." of menu "File" of menu bar item "File" of menu bar 1
						repeat until exists sheet 1 of window 1
						end repeat
						set pdfWindow to sheet 1 of window 1
						click menu button "PDF" of pdfWindow
						repeat until exists menu item "Send" of menu 1 of menu button "PDF" of pdfWindow
						end repeat
						click menu item "Send" of menu 1 of menu button "PDF" of pdfWindow
					end tell
				end tell
			end repeat
		end tell
	end perform mail action with messages
end using terms from

This kind of script is called a gui script. It uses menu item presses and button clicks to perform a task rather than Mail’s applescript commands. As such it works on whatever is in the frontmost window of Mail at the time of operation. You can’t target a specific message to print. It just prints whatever is in the window.

See in your code where you have currentMessage. You never do anything with that message. The print stuff is printing the front window, not the current message. So you need to make the currentMessage come forward before you start the printing commands. So you’ll have to set the selection in the front window to each message before you print it. Unfortunately I do not have any code for that offhand. Do some searching and see if you can find such code.

Is there a way to write the script so that it is not a gui script? I would prefer that, as my goal was to have the script run in the background without windows popping up and changing.

I did realize that currentMessage was set and not used. I just do not know how to select the message. I have been searching and tried some things that did not work- the last being ‘set selected messages to {currentMessage}’. So far I have been unsuccessful and my searches are not yielding any results that either work or perhaps I just do not understand.

Thanks for the help so far!

Gui scripting is the only way to print-to-pdf. However, you can make text into a pdf, so if you could accept just getting the text of the message and turning that into a pdf (or text file) then that would work how you want. That would be pretty simple to do.

Regarding selecting the message (if you continue with the gui script) that is difficult to do. If you look in the applescript dictionary of Mail you will see that “selection” is a read-only property. As such you would have to develop some sort of gui script to make a selection… again very difficult and not very reliable.