• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Send Mail Headers and Contents to BBEdit
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Send Mail Headers and Contents to BBEdit


  • Subject: Send Mail Headers and Contents to BBEdit
  • From: Christopher Stone <email@hidden>
  • Date: Mon, 04 Aug 2014 20:20:18 -0500

Hey Folks,

Since Apple in their infinite wisdom removed the ability to search Mail's message source window in Mountain Lion I got fed up and wrote a script to send the information I usually want to see to BBEdit (easily changed to TextWrangler - BBEdit's freeware sibling).

It requires that the Satimage.osax be installed.  ( No heckling Shane! :)

It's a work-in-progress, but I'm throwing it out there on the off-chance someone else will find it useful.

* I run BBEdit 24/7, so its available instantly for such things.

* My question to myself is: "Why didn't I do this sooner?"

--
Best Regards,
Chris

-------------------------------------------------------------------------------------------
# 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
# Osax: Satimage.osax { http://www.satimage.fr/software/en/downloads/downloads_companion_osaxen.html }
# 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
  if listID contains "applescript-users.lists.apple.com" then
    set content_ to removeAsulJunk(content_)
  end if
  set content_ to stripWhiteSpaceTopBottom(content_)
  set headers_ to cng("\\A\\s+|\\s+\\Z", "", headers_) of me

  

  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)
  set textData to cng("\\A\\s+|\\s+\\Z", "", textData) of me
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
-------------------------------------------------------------------------------------------

 _______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users

This email sent to email@hidden

  • Follow-Ups:
    • Re: Send Mail Headers and Contents to BBEdit
      • From: Shane Stanley <email@hidden>
    • Re: Send Mail Headers and Contents to BBEdit
      • From: "S. J. Cunningham" <email@hidden>
  • Prev by Date: Re: Applescript AeroSnap
  • Next by Date: Re: Applescript AeroSnap
  • Previous by thread: Re: find problem
  • Next by thread: Re: Send Mail Headers and Contents to BBEdit
  • Index(es):
    • Date
    • Thread