Go Back   The macosxhints Forums > OS X Help Requests > Applications



Reply
 
Thread Tools Rating: Thread Rating: 3 votes, 5.00 average. Display Modes
Old 02-11-2003, 01:07 PM   #1
wplate
Prospect
 
Join Date: Feb 2003
Posts: 14
script to print Mail.app messages

So Mail.app can't automatically Print a message from within a rule, but it can run AppleScripts.

Can someone offer me a script that will print a message, so I can trigger the script from a rule and automatically print messages?

Thank you.
wplate is offline   Reply With Quote
Old 02-11-2003, 03:39 PM   #2
jecwobble
Triple-A Player
 
Join Date: Sep 2002
Posts: 93
Try this...

...hint. I noticed this hint after reading your request. You might be able to modify it to get it to do what you want.
jecwobble is offline   Reply With Quote
Old 02-11-2003, 03:42 PM   #3
wplate
Prospect
 
Join Date: Feb 2003
Posts: 14
I've seen that.

I'm afraid it doesn't help me since I don't know AppleScript enough to make the script Print the message.

That's why I'm asking for help. Thanks.
wplate is offline   Reply With Quote
Old 07-30-2003, 09:50 PM   #4
willchernoff
Prospect
 
Join Date: Jul 2003
Posts: 10
Mail auto print

Any luck with this?
willchernoff is offline   Reply With Quote
Old 07-30-2003, 10:02 PM   #5
rusto
MVP
 
Join Date: Jan 2002
Location: Boston, MA
Posts: 1,489
I'm taking a crack at it...will keep you posted.
__________________
:: 3.4GHz Core i7 iMac 4GB RAM :: Black MacBook SR :: 10.7.2 :: iPhone 4 / iOS 5 ::
rusto is offline   Reply With Quote
Old 07-30-2003, 10:51 PM   #6
rusto
MVP
 
Join Date: Jan 2002
Location: Boston, MA
Posts: 1,489
After some noodling around with this, I've found (I think) an obstacle to the whole idea: when you trigger an AppleScript via a Rule, although you can get certain elements of a message and display a dialog with them (for instance, the subject, sender or content), to PRINT the message you need to open it. You can't open it until it arrives in a Mailbox. Trouble is, the message does not show up in a Mailbox until the rule has finished doing it's thing.
__________________
:: 3.4GHz Core i7 iMac 4GB RAM :: Black MacBook SR :: 10.7.2 :: iPhone 4 / iOS 5 ::
rusto is offline   Reply With Quote
Old 07-31-2003, 02:22 AM   #7
willchernoff
Prospect
 
Join Date: Jul 2003
Posts: 10
So how does microsoft do it )
willchernoff is offline   Reply With Quote
Old 07-31-2003, 03:27 AM   #8
jbc
All Star
 
Join Date: Feb 2003
Location: Chico, CA
Posts: 686
Well, assuming you're trying to print a normal email and not html, you could try a variation of this approach:

Code:
on perform_mail_action(info)
  tell application "Mail"
    set selectedMessages to |SelectedMessages| of info
    repeat with eachMessage in selectedMessages
      set theEmail to (headers of eachMessage as text) & return & return ¬
        & (content of eachMessage as text)
      set theTextFile to open for access ((path to startup disk as string) ¬
        & "MailText") with write permission
      set theTempFile to (((path to startup disk as string) & "MailText") as alias)
      write theEmail to theTextFile
      close access theTextFile
      tell application "TextEdit"
        print {theTempFile}
        quit
      end tell
      tell application "Finder"
        delete theTempFile
      end tell
    end repeat
  end tell
end perform_mail_action
Basically you're just writing the email headers and content to a temporary text file and printing the text file.

I'm thinking there's probably a more graceful way to send the text file to the printer in the background via a "do shell script" line, but I don't happen to know how to print with a shell script. Any unix experts care to streamline this a bit?

Note that Script Editor 2.0 keeps replacing "path to startup disk as string" with "path to startup disk type string" and then refuses to compile it a second time because of the changes it made! Just keep changing the "type" back to "as" and it seems to compile just fine.

Note also that if you want the whole works, you can put "source of eachMessage as text" into the text file; this is *ALL* the raw data of the email. But in this case large binary attachments will turn into big blocks of gibberish, since they will be included in the printout as text.
jbc is offline   Reply With Quote
Old 07-31-2003, 07:35 AM   #9
rusto
MVP
 
Join Date: Jan 2002
Location: Boston, MA
Posts: 1,489
Hehe, I went to bed last night after my post but could not let go of the problem. As I was falling asleep, it occurred to me a solution like yours might do the trick!

__________________
:: 3.4GHz Core i7 iMac 4GB RAM :: Black MacBook SR :: 10.7.2 :: iPhone 4 / iOS 5 ::
rusto is offline   Reply With Quote
Old 07-31-2003, 08:04 AM   #10
rusto
MVP
 
Join Date: Jan 2002
Location: Boston, MA
Posts: 1,489
Hrm, I keep changing the "type" to "as" and even in Script Editor 1.9 it changes it back to "type" EVERY TIME! Well, assuming you can get the "as" to stick, this ought to work using a command line call:

Code:
on perform_mail_action(info)
	tell application "Mail"
		set selectedMessages to |SelectedMessages| of info
		repeat with eachMessage in selectedMessages
			set theEmail to (headers of eachMessage as text) & return & return ¬
				& (content of eachMessage as text)
			set theTextFile to open for access ((path to startup disk as string) ¬
				& "MailText") with write permission
			set theTempFile to (((path to startup disk as string) & "MailText") as alias)
			write theEmail to theTextFile
			close access theTextFile
			do shell script "lpr" & theTempFile
			tell application "Finder"
				delete theTempFile
			end tell
		end repeat
	end tell
end perform_mail_action
__________________
:: 3.4GHz Core i7 iMac 4GB RAM :: Black MacBook SR :: 10.7.2 :: iPhone 4 / iOS 5 ::

Last edited by rusto; 07-31-2003 at 08:39 AM.
rusto is offline   Reply With Quote
Old 07-31-2003, 12:12 PM   #11
rusto
MVP
 
Join Date: Jan 2002
Location: Boston, MA
Posts: 1,489
Ok, I've twaddled your script a bit further to avoid the "as-to-type" problem with the compiler, but I've got a new problem that I think one of the UNIX guru's might be able to help with.

Here's the code:

Code:
on perform_mail_action(info)
	tell application "Mail"
		set selectedMessages to |SelectedMessages| of info
		repeat with eachMessage in selectedMessages
			set theEmail to (headers of eachMessage as text) & return & return ¬
				& (content of eachMessage as text)
			do shell script "echo " & theEmail & " > /MailText;lpr /MailText"
			tell application "Finder"
				delete "/MailText"
			end tell
		end repeat
	end tell
end perform_mail_action
The problem (I think) is that the text in the variable theEmail has Mac line endings and the "do shell script" that writes the file wants UNIX line endings. Is there a way in AppleScript to change the line endings?

The following test code shows that the basic idea of the code will work for one line of text:

Code:
set theText to "all this gobbledeegoop and this stuff too" as text
do shell script "echo " & theText & " >  /MailText;lpr  /MailText"
__________________
:: 3.4GHz Core i7 iMac 4GB RAM :: Black MacBook SR :: 10.7.2 :: iPhone 4 / iOS 5 ::
rusto is offline   Reply With Quote
Old 07-31-2003, 01:55 PM   #12
jbc
All Star
 
Join Date: Feb 2003
Location: Chico, CA
Posts: 686
Hehe. I must be living in a parallel universe. Got the idea from your comments to just move everything into shell commands to avoid the Applescript glitches. Came up with this.

Code:
on perform_mail_action(info)
  tell application "Mail"
    set selectedMessages to |SelectedMessages| of info
    repeat with eachMessage in selectedMessages
      set theEmail to (headers 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
  end tell
end perform_mail_action
And I ran into the same problem! The variable theEmail contains data, but it's not getting written to the file. Seems to be some weirdity with writing the variable to a file when the email is multipart. Works fine for me with plain text emails. And it's not simply an issue with the shell script redirect: my original method of writing the file using Applescript has the same problem.

Popped in to see if you had any ideas. Looks like we're thinking along the same lines!

Probably the easiest way to munge the text for line endings is to pipe the text through sed or awk or what not. There is a regex scripting addition for Applescript, but it only works with OS < X.

Last edited by jbc; 07-31-2003 at 01:58 PM.
jbc is offline   Reply With Quote
Old 07-31-2003, 02:02 PM   #13
rusto
MVP
 
Join Date: Jan 2002
Location: Boston, MA
Posts: 1,489
Thumbs up Success

Worked at it from a different angle: I just made the paths to the file, "MailText" explicit and that allowed me to write to it via AppleScript directly, thus avoiding the line-endings issue I was having earlier:

Code:
on perform_mail_action(info)
	tell application "Mail"
		set selectedMessages to |SelectedMessages| of info
		repeat with eachMessage in selectedMessages
			set theEmail to (headers of eachMessage as text) & return & return ¬
				& (content of eachMessage as text)
			set theTextFile to open for access ":MailText" with write permission
			set theTempFile to (":MailText" as alias)
			write theEmail to theTextFile
			close access theTextFile
			do shell script "lpr /MailText"
			tell application "Finder"
				delete theTempFile
			end tell
		end repeat
	end tell
end perform_mail_action
Note that the lpr command will send the print job to your default printer. If you wish to send it to a different printer, in terminal type

Code:
lprstat -a
to get the list of available printers (and the names that the lpr command will recognize) and merely change the line

Code:
do shell script "lpr /MailText"
with

Code:
do shell script "lpr nameOfPrinter /MailText"
__________________
:: 3.4GHz Core i7 iMac 4GB RAM :: Black MacBook SR :: 10.7.2 :: iPhone 4 / iOS 5 ::
rusto is offline   Reply With Quote
Old 08-01-2003, 02:21 AM   #14
willchernoff
Prospect
 
Join Date: Jul 2003
Posts: 10
cool

So the above script, when compiled, will automatically print incoming emails based on my desired rule?
willchernoff is offline   Reply With Quote
Old 08-01-2003, 09:43 PM   #15
jbc
All Star
 
Join Date: Feb 2003
Location: Chico, CA
Posts: 686
It appears to. Not quite sure what the difference is with the last changes that rusto made, but I'm no longer having problems with the file not getting written for multipart emails as before.

I don't currently have a printer in the house, so I have to trust that the lpr command actually prints the file, but everything else seems to be working as it should.

It should print out a text version of whatever emails match the rules in which you include a "Run Applescript" action that points to this script. It will use the default printer unless you specify some options as rusto noted above. Use "man lpr" in terminal; there are options for printing headers and other useful things.

There are also many options for modifying the script. For example, if you don't want all the email headers to be printed, you could use a line like the following to build a simpilfied version of the text to be printed.

Code:
set theEmail to "From: " & (sender of eachMessage as text) & return & ¬
"Subject: " & (subject of eachMessage as text) & return & return ¬
& (content of eachMessage as text)
The 'tell application "Finder"/delete theTempFile/end tell" lines actually move the temp file to the trash. You could also use 'do shell script "rm /MailText"' as in my shell script attempt to delete the file immediately.

Try it! If you have problems, post back here and maybe we can sort them out for you.
jbc is offline   Reply With Quote
Old 08-04-2003, 09:35 PM   #16
willchernoff
Prospect
 
Join Date: Jul 2003
Posts: 10
When running the script through mail does it matter if I've compiled the code as either a script or an application?

So far I've tried it in both form and it does not seem to be working.

-Will
willchernoff is offline   Reply With Quote
Old 08-04-2003, 11:49 PM   #17
jbc
All Star
 
Join Date: Feb 2003
Location: Chico, CA
Posts: 686
It needs to be saved compiled rather than as an application. I believe this is true for any Applescript that gets used for a Mail rule.

Name it "Print Email" or somesuch. Then set up a rule to tell Mail which emails get printed, such as

If "any" of the following conditions are met:

From contains "bigcheese@yourcompany.com"
From contains "dubya@whitehouse.com"

Perform the following actions:

Run Applescript "Print Email"
jbc is offline   Reply With Quote
Old 08-08-2003, 12:40 AM   #18
willchernoff
Prospect
 
Join Date: Jul 2003
Posts: 10
Hmm... I pasted the following code into the applescript compiler and saved the file as a script, however, Mail does not seem to be auto printing.

on perform_mail_action(info)
tell application "Mail"
set selectedMessages to |SelectedMessages| of info
repeat with eachMessage in selectedMessages
set theEmail to (headers of eachMessage as text) & return & return ¬
& (content of eachMessage as text)
set theTextFile to open for access ":MailText" with write permission
set theTempFile to (":MailText" as alias)
write theEmail to theTextFile
close access theTextFile
do shell script "lpr /MailText"
tell application "Finder"
delete theTempFile
end tell
end repeat
end tell
end perform_mail_action

_____________________________________

I have a USB printer, so I'm not sure if that is the issue.

-Will
willchernoff is offline   Reply With Quote
Old 08-08-2003, 07:07 AM   #19
rusto
MVP
 
Join Date: Jan 2002
Location: Boston, MA
Posts: 1,489
ouch, nasty double post..sorry!
__________________
:: 3.4GHz Core i7 iMac 4GB RAM :: Black MacBook SR :: 10.7.2 :: iPhone 4 / iOS 5 ::

Last edited by rusto; 08-08-2003 at 07:09 AM.
rusto is offline   Reply With Quote
Old 08-08-2003, 07:09 AM   #20
rusto
MVP
 
Join Date: Jan 2002
Location: Boston, MA
Posts: 1,489
Hmm, works great here. This script is activated by a Rule in Mail.app...you DO have a Rule set up to call the script, right?

If that's not it, let's see if the lpr command is working for you:
  • 1) make sure your printer is on

    2) Create a text file on your Desktop.

    3) Open the Terminal

    4) type lpr into the Terminal followed by one space

    5) drag and drop your test text file into the Terminal window (this will enter it's path on the command line after "lpr")

    6) hit Return or Enter

Did anything print?
__________________
:: 3.4GHz Core i7 iMac 4GB RAM :: Black MacBook SR :: 10.7.2 :: iPhone 4 / iOS 5 ::
rusto is offline   Reply With Quote
Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump



All times are GMT -5. The time now is 02:26 PM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.
Site design © IDG Consumer & SMB; individuals retain copyright of their postings
but consent to the possible use of their material in other areas of IDG Consumer & SMB.