• 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
Edit Message from Mail.app in BBEdit
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Edit Message from Mail.app in BBEdit


  • Subject: Edit Message from Mail.app in BBEdit
  • From: Christopher Stone <email@hidden>
  • Date: Sun, 14 Apr 2013 07:02:13 -0500

Hey Folks,

Like me I'm sure some of you miss Eudora's Pencil button for editing a received email message.

I finally revamped my script for editing a Mail.app message in BBEdit and have it working flawlessly (so far) on my machine with Mountain Lion.

TextWrangler (freeware) is a viable alternative to BBEdit, and the mod is simple (just change 'BBEdit' to 'TextWrangler').

I run mine from FastScripts via Cmd-Opt-Shift-B, but of course there are other options.

One key thing you need to know about editing the raw text of a Mail message is:

30188 <-- The first line of the message text indicates the number of characters until first metadata line.
...
<?xml version="1.0" encoding="UTF-8"?>  <-- First metadata line.

If you edit the message and don't change that number to the correct count the message will not display properly.

Somewhere I have a script I wrote to automatically calculate and change that number, but I can't find it at the moment.  (I rewrote it - only lightly tested - appended.)

(Of course I know the method of editing emails by putting them in the drafts folder, but I generally don't like doing that due to the loss of all header info and metadata.)

Mail will not register the edit until you do one of the following:

* Restart Mail.
* Rebuild the mailbox containing the message.
* Move the message to a different mailbox.
* Copy the message to another mailbox (only the copied one will register until one of the previous options is used).

Enjoy.

--
Best Regards,
Chris

-------------------------------------------------------------------------------------------
# Auth: Christopher Stone <email@hidden>
# dCre: 2012-07-18 : 05:51
# dMod: 2013-04-14 : 04:20
# Appl: Mail & BBEdit
# Task: Open selected message's file in BBEdit for editing.
# Deps: Uses only OSX components.
# Compatibility: Lion & Mountain Lion (might work in Snow Leopard but I cannot test).
# Tags: @Applescript, @Mail, @BBEdit, @Edit, @Message
-------------------------------------------------------------------------------------------
--» HANDLERS
-------------------------------------------------------------------------------------------
on REPL(_str, _find, _repl)
  set {oldTIDS, AppleScript's text item delimiters} to {AppleScript's text item delimiters, _find}
  set _str to text items of _str
  set AppleScript's text item delimiters to _repl
  set _str to _str as text
  set AppleScript's text item delimiters to oldTIDS
  return _str
end REPL
-------------------------------------------------------------------------------------------
on textToBBEdit(_data)
  if _data starts with "/" then
    try
      set _data to alias POSIX file _data
    end try
  end if
  tell application "BBEdit"
    activate
    if class of _data = alias then
      open _data
    else if class of _data = text then
      set newDoc to make new document with properties {text:_data}
    end if
    set bounds of front window to {303, 44, 1617, 1196}
  end tell
end textToBBEdit
-------------------------------------------------------------------------------------------
--» MAIN
-------------------------------------------------------------------------------------------
try

  

  set AppleScript's text item delimiters to {""}

  

  tell application "Mail"
    set _sel to selection
    if length of _sel = 1 then
      set _msg to item 1 of _sel
    else
      error "Too many messages selected."
    end if
    # Use account to find mail-folder in attempt to be compatible back to Snow Leopard.
    set acnt1Dir to (account directory of account 1) as string
    set {oldTIDS, AppleScript's text item delimiters} to {AppleScript's text item delimiters, ":"}
    set mailFldrLion to POSIX path of (text items 1 thru -3 of acnt1Dir as string)
    set AppleScript's text item delimiters to oldTIDS
    tell _msg
      set mbxName to name of its mailbox
      set mID to its id
      set mSub to its subject
    end tell
  end tell
  # Escape single quotes in subject for spotlight query string.
  if mSub contains "'" then
    set mSub to REPL(mSub, "'", "'\"'\"'")
  end if

  

  # Find email with Spotlight.
  set _cmd to "mdfind -onlyin " & mailFldrLion & " 'kMDItemDisplayName == \"" & mSub & "\"cd && kMDItemFSName == \"" & mID & ".emlx\"cd' | egrep -i \"" & mbxName & "\""

  

  --» •••• Debug Code ••••
  # textToBBEdit(_cmd) of me
  # return

  

  set emlFile to do shell script _cmd
  textToBBEdit(emlFile)

  

on error e number n
  set AppleScript's text item delimiters to {""}
  set e to e & return & return & "Num: " & n
  tell me to set dDlg to display dialog e with title "ERROR!" buttons {"Cancel", "Copy", "OK"} default button "OK"
  if button returned of dDlg = "Copy" then set the clipboard to e
end try

-------------------------------------------------------------------------------------------
# Automagically calculate the correct character number.
-------------------------------------------------------------------------------------------

tell application "BBEdit"
  tell text of front text document
    set metaDataLine to lines whose contents is "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
    if length of metaDataLine = 1 then
      set metaDataLine to item 1 of metaDataLine
    end if
    set _text to it
    set cA to (characterOffset of (last character of line 1)) + 1
    set cZ to (characterOffset of (item 1 of (get lines whose contents is "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"))) - 1
    set charCnt to cZ - cA
    set charCnt to charCnt
    set contents of line 1 to charCnt
  end tell
end tell

-------------------------------------------------------------------------------------------
# Copy and open the edited message for easy checking.
# I use an account that never has any mail in the InBox.
# You can just hit delete after making sure the edits look right, and the original is still
# in place.
-------------------------------------------------------------------------------------------

tell application "Mail"
  set selMsgList to selection
  if selMsgList ≠ {} then
    set selMsg to item 1 of selMsgList
    copy selMsg to mailbox "INBOX" of account "test" of application "Mail"
    tell mailbox "INBOX" of account "mom" of application "Mail"
      open first message
    end tell
  end if
end tell

-------------------------------------------------------------------------------------------

 _______________________________________________
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

  • Prev by Date: Using Home Dir Notation in Do Shell Script
  • Next by Date: Re: duplicate command
  • Previous by thread: Using Home Dir Notation in Do Shell Script
  • Next by thread: AUTO: Allyn Martin is out of office.
  • Index(es):
    • Date
    • Thread