The Script:
|
property SelectedButton : "One Window"
global Die, TransferMessageInfo, ConcatenateMessages, HeapsOfDashes, messageSeparator
-- Change the following variable to 'false' if you don't like to get additional message
-- information (from_address, to/cc/bcc_recipients, date_sent/received) inserted at the
-- top of your BBEdit 6.x window:
set TransferMessageInfo to true
-- With multiple messages selected the script will ask you whether you'd like to have
-- each message copied to it's own window or rather have them all concatenated into one window.
-- You can prevent this dialog by setting the following variable to <false> / <true>
-- if you always want to have each message transferred into <its own window> / <one window only>:
set ConcatenateMessages to "ask" -- valid values: true, false, "ask" (ask must be quoted!)
set Die to false
set old_delims to AppleScript's text item delimiters -- save text item delimiters
try -- get the currently selected message(s) or the text selection:
tell application "Mailsmith 1.5"
set sel to selection -- 'set sel to selection as list' bombs Mailsmith 1.1.6 if the selection's class is 'string'!!
set sel to sel as list
set ct to length of sel
if ct is not 0 then
set selType to class of item 1 of sel
if selType is string then -- some text is selected within a single message
set ConcatenateMessages to false
set Msg to message of window 1
set selectedText to selection as text
my TransferMsg(1, 1, Msg, selectedText) -- transfer single message
else if selType is message then -- one or more messages selected
if ct = 1 then -- single message selected
set ConcatenateMessages to false
else -- ct > 1, i.e. multiple messages selected
if ConcatenateMessages is "ask" then
set DialogResult to display dialog ¬
"Transfer each of the " & ct & " selected messages into its own window or concatenate all messages into" & ¬
" one window?" with icon note buttons {"Cancel", "Multiple Windows", "One Window"} default button SelectedButton
if button returned of DialogResult is not "Cancel" then
set SelectedButton to button returned of DialogResult -- save the chosen button as default for the next time
if SelectedButton is "One Window" then
set ConcatenateMessages to true
else if SelectedButton is "Multiple Windows" then
set ConcatenateMessages to false
end if
else -- user pressed "Cancel"
set Die to true
end if
end if
end if
if not Die then
if ConcatenateMessages then
set mboxName to name of container of (item 1 of sel) -- get mailbox name
-- to avoid a repeat loop when building the setext heading:
set HeapsOfDashes to "--------------------------------------------------------------" & ¬
"----------------------------------------------------------------------------" & ¬
"----------------------------------------------------------------------------"
set messageSeparator to "------------------------------------" & ¬
"------------------------------------"
set ProgressInfo to ("-> processing message 1 (of " & ct & "), please wait..." & return & "-> (press cmd-. to cancel)") as string
tell application "BBEdit 6.5"
activate
make new window with properties {name:"" & ct & " messages from mailbox \"" & mboxName & "\"", contents:ProgressInfo}
end tell
end if
repeat with i from 1 to ct -- transfer all messages
set Msg to item i of sel
my TransferMsg(i, ct, Msg, Msg as text)
end repeat
set AppleScript's text item delimiters to old_delims -- restore text item delimiters
end if
else -- class of selection is not string or message
error
end if
else -- nothing selected
error
end if
end tell
on error ErrTxt number ErrNum
if ErrNum is not in {-128, -1711} then -- -128: "User canceled", -1711: "User canceled out of wait loop for reply or receipt."
if ErrNum is in {-1700, -1728, -2700} then -- these errors occur if the user didn't select one or more messages
display dialog "Can't get the message(s), perhaps you haven't selected some!" buttons {"Cancel"} default button 1 with icon caution
else
display dialog "The following error has occurred:" & return & return & ErrTxt & ¬
return & return & "(" & ErrNum & ")" buttons {"Cancel"} default button 1 with icon caution
end if
end if
end try
on TransferMsg(i, ct, Msg, selectedText)
try
tell application "Mailsmith 1.5"
-- get the subject of the message:
set theSubject to subject of Msg
if theSubject is "" then set theSubject to "(no subject)"
if TransferMessageInfo then -- get additional information of the message
-- check the message type:
if (header text of Msg as text) is "" then -- outgoing messages have no headers
set incomingMessage to false
else
set incomingMessage to true
end if
set AppleScript's text item delimiters to ", "
-- get the sender of the message:
set theOriginator to originator of Msg
set origName to display name of theOriginator
set origEmail to address string of theOriginator
set theOriginator to origEmail & " (" & origName & ")"
-- get the recipients of the message:
set toRecipients to my addressConstructor(to_recipients of Msg) as text
set ccRecipients to my addressConstructor(cc_recipients of Msg) as text
set bccRcipients to my addressConstructor(bcc_recipients of Msg) as text
-- get the date information of the message:
set msgSent to time sent of Msg
-- if you're using MacOS 8.x/9.x and you'd like the date & time format resemble Mailsmith's display format,
-- get & install the "Akua Sweets" osax (<ftp://ftp.akua.com/pub/mac/AkuaSweets/>) and uncomment the
-- next six commented lines below:
-- set timeSent to (the clock using system form "%T" from msgSent) -- uses "Akua Sweets" osax (command: "the clock")
-- set dateSent to (the clock using system form "%d" from msgSent) -- uses "Akua Sweets" osax (command: "the clock")
-- set msgSent to dateSent & " " & timeSent
if incomingMessage then
set msgReceived to time received of Msg
-- set timeReceived to (the clock using system form "%T" from msgReceived) -- uses "Akua Sweets" osax (command: "the clock")
-- set dateReceived to (the clock using system form "%d" from msgReceived) -- uses "Akua Sweets" osax (command: "the clock")
-- set msgReceived to dateReceived & " " & timeReceived
else
set msgReceived to ""
end if
-- merge the message information:
set msgInfo to "Subject: " & theSubject & return & ¬
"From: " & theOriginator & return & ¬
"To: " & toRecipients & return & ¬
"CC: " & ccRecipients & return & ¬
"BCC: " & bccRcipients & return
if incomingMessage then
set msgInfo to msgInfo & "Sent: " & msgSent & return & ¬
"Received: " & msgReceived & return & return & return
else
set msgInfo to msgInfo & "Sent: " & msgSent & return & return & return
end if
else
set msgInfo to ""
end if
end tell
if ConcatenateMessages then
set setextHeader to (theSubject & " (" & origName & ")")
set AppleScript's text item delimiters to ""
set setextLine to (characters 1 thru (length of setextHeader) of HeapsOfDashes) as string
if i < ct then
set ProgressInfo to ("-> processing message " & (i + 1) & " (of " & ct & "), please wait..." & return & "-> (press command-. to cancel)") as string
tell window 1 of application "BBEdit 6.5" -- append message to eof:
set lines -2 thru -1 to "" -- remove progress info at eof
set selection to setextHeader & return & setextLine & return & return & msgInfo & selectedText & ¬
return & return & messageSeparator & return & return & ProgressInfo
select insertion point after character -1 -- go to eof (BBEdit 6.x) in order to show the ProgressInfo
end tell
else -- i = ct -> last message
tell window 1 of application "BBEdit 6.5" -- append message to eof:
set lines -2 thru -1 to "" -- remove progress info at eof
set selection to setextHeader & return & setextLine & return & return & msgInfo & selectedText
-- select character 0 -- go to line 1 (BBEdit 5.x)
select insertion point before character 1 -- go to line 1 (BBEdit 6.x)
end tell
end if
else -- copy each message into it's own window:
tell application "BBEdit 6.5"
activate
make new window with properties {name:theSubject, contents:msgInfo & selectedText}
-- select character 0 -- go to line 1 (BBEdit 5.x)
tell window 1
select insertion point before character 1 -- go to line 1 (BBEdit 6.x)
end tell
end tell
end if
on error number -128 -- user canceled
if ConcatenateMessages then
tell window 1 of application "BBEdit 6.5" to set lines -2 thru -1 to "•• Processing of messages was canceled! ••"
end if
error number -128
end try
end TransferMsg
-- this section handles building full addresses from the parts that Mailsmith provides:
on addressConstructor(theRecipients)
tell application "Mailsmith 1.5"
set recipientList to {}
if theRecipients is not {} then
set theRecipients to theRecipients as list
repeat with i from 1 to count items of theRecipients
set theName to display name of item i of theRecipients
set theAddress to address string of item i of theRecipients
set thePerson to theAddress & " (" & theName & ")" as text
set recipientList to recipientList & thePerson
end repeat
end if
recipientList
end tell
end addressConstructor
|