Is it possible to automatically print mail messages?

G’day

I’ve been helping someone to automate (rename, duplicate twice, duplicate to server, duplicate to desktop, move to trash), a workflow of incoming mail, (using ‘Mail’), but now he’s expressed a desire to print the automatically selected mail, along with a heading based on it’s new name which in turn is based on the date & time

I’ve scoured these forums, but it doesn’t seem possible.

Does anyone know a way of doing this?

Regards

Santa

Model: G5 1.8 GHz
AppleScript: 2.1.1
Browser: Safari 419.3
Operating System: Mac OS X (10.4)

G’day Santa, be glad to try and help an Aussie out

found this thread:
http://www.macworld.com/forums/ubbthreads/showflat.php?Cat=0&Number=483770&Main=481452

this from that thread works fine for me, I added “set selectedMessages to selection” and tested from my desktop

on perform_mail_action(info)
tell application “Mail”
try
set selectedMessages to |SelectedMessages| of info
–set selectedMessages to selection
repeat with eachMessage in selectedMessages
set theEmail to "From: " & (sender of eachMessage as text) & return & ¬
“Subject:” & (subject of eachMessage as text) & return & ¬
“Date:” & (date sent of eachMessage as text) & return ¬
& return & (content of eachMessage as text)
do shell script “echo "” & theEmail & “" > /tmp/mailcontent.txt”
do shell script “lpr /tmp/mailcontent.txt”
do shell script “rm /tmp/mailcontent.txt”
end repeat
on error TheError
display dialog "Problem: " & TheError
end try
end tell
end perform_mail_action

this UI script works ok to:

on run
tell application “Mail”
activate
end tell
tell application “System Events” to tell process “Mail”
keystroke “p” using command down
delay 1
tell window “print”
delay 1
keystroke return
end tell
end tell
end run

Budgie

G’day Budgie

Thanks for that. Unfortunately the first shell script doesn’t work for me. No errors, just doesn’t print. I backtracked all the links, and some people have had a problem with the routine. As well (and I don’t know shell scipting from my backside) it seems to save to a text file, prints, then deletes the file. I need to keeo rtf intact, as well as embedded attachments.

The second method works fine for selected messages, but I can’t work out how to select the actual message I want to print. I’m using a mail rule action to shift certain incoming mail to a folder, where an attached folder action deals with the mail. I now need to print that mail ‘as received’.

Here’s what I’ve tried (amongst many other things) this bits the problem

– How do I select the required message to print it?
select AMessage

Regards

Santa


(*    Mail Item mover

Use as a Folder Action attached to a dedicated Mail folder, however
NOTE that the script must be attached to the 'Messages' folder 
INSIDE the ACTUAL Mail Folder.

The path to the folder is

Username/Library/Mail/Mailboxes/Name_of_your_mailbox/Messages

When mail is dropped on, or shifted into, the Mail folder, they are copied to... 

1/ a yearly storage folder on the server, with sub-folders based on the 
senders name & eMail addresses

2/ a daily, date-orientated folder on the server, with sub-folders
based on senders name & eMail addresses.

3/ a daily folder on the Desktop

The dropped messages are then moved to the Trash folder, to avoid the 
dedicated shifting folder from becoming too full, and slow.

By Santa
Version 1.3
*)
-- Alter this property to reflect the name of the Mail box folder
property MailBoxName : "* items to shift"

global ServerFolder
global destination_folder
global ServerFolder
global YearlyStorage


on adding folder items to this_folder after receiving added_items
	tell application "Finder"
		try
			set ServerFolder to (folder "!preflight_art" of folder "Art Dept" of disk "Internal Backup") as alias
			
			using terms from application "Mail"
				tell application "Mail"
					repeat with AMessage in added_items
						set TheName to my getName(AMessage as string) as string
						repeat with CycleThroughMessages in messages of mailbox MailBoxName
							if (id of CycleThroughMessages & ".emlx" as string) is equal to TheName then
								set theSource to (the sender of CycleThroughMessages) as string
								-- lines added to keep server happy
								if length of theSource > 30 then
									set theSource to text 1 thru 30 of theSource
								else
									set theSource to text 1 thru end of theSource
								end if
								set theSubject to (the subject of CycleThroughMessages) as string
								exit repeat
							end if
						end repeat
						set ShiftToTrash to false
						tell application "Finder"
							try
								set tempDate to the current date
								
								my SetUpFolders(tempDate)
								
								set temp3 to day of tempDate
								if temp3 < 10 then set temp3 to "0" & temp3
								set temp4 to time of tempDate
								set temp5 to temp4 div 3600
								set temp6 to (temp4 - (temp5 * 3600)) div 60
								set temp7 to temp4 - (temp5 * 3600) - (temp6 * 60)
								if temp5 < 10 then set temp5 to "0" & temp5
								if temp6 < 10 then set temp6 to "0" & temp6
								if temp7 < 10 then set temp7 to "0" & temp7
								
								--NOTE : this changes name AND EXTENSION
								set DateTimeName to (year of tempDate & " " & month of tempDate & " " & temp3 as string) & " " & temp5 & "." & temp6 & "." & temp7 & ".eml"
								set FolderDateName to (year of tempDate & " " & month of tempDate & " " & temp3 as string)
								-- Check and create DAILY DESKTOP folder
								if not (exists folder FolderDateName of desktop) then
									make new folder at desktop with properties {name:FolderDateName}
								end if
								if not (exists folder theSource of folder YearlyStorage of folder ServerFolder) then
									make new folder at folder YearlyStorage of folder ServerFolder with properties {name:theSource}
								end if
								if not (exists folder theSource of folder destination_folder) then
									make new folder at folder destination_folder with properties {name:theSource}
								end if
								
								-- Do any desired stuff to message here, before moving (actually copying)
								
								--Now copy to Yearly storage
								move AMessage to folder theSource of folder YearlyStorage of folder ServerFolder
								set TheName to my getName(AMessage as string)
								set theFilePath to (folder theSource of folder YearlyStorage of folder ServerFolder) as string
								set theFilePath to theFilePath & TheName
								tell application "Finder" to set name of file the theFilePath to DateTimeName
								
								--Now copy to Daily storage
								move AMessage to folder theSource of folder destination_folder
								set theFilePath to (folder theSource of folder destination_folder) as string
								set theFilePath to theFilePath & TheName
								tell application "Finder" to set name of file the theFilePath to DateTimeName
								
								--Now copy to Desktop
								duplicate AMessage to folder FolderDateName of desktop
								
								tell application "Mail"
									activate
									try
										tell application "System Events" to tell process "Mail"
											
											-- How do I select the required message to print it?
											select AMessage
											
											keystroke "p" using command down
											delay 1
											tell window "print"
												delay 1
												keystroke return
											end tell
										end tell
									on error TheError
										display dialog "Problem: " & TheError
									end try
								end tell
								
								set ShiftToTrash to true
								delay 1
							end try
						end tell
						if ShiftToTrash then
							set mailbox of CycleThroughMessages to mailbox "Trash"
						else
							display dialog "The message '" & theSubject & "' could not be moved."
						end if
					end repeat
				end tell
			end using terms from
		on error errMsg number errNum
			display dialog "An error has occurred. Please email me at my contact email address at MacScripters, with the error number and message." & return & return & "Regards, Santa" & return & return & "Error number : " & errNum & return & return & errMsg buttons {"OK"}
		end try
	end tell
end adding folder items to

on SetUpFolders(TheDate)
	tell application "Finder"
		try
			set YearlyStorage to "Yearly Storage " & (year of TheDate) as string
			
			set tempYear to (year of TheDate) as string
			set tempMonth to tempYear & "/" & (month of TheDate) as string
			set tempDay to (day of TheDate)
			if tempDay < 10 then set tempDay to "0" & tempDay
			set tempDay to tempMonth & "/" & (tempDay) as string
			
			--Check and create YEAR of Storage folder
			if not (exists folder YearlyStorage of folder ServerFolder) then
				make new folder at folder ServerFolder with properties {name:YearlyStorage}
			end if
			--Check and create YEAR folder
			if not (exists folder tempYear of ServerFolder) then
				make new folder at folder ServerFolder with properties {name:tempYear}
			end if
			--Check and create MONTH folder
			if not (exists folder tempMonth of folder tempYear of ServerFolder) then
				make new folder at folder tempYear of folder ServerFolder with properties {name:tempMonth}
			end if
			--Check and create DAY DATE folder
			if not (exists folder tempDay of folder tempMonth of folder tempYear of ServerFolder) then
				make new folder at folder tempMonth of folder tempYear of folder ServerFolder with properties {name:tempDay}
			end if
			-- Now save new path as a variable to speed things up
			set the destination_folder to (folder tempDay of folder tempMonth of folder tempYear of ServerFolder) as alias
		end try
	end tell
end SetUpFolders

on getName(TheName)
	set KeepDelimiters to AppleScript's text item delimiters
	set AppleScript's text item delimiters to {":"}
	if TheName > "" then
		set TheName to (text items -1 thru end of TheName)
	end if
	set AppleScript's text item delimiters to KeepDelimiters
	return TheName
end getName


G’day again

I’ve cobbled together a script that prints the actual mail file as is, but still need to prefix it with a header.

Does anyone know of a way of including an extra line of print in front of this please?

I’ve tried altering the Subject, but it won’t print, even though it shows as altered briefly on the screen.

Thanks, & regards

Santa



tell application "Mail"
									activate
									try
										set the_messages to (every message of mailbox MailBoxName)
										set mess_viewer to first message viewer
										set selected mailboxes of mess_viewer to {mailbox MailBoxName}
										repeat with this_mess in the_messages
											if (id of this_mess & ".emlx" as string) is equal to thePrintName then
												set selected messages of mess_viewer to (this_mess as list)
												tell application "System Events" to tell process "Mail"
													keystroke "p" using command down
													delay 1
													tell window "print"
														delay 1
														keystroke return
													end tell
												end tell
											end if
										end repeat
									on error TheError
										display dialog "Printing problem: " & TheError
									end try
								end tell

Model: G5 1.8 GHz
AppleScript: 2.1.1
Browser: Safari 419.3
Operating System: Mac OS X (10.4)

Hi Santa,

altering the subject of an incoming mail is not possible

Thank you Santa. Your script above taught me how to select an email message in Mail, and that trick solved my problem here: http://bbs.applescript.net/viewtopic.php?pid=79626#p79626

Here’s a thought for your problem…

If you look at the properties of a message, you will find all the information you need… meaning the subject and the content of the email message itself is in the properties. If you really need to add a line you can probably do it by getting the properties you need and writing that (along with your extra info) to a text file and then printing the text file instead of the email, basically reconstructing the email message with the extra information in a text file. It would take some scripting skills to pull it off but I’m sure it’s doable.

Thanks to everyone who answered, especially Budgie, whose routines I finally got to work (for some reason the repeat loop jammed the print queue)

Now I’ve got another question… In this routine…


 set theEmail to "From: " & (sender of eachMessage as text) & return & ¬
                    "Subject:" & (subject of eachMessage as text) & return & ¬
                    "Date:" & (date sent of eachMessage as text) & return ¬
                    & return & (content of eachMessage as text)
                do shell script "echo \"" & theEmail & "\" > /tmp/mailcontent.txt"
                do shell script "lpr /tmp/mailcontent.txt"
                do shell script "rm /tmp/mailcontent.txt"
       

is it possible to make the size of the first printed line larger, and bold?

Regards

Santa

Model: G5 1.8 GHz
AppleScript: 2.1.1
Browser: Safari 419.3
Operating System: Mac OS X (10.4)

I improved the code using the shell a bit because the script deletes the file to be printed before the printer received all data. That may have caused the problems mentioned (imho it was not caused by the repeat loop).

After

insert


	tell application "Printer Setup Utility"
		
		-- My PDF-Printer in this case! Just check your Printer Setup Utility for the correct printer number!
		set _printer to printer 2
				
		repeat
			if status of _printer = idle then exit repeat
		end repeat
				
	end tell

before

Best wishes!
Thomas

Model: Mac-Mini 1.42 G4 1GB RM
Browser: Firefox 2.0.0.11
Operating System: Mac OS X (10.4)