-------------------------------------------------------------------------------------------
# Auth: Christopher Stone
# dCre: 2014/07/31 12:04
# dMod: 2014/08/04 20:00
# Appl: Mail
# Task: Send Mail Headers and Contents to BBEdit
# Libs: None
# Tags: @Applescript, @Script, @Mail, @BBEDIT, @Satimage.osax
# Note: Tested only on OSX 10.9.4
-------------------------------------------------------------------------------------------
--» MAIN
-------------------------------------------------------------------------------------------
try
set sep_ to "-----------------------------------------------------------------------------------------------------------------"
tell application "Mail"
set selMsgList to selection
if selMsgList ≠ {} then
set selMsg to item 1 of selMsgList
tell selMsg
set headers_ to all headers
set content_ to content
set listID to headers whose name is "List-Id"
if length of listID = 1 then
set listID to content of item 1 of listID
end if
end tell
end if
end tell
set from_ to fnd("^From:.*", headers_, 0, 0, 1) of me
set subject_ to fnd("^Subject:.*", headers_, 0, 0, 1) of me
set date_ to fnd("^Date:.*", headers_, 0, 0, 1) of me
set to_ to fnd("^To:.*", headers_, 0, 0, 1) of me
if fndBool("^Reply-To:.*", headers_, 0, 0, 1) of me ≠ false then
set replyTo to result
else
set replyTo to ""
end if
if fndBool("^X-Sender:.*", headers_, 0, 0, 1) of me ≠ false then
set xSender to result
else
set xSender to ""
end if
set smallHeaders to join {xSender, from_, subject_, date_, replyTo, to_} using linefeed
set smallHeaders to cng("^[[:blank:]]*$[\\n\\r]?", "", smallHeaders) of me
set smallHeaders
to cng
("^([^:]+:) ", "\\1\\t", smallHeaders
) of me set smallHeaders to do shell script "column -t -s'" & tab & "' <<< " & quoted form of smallHeaders without altering line endings
set smallHeaders to cng("^([^:]+)(:)([[:blank:]]+)", "\\1\\3\\2 ", smallHeaders) of me
# set headers_ to cng("^(From|Subject|Date|To|X-Sender|Reply-To):.*\\s", "", headers_) of me
set content_
to cng
("\\x{FFFC}", "", content_
) of me set content_ to removeAsulJunk(content_)
end if
set content_ to stripWhiteSpaceTopBottom(content_)
set text_ to sep_ & linefeed & ¬
smallHeaders & ¬
sep_ & ¬
linefeed & linefeed & ¬
content_ & ¬
linefeed & linefeed & ¬
sep_ & linefeed & ¬
"MAIL HEADERS" & linefeed & ¬
sep_ & linefeed & ¬
headers_ & linefeed & ¬
sep_ & linefeed
tell application "BBEdit"
activate
set newDoc to make new document with properties {text:text_, bounds:{340, 44, 1380, 1196}}
tell newDoc
select insertion point before character 1
end tell
end tell
on error e number n
stdErr(e, n, true, true) of me
end try
-------------------------------------------------------------------------------------------
--» HANDLERS
-------------------------------------------------------------------------------------------
on cng(findText, changeText, srcData)
change findText into changeText in srcData with regexp without case sensitive
end cng
-------------------------------------------------------------------------------------------
on fnd(_find, _data, _case, _all, strRslt) # Last 3 are all bool
try
find text _find in _data case sensitive _case all occurrences _all string result strRslt with regexp
on error
return false
end try
end fnd
-------------------------------------------------------------------------------------------
on fndBool(_find, _data, _case, _all, strRslt)
try
find text _find in _data case sensitive _case all occurrences _all string result strRslt with regexp
true
on error
false
end try
end fndBool
-------------------------------------------------------------------------------------------
on removeAsulJunk(textData)
set textData to cng("(?m)_______________________________________________.+?AppleScript-Users mailing list.+?This email sent to.+", "", textData) of me
end removeAsulJunk
-------------------------------------------------------------------------------------------
on stripWhiteSpaceTopBottom(textData)
end stripWhiteSpaceTopBottom
-------------------------------------------------------------------------------------------
on stdErr(e, n, beepFlag, ddFlag)
set e to e & return & return & "Num: " & n
if beepFlag = true then
beep
end if
if ddFlag = true then
tell me
set dDlg to display dialog e with title "ERROR!" buttons {"Cancel", "Copy", "OK"} default button "OK"
end tell
if button returned of dDlg = "Copy" then set the clipboard to e
else
return e
end if
end stdErr
-------------------------------------------------------------------------------------------