***** $100 by paypal to anyone who can help me *****8

I have this script i found on here. After updating to lion it prints the message but does not print the attachment. I only want to print the attachments. Can someone please help me fix this.

I want this script to just print attachments only not the mail message.

Im offering a $100 payment to who ever can make this work.

-- author: Martin Michel
-- eMail: martin.michel@macscripter.net
-- created: 05.02.2008

-- Installed together with an AppleScript-based Apple Mail mail rule,
-- this script - hopefully - prints out all rule-matching eMails (and their
-- attachments according to the options manually set below)

-- The script was tested under/with:
-- ¢ Mac OS X 10.5.1
-- ¢ Apple Mail 3.1
-- ¢ Intel & PowerPC based Macs

property mytitle : "pergamail"
property myversion : "0.1b"

-- here you can set the application which is going to
-- print the eMail message: TextEdit or lpr
-- the default value is 'TextEdit'
-- why 'TextEdit'? because it can print without
-- invoking the print dialog
-- lpr is pure printing via the command line
-- possible values: "TextEdit" or "lpr"
property defprintapp : "TextEdit"
-- you can choose a default printer to be used, e.g.
-- "HP LaserJet 1300"
-- default value is missing value
property defprintername : missing value
-- do you also want to try to print the eMail attachments?
-- please note, that you can currently only print
-- attachments of the following types (due to the use of the 'lpr'-command):
-- PDF, RTF(D), TXT, HTML, most image formats natively
-- supported by Mac OS X
property defprintatms : false
-- you can set this property to 'long' to see even more mail infos
-- in the plain text report
property defmsginfo : "short"

-- unique handler to perform AppleScript actions on rule-matching Apple Mail messages
using terms from application "Mail"
	on perform mail action with messages matchmsgs for rule therule
		repeat with matchmsg in matchmsgs
			my printmailmsg(matchmsg)
		end repeat
	end perform mail action with messages
end using terms from

-- deactivated debug routine 
(*on run
	tell application "Mail"
		set msg to item 1 of (selection as list)
		my printmailmsg(msg)
	end tell
end run*)

-- main function controlling the script procedure
on printmailmsg(msg)
	try
		-- converting the Apple Mail message to a plain text report
		--set msgtxt to "Hallo"
		set msgtxt to my msgtotext(msg)
		-- finding and getting an unused temp file
		set tmpfilepath to my TmpFile's newpath()
		-- saving the plain text report in the temp file as UTF-8
		my writetofile(msgtxt, tmpfilepath)
		-- printing the temp file
		my printfile(tmpfilepath, defprintapp)
		-- deleting the temp file
		my TmpFile's remove()
		-- printing the attached files if possible
		if defprintatms is true then
			my printatms(msg)
		end if
		-- THE END
	on error errmsg number errnum
		set logmsg to ((current date) as Unicode text) & ": " & errmsg & " (" & errnum & ")"
		set logfile to ((path to desktop) as Unicode text) & "pergamail.log"
		my writetofile(logmsg, logfile)
	end try
end printmailmsg

-- I am printing the given file with the chosen application
on printfile(filepath, printapp)
	-- if the user chose to use a default printer,
	-- we must know the current printer, as we might have
	-- to set it back later
	if defprintername is not missing value then
		set curprintername to my getcurprintername()
		my setcurprinterbyname(defprintername)
	end if
	-- printing via the command line
	if printapp is "lpr" then
		-- silence is golden...
		set cmd to "lpr " & quoted form of (POSIX path of filepath)
		set cmd to cmd as «class utf8»
		do shell script cmd
		-- printing via TextEdit
	else if printapp is "TextEdit" then
		-- if TextEdit is currently not running, we
		-- should quit it afterwards
		if appisrunning("TextEdit") then
			set tewasrunning to true
		else
			set tewasrunning to false
		end if
		-- printing the file without invoking the print dialog and
		-- without bringing TextEdit to the foreground
		tell application "TextEdit"
			set newdoc to open (filepath as alias)
			print newdoc without print dialog
			close newdoc
		end tell
		-- cleaning up...like mother told us
		if not tewasrunning then
			tell application "TextEdit"
				quit
			end tell
		end if
	end if
	-- setting back the default printer to its prior value
	if defprintername is not missing value then
		my setcurprinterbyname(curprintername)
	end if
end printfile

-- I am printing the attachments of a mail message if possible
on printatms(msg)
	-- saving all attachments in temporary files
	set tmpfilepaths to {}
	tell application "Mail"
		set atms to mail attachments of msg
		repeat with atm in atms
			set atmname to name of atm
			repeat
				set rndnum to random number from 1000 to 99999
				set tmpfolder to (path to temporary items folder from user domain) as Unicode text
				set tmpfilepath to tmpfolder & "tmp_" & rndnum & "_" & atmname
				try
					set tmpfilepath to tmpfilepath as alias
				on error
					exit repeat
				end try
			end repeat
			try
				save atm in tmpfilepath
				set tmpfilepaths to tmpfilepaths & tmpfilepath
			end try
		end repeat
	end tell
	-- printing the temporarily saved attachments and then deleting them afterwards
	-- not all attachment types can be printed (e.g. MS Word/Exel, etc.)
	repeat with tmpfilepath in tmpfilepaths
		my printfile(tmpfilepath, "lpr")
		tell application "Finder"
			delete (tmpfilepath as alias)
		end tell
	end repeat
end printatms

-- I am returning the name of the current printer
on getcurprintername()
	tell application "Printer Setup Utility"
		return name of current printer
	end tell
end getcurprintername

-- I am setting the current printer to the given printer name
on setcurprinterbyname(printername)
	tell application "Printer Setup Utility"
		set allprinters to every printer
		set printernames to name of every printer
		if printername is not in printernames then
			return false
		else
			if name of current printer is not equal to printername then
				set current printer to printer printername
			end if
			return true
		end if
	end tell
end setcurprinterbyname

-- I am converting an Apple Mail eMail message to a plain text report
on msgtotext(msg)
	set newline to ASCII character 10
	tell application "Mail"
		--set msgid to id of msg
		set msguid to message id of msg
		set msgsize to my getstringsize(message size of msg)
		--set msgmbox to name of mailbox of msg
		set msgcont to content of msg
		set msgsender to sender of msg
		set msgrecvd to date received of msg
		set msgsent to date sent of msg
		set msgsubj to subject of msg
		set msgtorec to my gettxtrecipients("TO", msg)
		set msgccrec to my gettxtrecipients("CC", msg)
		set msgbccrec to my gettxtrecipients("BCC", msg)
		set msgatms to my gettxtatms(mail attachments of msg)
		set msgreplyto to reply to of msg
		--set msgpath to my getmsgpath(msgid)
	end tell
	-- basic report:
	set msgtext to "+ + + + + + + + + + + + + + + + + + + + + + + + + + + +" & newline & newline
	set msgtext to msgtext & "SUBJECT: " & msgsubj & newline & "FROM: " & msgsender & newline & "DATE: " & msgrecvd & newline & "TO: " & msgtorec
	-- further enhancing the report if information is available
	-- >> CC recipients?
	if msgccrec is not missing value then
		set msgtext to msgtext & newline & "CC: " & msgccrec
	end if
	-- >> BCC recipients?
	if msgbccrec is not missing value then
		set msgtext to msgtext & newline & "BCC: " & msgbccrec
	end if
	-- >> long info?
	if defmsginfo is "long" then
		set msgtext to msgtext & newline & "SIZE: " & msgsize & newline & "SENT: " & msgsent & newline & "REPLY-TO: " & msgreplyto
	end if
	-- >> attachments?
	if msgatms is not missing value then
		set msgtext to msgtext & newline & newline & "##########" & newline & msgatms & newline & "##########"
	end if
	set msgtext to msgtext & newline & newline & "+ + + + + + + + + + + + + + + + + + + + + + + + + + + +" & newline & newline & msgcont
	return msgtext
end msgtotext

-- I am returning the recipients of an eMail message as plain text list
on gettxtrecipients(rectype, msg)
	-- I am returning the recipeints as a text list, e.g.:
	-- "Martin Michel <martin@joyofscripting.com>, Steve Jobs <steve@mac.com>"
	-- If there are no recipients availabe, I return «missing value»
	set textrecipients to ""
	tell application "Mail"
		if rectype is "TO" then
			set recpnts to to recipients of msg
		else if rectype is "CC" then
			set recpnts to cc recipients of msg
		else if rectype is "BCC" then
			set recpnts to bcc recipients of msg
		end if
		if recpnts is {} then
			return missing value
		else
			set countrecpnts to length of recpnts
			repeat with i from 1 to countrecpnts
				set recaddress to address of (item i of recpnts)
				set recname to name of (item i of recpnts)
				-- sometimes the «name» property is not available for a recipient,
				-- so we have to use an ugly try...end try-block below:
				if i is equal to countrecpnts then
					try
						set textrecipients to textrecipients & recname & " <" & recaddress & ">"
					on error
						set textrecipients to textrecipients & "<" & recaddress & ">"
					end try
				else
					try
						set textrecipients to textrecipients & recname & " <" & recaddress & ">, "
					on error
						set textrecipients to textrecipients & "<" & recaddress & ">, "
					end try
				end if
			end repeat
			return textrecipients
		end if
	end tell
end gettxtrecipients

-- I am returning the eMail attachments as a plain text list
on gettxtatms(atms)
	set textatms to ""
	if atms is {} then
		return missing value
	else
		set countatms to length of atms
		if countatms is equal to 1 then
			set textatms to ("1 Attachment:" & return)
		else
			set textatms to (countatms & " Attachments:" & return)
		end if
		tell application "Mail"
			repeat with i from 1 to countatms
				set atm to item i of atms
				set atmname to name of atm
				set atmsize to my getstringsize(file size of atm)
				set atmmime to MIME type of atm
				set atmdl to downloaded of atm
				set atmentry to "   «" & atmname & "» (" & atmsize & ")"
				if i is equal to countatms then
					set textatms to textatms & atmentry
				else
					set textatms to textatms & atmentry & return
				end if
			end repeat
		end tell
		return textatms
	end if
end gettxtatms

-- I am returning the given byte site in human readable form
on getstringsize(bytesize)
	set pyscriptpath to ((path to me) as Unicode text) & "Contents:Resources:Scripts:utl.py"
	set cmd to "python " & quoted form of (POSIX path of pyscriptpath) & space & bytesize
	set cmd to cmd as «class utf8»
	set shellresult to do shell script cmd
	return shellresult
end getstringsize

-- I am returning the file pathof the eMail message based on its message ID
on getmsgpath(msgid)
	set mailfolder to ((path to library folder from user domain) as Unicode text) & "Mail"
	set cmd to "find " & quoted form of (POSIX path of mailfolder) & space & "-name" & space & msgid & ".emlx"
	set cmd to cmd as «class utf8»
	set shellresult to do shell script cmd
	if shellresult is "" then
		return missing value
	else
		return shellresult
	end if
end getmsgpath

-- I am writing given content to a given file using UTF-8 text encoding
on writetofile(cont, filepath)
	try
		set openfile to open for access filepath with write permission
		set eof of openfile to 0
		set BOM_UTF8 to ((ASCII character 239) & (ASCII character 187) & (ASCII character 191))
		write (cont as «class utf8») to openfile
		close access openfile
		return true
	on error
		try
			close access openfile
		end try
		return false
	end try
end writetofile

-- I am indicating if a given application is currently running
on appisrunning(appname)
	tell application "System Events"
		set processnames to name of processes
	end tell
	if appname is in processnames then
		return true
	else
		return false
	end if
end appisrunning

-- script object to manage a temporary file
script TmpFile
	property filepath : missing value
	
	-- I am creating a new, not yet existing file path in the temp folder
	on newpath()
		set tmpfolderpath to (path to temporary items folder from user domain) as Unicode text
		repeat
			set rndnum to random number from 1000 to 99999
			set tmpfilepath to (tmpfolderpath & (rndnum as Unicode text) & ".tmp")
			try
				set tmpfilepath to tmpfilepath as alias
			on error
				set filepath to tmpfilepath
				exit repeat
			end try
		end repeat
		return filepath
	end newpath
	
	-- I am returning the file path of the temporary file 
	on getpath()
		return filepath
	end getpath
	
	-- I am trying to delete the temporary file
	on remove()
		try
			set command to "rm " & quoted form of (POSIX path of (filepath as Unicode text))
			do shell script command
		end try
	end remove
end script

Hi, nuwear.

Please give your queries subject lines which describe the problem, not offer cash rewards.

Posting Guidelines

Hi nuwear,

According to this posting the save command is currently broken in Apple Mail on Lion.

As pergamail uses this command to save the attachments to a temporary folder prior to printing, it does not work on Lion. The posting suggests to use GUI Scripting instead, but my experience is that this is not very reliable for such kinds of scripts. Your mileage may vary.

So my hope is that Apple will fix this with one of the next updates.

Best regards from Leipzig,

Martin

Will this script work with other mail programs.

Hi nuwear,

I read that Outlook for Mac 2011 features rules like Apple’s Mail, which let you automate the mail handling (to, e.g., file, categorize, delete, announce mail or run AppleScript).

But I do not have MS Office for Mac installed on my developer Mac.

Best regards from Berlin,

Martin

Are there any other suggestions. We use this auto print in a vital part of my business.

Hi,

maybe I have a workaround for this problem.
I noticed that the attachments are saved in the mailboxes in ~/Library/Mail.

I hope, that this is reliable and all attachments are saved this way.

My approach is to find the location of the message .emlx file by its message ID.
The parent folder contains the attachment folder / message ID folder / attachment ID folder / the attachment.

Run this script to check it, the result is a list of HFS paths of all attachments in mailbox inbox.

The mdfind command using spotlight might be faster, but the find command is probably more reliable in case
that the content of ~/Library has not been indexed by Spotlight in Lion


set tmpfilepaths to {}
set mailFolder to quoted form of (POSIX path of (path to library folder from user domain) & "Mail")
tell application "Mail"
	set allMessages to messages of inbox
	repeat with aMessage in allMessages
		set messageID to (id of aMessage as string)
		try
			set messageLocation to do shell script "find " & mailFolder & " -name " & messageID & ".emlx"
			if messageLocation is not "" then
				repeat with anAttachment in (get mail attachments of aMessage)
					set {name:fileName, id:fileID} to anAttachment
					set end of tmpfilepaths to my attachmentPath(messageLocation, messageID, fileID, fileName)
				end repeat
			end if
		end try
	end repeat
end tell
tmpfilepaths

on attachmentPath(aLocation, mID, aID, fName)
	set {TID, text item delimiters} to {text item delimiters, "/"}
	set mailboxFolder to text items 1 thru -3 of aLocation
	set end of mailboxFolder to {"Attachments", mID, aID, fName}
	set attachmentFile to mailboxFolder as text
	set text item delimiters to TID
	return POSIX file attachmentFile as text
end attachmentPath

In Martin’s script exchange the printatms() handler with the following code (the attachmentPath() handler will be added)

-- I am printing the attachments of a mail message if possible
on printatms(msg)
	-- saving all attachments in temporary files
	set mailfolder to quoted form of (POSIX path of (path to library folder from user domain) & "Mail")
	set tmpfilepaths to {}
	tell application "Mail"
		set atms to mail attachments of msg
		set messageID to (id of msg as string)
		try
			set messageLocation to do shell script "find " & mailfolder & " -name " & messageID & ".emlx"
			if messageLocation is not "" then
				repeat with anAttachment in atms
					set {name:fileName, id:fileID} to anAttachment
					set end of tmpfilepaths to my attachmentPath(messageLocation, messageID, fileID, fileName)
				end repeat
			end if
		end try
	end tell
	-- printing the temporarily saved attachments and then deleting them afterwards
	-- not all attachment types can be printed (e.g. MS Word/Exel, etc.)
	repeat with tmpfilepath in tmpfilepaths
		my printfile(tmpfilepath, "lpr")
	end repeat
end printatms

-- retrieve attachment path
on attachmentPath(aLocation, mID, aID, fName)
	set {TID, text item delimiters} to {text item delimiters, "/"}
	set mailboxFolder to ((text items 1 thru -3) of aLocation)
	set end of mailboxFolder to {"Attachments", mID, aID, fName}
	set attachmentFile to mailboxFolder as text
	set text item delimiters to TID
	return POSIX file attachmentFile as text
end attachmentPath

Hope it helps

i keep getting this error in the log

Saturday, September 3, 2011 12:15:12 PM: lpr: Error - no default destination available. (1)ent printer. (-1728)

Have you set a default printer on your computer?

I’ve just tested the script and it works fine.
There is a little improvement, some message file are split into pieces, the names are xxxxx.partital.emlx and xxxxx.2.emlxpart

Change this line to consider this case


	set messageLocation to do shell script "find " & mailfolder & " -name " & messageID & "*.emlx"

You have this script printing pdf attachments. I can get it to print the email but we only need it to print the attachment and its not doing that at all

This is a compacted version of Martin’s script which prints only the attachments.

The printer in defprintername must be spelled as displayed with lpstat -a.
The files are filtered by the fileTypeList.

To test the script without a rule comment out the using terms from block and uncomment
the code between the second occurrence of (* *).

Then mark a message in Mail.app with an attachment and run the script.
Look into the event log if there are error messages


property defprintername : "Canon_i865" -- syntax must be the same as displayed with lpstat -a
property fileTypeList : {"pdf", "txt", "html", "rtf", "rtfd", "jpg", "tif", "png"}

-- unique handler to perform AppleScript actions on rule-matching Apple Mail messages

-- (*
using terms from application "Mail"
	on perform mail action with messages matchmsgs for rule therule
		repeat with matchmsg in matchmsgs
			my printatms(matchmsg)
		end repeat
	end perform mail action with messages
end using terms from
-- *)

(*
tell application "Mail" to set theMessages to selection
repeat with matchmsg in theMessages
	my printatms(matchmsg)
end repeat
*)

on printatms(msg)
	set mailfolder to quoted form of (POSIX path of (path to library folder from user domain) & "Mail")
	set tmpfilepaths to {}
	tell application "Mail"
		set atms to mail attachments of msg
		set messageID to (id of msg as string)
		try
			set messageLocation to do shell script "find " & mailfolder & " -name " & messageID & "*.emlx"
			if messageLocation is not "" then
				repeat with anAttachment in atms
					set {name:fileName, id:fileID} to anAttachment
					set end of tmpfilepaths to my attachmentPath(messageLocation, messageID, fileID, fileName)
				end repeat
			end if
		end try
	end tell
	-- not all attachment types can be printed (e.g. MS Word/Exel, etc.)
	repeat with tmpfilepath in tmpfilepaths
		log tmpfilepath
		set fileExtension to name extension of (info for file tmpfilepath)
		if fileExtension is in fileTypeList then
			do shell script "lpr -P " & defprintername & space & quoted form of (POSIX path of tmpfilepath)
		end if
	end repeat
end printatms

-- retrieve attachment path
on attachmentPath(aLocation, mID, aID, fName)
	set {TID, text item delimiters} to {text item delimiters, "/"}
	set mailboxFolder to ((text items 1 thru -3) of aLocation)
	set end of mailboxFolder to {"Attachments", mID, aID, fName}
	set attachmentFile to mailboxFolder as text
	set text item delimiters to TID
	return POSIX file attachmentFile as text
end attachmentPath

Im on lion

I have tried a million times and nothing happens

Ok so it prints the attachment if i manually go to the message and select apply rules but if i just run the rules as normal it does not work.

I’ ll try it in Lion.
If the execution of shell scripts are prevented in Lion (I can’t believe that)
you could use a helper app, a script saved as application which will be called by the rule script

seems to be a general problem of Lion, there are a lot of messages “problem with NSScriptCommand” in console.log

Please Apple do your homework :wink:

I’ve noticed a large difference between Lion and SL with Mail too.

So is there any solutions you suggest

no offense, but upgrading to a new system version in a business environment before testing all relevant functions is quite careless.

Are other rules executed properly which call AppleScripts?
If scripts are not called at all, I guess there is no workaround

I posted a working script (but not tested in Lion) the other day for PowerMail.

The desire to write an alternative Mail front end for it forced me to set up Mail on my own machine to get an understanding of the problems. I hadn’t appreciated that Mail doesn’t actually separate attachments from messages unless specifically made to do so. This is of course the root cause of the problem, which the non-functional ‘save’ command does nothing to alleviate!

One thing I’ve noticed with Mail 4.5 in Snow Leopard is that, if a message is moved or duplicated to another mailbox, its attachments are saved in their own files at the destination. This is just as pointless as not doing it in the first place; but if it’s the same in Lion, a workround for the current problem would be for the script to duplicate any message with attachments to another mailbox (one specially set up for the purpose), identify the message in that mailbox with the same ‘message id’ as the original, and print its detached attachments. The path to each attachment could be constructed from known elements without the use of ‘find’. The only disadvantages I can think of are that Mail takes a while to duplicate a message with attachments and the special mailbox would have to be purged occasionally when the printer queue was clear.

I got this idea to work on my Snow Leopard machine this morning, but can any Lion user tell me if the phenomenon of attachments being saved (in Mail) when a message is duplicated or moved still occurs in Lion?

so did anyone get their $100? he said “to anyone who can help me”, he didn’t say anyone that solved his problem :wink: