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.)
-------------------------------------------------------------------------------------------
# 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
-------------------------------------------------------------------------------------------