![]() |
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. |
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.
|
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. |
Mail auto print
Any luck with this?
|
I'm taking a crack at it...will keep you posted.
|
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.
|
So how does microsoft do it :o)
|
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)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. |
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!
:) |
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) |
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)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 |
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)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. |
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)Code:
lprstat -aCode:
do shell script "lpr /MailText"Code:
do shell script "lpr nameOfPrinter /MailText" |
cool
So the above script, when compiled, will automatically print incoming emails based on my desired rule?
|
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 & ¬Try it! If you have problems, post back here and maybe we can sort them out for you. |
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 |
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" |
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 |
ouch, nasty double post..sorry!
|
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:
Did anything print? |
My rule is something along the lines of if subject contains "print" then print.
and lpr seems to be active (as in working) through the console. My code is as follows: Code:
|
further empirical testing:
comment out the lines Code:
tell application "Finder" |
I'll try that this weekend... thanks and I'll keep you posted :o)
|
use full paths in 'do shell script'
Read this Apple article about 'do shell script':
http://developer.apple.com/technotes/tn2002/tn2065.html One of the most important things it tells you is that you should use full paths for any executables or files referred to in the script. Thus you should use /usr/bin/lpr instead of just lpr. The section "Dealing with Files" might also be useful - the 'POSIX path' and 'POSIX file' constructions are useful when going back & forth between AppleScript and UNIX shell scripts. |
Good stuff to keep in mind when debugging, Hayne. Thanks!
|
Still no Luck
Hmm... I posted the code exactly as stated under "success," but it still will not work. I also tried using full path names to lpr and I tried using my full printer name.
I also tried to comment out the lines that delete the temp file. When I ran the script again I couldn't find the temp file. Possible reasons why this is not working: 1. I am using my airport base as a print station. 2. I am not using the default account that came on this ibook. I created a new one and then delete that one. I gave this account "addminister" privileges, so I assume my current account to be no different to the original account. Thank again, Will |
ping
ping
|
Wonderfull, just what I needed, needed some tweaking on my machine as I didn't get headers, I've reduced this info down to the sender, time, subject & content.
Code:
using terms from application "Mail" |
Thanks for the update, I love to see a great old thread revived!
|
Thanks a buch for the scripts folks, bun none of the scripts work, the first one prints but only "?" is printed. The other one "the above" doest even print.
Anymore great ideas would be apprecited. :confused: |
I don't actually use it myself, one of my clients wanted it, it did work correctly when posted, I'll check it out to see what's going wrong.
|
Still works for me, I would test lpr from your terminal to make sure that command operates, write a text file using textedit, (covert to plain text Format>Make plain text), save it in your home folder as test.txt, then type: lpr test.txt
in the terminal and hit return. If that does not function try: /usr/bin/lpr test.txt Make sure your default printer is set to an available printer before running any of these commands. |
Gpg
I'm having an issue with the print script and GPG. I've got GPG working in Mail, but when I use a auto print script I get the unencrypted source. Does anyone have script to decrypt and print?
|
| All times are GMT -5. The time now is 09:43 AM. |
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.