Hi,
Here is my first draft,
I have tested this script and I think it does what you want.
The reason I asked about odd characters in the text, is no matter what method I tried, Automator, Applescript…
I got lots of garbage from the bad html type emails.
I had the bases of the pdf conversion and emailer pretty much already,I could have posted a script sooner but was not really happy with the garbage in the end result.
So spent sometime trying to find a solution.
This works. Now saying that, I am sure someone will do it better
The script gathers the selected emails. (You will need to change the get selection to what ever method you want to use to gather the emails)
It then gets each emails details and combines them in a similar format used by the Automator action and cleans any garbage characters out by writing out two helper files.
Then writes out the final pdf file, and deletes the two help files.
Formats the email and sends it. ( Actually I have set it here to SAVE in drafts, Change it to SEND)
Thats it, I wrote this on the fly kinda thing, so it most likely can slimmed down;)
(* CHANGE EMAIL ADDRESSES *)
property theAddressTo : "TheirEmailAddress@mac.com"
property theAddressFrom : "YourEmailAddress@mac.com"
(* Digest format lines *)
set sendr to "FROM: "
set dte to "DATE: "
set sbj to "SUBJECT: "
set rcpt to "RECIPIENT: "
set body to ""
set demark to "-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*"
set theMessageString to ""
set theBody to ""
(*Path and file names for helper conversion files
CHANGE PATH TO WHERE YOU WANT *)
set usr to POSIX path of (path to current user folder from user domain)
set textTidyFile to usr & "out-file.html"
set newFileName to ""
set textOutFile to usr & "out"
global textTidyFile, textOutFile, newFileName
tell application "Mail"
set added_items to get selection
repeat with i from 1 to number of items in added_items
set this_item to item i of added_items
set theMessage to {dm:demark & return & return, theDate:dte & date sent of this_item & return, theFrom:sendr & sender of this_item & return, theSubject:sbj & subject of this_item & return, theRecipient:rcpt & address of recipient of this_item & return & return, theContent:body & content of this_item & return & return}
set theMessageString to theMessageString & (items of theMessage as string)
end repeat
(* Cleanup any garbage from html emails *)
set theMessageString to my textUtilStuff(theMessageString)
(* write out helper file *)
my writeFile(theMessageString)
(* convert text from helper file to pdf and then remove helper file (-D option in the convert command *)
my processFile()
(* mail out pdf file
Change SAVE to SEND in the _mailer() handler*)
my _mailer()
end tell
on textUtilStuff(theBody)
(* using -inputencoding utf-8, seems to stop some bits of garbled text from the html emails *)
(* using -utf8 in the tidy solved the rest of the garbled text from the html emails
The echo "d" >&2 stops the warnings *)
set theBody to (do shell script "echo " & quoted form of theBody & "|textutil -inputencoding utf-8 -convert html -stdin -stdout | tidy -iq -utf8 -output " & textTidyFile & ";echo \"d\" >&2 ")
set theMessageString to (do shell script "textutil -inputencoding utf-8 -convert txt " & textTidyFile & " -stdout ")
return theMessageString
end textUtilStuff
on writeFile(theMessageString)
set t_message to (do shell script "echo " & quoted form of theMessageString & "> " & textOutFile)
end writeFile
on processFile()
set thePOSIXFileName to textOutFile
set terminalCommand to ""
set convertCommand to "/System/Library/Printers/Libraries/./convert "
set newFileName to thePOSIXFileName & ".pdf"
set terminalCommand to convertCommand & "-i \"text/plain\" -D -f " & "\"" & thePOSIXFileName & "\"" & " -o " & "\"" & newFileName & "\"" & " -j \"application/pdf\""
do shell script terminalCommand
tell application "Finder" to delete (POSIX file textTidyFile as alias)
end processFile
on _mailer()
set theDigest to (POSIX file newFileName) as alias
tell application "Mail"
set theSubject to "Combined pdf digest "
set newMessage to make new outgoing message with properties {subject:theSubject, content:"New Digest" & return & return}
tell newMessage
set visible to false
set sender to theAddressFrom
make new to recipient at end of to recipients with properties {address:theAddressTo}
(* change save to send to send*)
tell content
make new attachment with properties {file name:theDigest} at after the last paragraph
end tell
save --<<<<---------------- change save to send to send
(* change save to send to send*)
end tell
end tell
end _mailer