auto print

Hi, I hope someone can help me. I need to create a rule in mail that will print a pdf file found in an incoming mail message to a specific printer. The rule part is fine as all messages coming into that particular mailbox will always contain a pdf file. I usually open the file in preview and print it from there but I would like to automate this process and for the life of me have not been able to figure it out. This would seem to be an easy thing to do but I’ve searched all the scripting sites to no avail. Am I overcomplicating this? I thought I had it done with automator, but it never worked.

Thanks,

Model: imac g5
AppleScript: latest
Browser: Safari 417.9.2
Operating System: Mac OS X (10.4)

gcap:

This is a script I use for incoming paystubs to my office. It saves each single attached pdf to a folder, named for the day’s date, and then prints it out automatically to the named printer.

If this is too much, I would be happy to strip it down for you.

--As each paystub comes in, this should make a new folder (named for the date), rename the file based on the employee's name, and print it out.

using terms from application "Mail"
	on perform mail action with messages The_Messages
		set today_folder to CheckOrMakeFolder((current date)'s short date string) --Gets today's date to make a new folder to file the attached pdf.
		tell application "Mail" to repeat with This_Message in The_Messages
			set PDF_name to text from word 2 to word 3 of (get This_Message's content)
			set PDF_file to today_folder & PDF_name & ".pdf"
			save first mail attachment of This_Message in PDF_file --Saves pdf in folder with today's date.
			my printFile(PDF_file) --Prints out pdf to specific printer
		end repeat
	end perform mail action with messages
end using terms from
----------------------------------------------------------------------
on CheckOrMakeFolder(date_string)
	set dp to (path to documents folder as Unicode text) & "Cathouse:Payroll:Paystubs:" & date_string & ":"
	tell application "Finder" to if not (exists folder dp) then do shell script "mkdir -p " & quoted form of POSIX path of dp
	dp
end CheckOrMakeFolder
----------------------------------------------------------------------
to printFile(b)
	set Print_er to "Deskjet_5400_series" --Must use underscores
	set file_to_print to POSIX path of (b as Unicode text)
	try
		do shell script "lpr -P " & (quoted form of Print_er) & " " & (quoted form of file_to_print)
	end try
end printFile

Thanks,
I’m not vry good at scripting but will try to integrate it at the Office tomorrow,

Gino

Gino:

Don’t worry, and don’t get a headache. If it is too daunting, come on back with whatever you have, and we can all help put it together to work for you.

The main information needed would be where to store the pdf attachments once received in Mail. The files need to be saved somewhere so that they can be sent to the printer.

You may also need to go into the Terminal to find the correct name for the printer that you want these documents printed with. If you look through this thread, you will see how to find the proper name of the printer to use in the PrintFile handler.

If you can provide that data, you should have a nifty and useful script by the end of the day.

I use this fragment to choose a printer:

tell application "Printer Setup Utility"
	activate
	set myPrinters to every printer
	
	-- you can find out which is default:
	
	set DefaultPrinter to current printer
	
	-- then if you know which item in that list is "your" printer or if the current default is ok:
	
	set current printer to item k of myPrinters
	
	-- you can also set this by name:
	
	set current printer to printer "hp LaserJet 1012" of application "Printer Setup Utility"
	quit -- get rid of it
end tell

Craig’s method has the advantage of not opening an application.

hey,

im new to applescript and just macs in general

i’ve been messing around trying to make an applescript like this work. but so far no luck. what i basically want it to do is check a rule in mail.app and if it matches that rule. it prints out automatically, can any of you kindly walk me through these steps so that i understand the process and in the end have a working script?

thanks.

and here is what i have so far:


using terms from application "Mail"
	on perform mail action with messages theMessages for rule theRule
		tell application "Mail"
			set Print_er to "WCP40"
			set file_to_print to theMessages
			try
				do shell script "lpr -P " & (quoted form of Print_er) & " " & (quoted form of file_to_print)
			end try
		end tell
	end perform mail action with messages
end using terms from

you’ll probably laugh at how wrong i am :frowning: ahah

One other small note. If you are printing these to a printer that has letter and tabloid size paper, the shell “lpr” command may default to printing on tabloid paper size. I had this problem and dug up the option to force letter size. Add “-o media=letter” to the lpr line.

do shell script "lpr -P " & (quoted form of Print_er) & " " & (quoted form of file_to_print) -o media=letter

Model: Mac G5 OS 10.3.9
Browser: Safari 312.3.1
Operating System: Mac OS X (10.3.9)

another interesting discovery, in testing the sample rule script that apple provides. it crashes my mail.app. any thoughts on that as well? haha

thx matt. ill keep that in mind, the printer stuff seems to be tricky in applescript.

ok i got rid of the crashing, but now i cant get the print line to work…i can get it to display a dialog but it doesnt print or anything…i dont know if theMessages is the wrong term to be using since I’m wanting to print the message…thanks


using terms from application "Mail"
	on perform mail action with messages theMessages
		tell application "Mail"
			my printFile(theMessages)
		end tell
	end perform mail action with messages
end using terms from

to printFile(theMessages)
	set Print_er to "hp_color_LaserJet_2550_series"
	set file_to_print to theMessages
	try
		do shell script "lpr -P " & quoted form of Print_er & " " & quoted form of file_to_print
	end try
	display dialog "yep"
end printFile

It is most likely due to a misunderstanding (at the shell level) of a quirk in the type of data sent by Mail. When Mail rules trigger an AppleScript, the message is sent in a list, even if only 1 message is contained therein. In your form of the script, you are sending the list to the printFile handler instead of the message inside of the list. This should fix it, by looping through the list (even if it is only 1 item):


using terms from application "Mail"
	on perform mail action with messages theMessages
		tell application "Mail" to repeat with a_Message in the messages
			my printFile(a_Message)
		end repeat
	end perform mail action with messages
end using terms from

to printFile(file_to_print)
	set Print_er to "hp_color_LaserJet_2550_series"
	try
		do shell script "lpr -P " & quoted form of Print_er & " " & quoted form of file_to_print
	end try
	display dialog "yep"
end printFile

i guess im having problems in the printing department, ive looked at the status of my printers and have tried several of them based on the name given in terminal. do these printers have to be added in a certain way? theyre all network printers.

after messing around with it some more…i figured out i can print files with lpr -P printername filename just find through terminal…so im guessing its not finding the right thing to print…any other ideas…this is driving me nuts!

You may still be having an issue with the string you send to the shell for lpr. This thread has a few details you may find useful. Try that and check back.

right…ive seen this thread before, im pretty sure i have the right printer name since i can print to it from terminal, my thinking is that somehow a_Message or theMessages isnt getting through to the printer. is there any way to test this to make sure im getting the right variables? maybe the messages need to be sent to a txt file before they are sent to the printer. im running out of ideas.

OK, I found a couple of issues during testing. First, neither you nor I noticed a typo in the first repeat. Instead of repeating through the variable [theMessages], it was written [the messages]. Secondly, when we called the print handler, we were sending the entire message, which does not seem to work, so now, we just send the content:

using terms from application "Mail"
	on perform mail action with messages theMessages
		tell application "Mail" to repeat with a_Message in theMessages
			my printFile(a_Message's content)
		end repeat
	end perform mail action with messages
end using terms from

to printFile(file_to_print)
   set Print_er to "hp_color_LaserJet_2550_series"
   try
       do shell script "lpr -P " & quoted form of Print_er & " " & quoted form of file_to_print
   end try
   display dialog "yep"
end printFile

Try this and let me know if we are closer to what you want.

i tried what you suggested, and still nothing…

so here is my list of printers that ive tried this on and none of them work…i wonder if its maybe that theyre set up via ip socket?

_192_168_0_31:
printer is on device ‘socket’ speed -1
queuing is enabled
printing is enabled
no entries
daemon present
_192_168_0_35:
printer is on device ‘socket’ speed -1
queuing is enabled
printing is enabled
no entries
daemon present
hp_color_LaserJet_2550_series:
printer is on device ‘mdns’ speed -1
queuing is enabled
printing is enabled
no entries
daemon present

thanks for taking the time to look at this. hopefully we’ll figure it out eventually!

by the way im running this on an intel 10.4.6 imac

Wow. This is infuriating. Sounds like time to run some serious shell testing. It may be that the shell only likes files to print, rather than plain text. Try making a dummy .txt file, and sending that to the handler. According the lpr man page:

DESCRIPTION
lpr submits files for printing. Files named on the command line are
sent to the named printer (or the system default destination if no des-
tination is specified). If no files are listed on the command-line lpr
reads the print file from the standard input.

So, it should work even if you send a string there, but I admit that I have only used it for printing files, not strings. This should help test it out some:

set abc to "abcdefghijklmnopqrstuvwxyz"
printFile(abc)

to printFile(file_to_print)
	set Print_er to "hp_color_LaserJet_2550_series"
	try
		do shell script "lpr -P " & quoted form of Print_er & " " & quoted form of file_to_print
	end try
	display dialog "yep"
end printFile

Or this:

set abc to (choose file with prompt "Select a .txt file:") as Unicode text
printFile(abc)

to printFile(file_to_print)
	set Print_er to "hp_color_LaserJet_2550_series"
	try
		do shell script "lpr -P " & quoted form of Print_er & " " & quoted form of file_to_print
	end try
	display dialog "yep"
end printFile

weird…not even that works…maybe something has changed in 10.4.6

but i can print manually from the shell…ponder on that one hahha :confused: sorry for this retarded applescript. i cant believe im the only one with this problem.