I was trying to understand why it was so difficult to construct the path to a message file, when I was interrupted by this problem. (It ought to be easy to do this.) I originally thought the problem had nothing to do with AppleScripting, however.
When I returned to my script, I discovered precisely why I was having difficulty constructing a path to a message file and it's directly related to the problem that I bug-reported. The following script will illustrate the problem, provided you have an imap (Me) account to test it on.
I have appended this script to Bug ID# 6954718.
(*
This script illustrates a problem with Mail's mailboxes and accounts. (Leopard 10.5.7)
If the selected mailbox is "On My Mac", there is no account name or directory (understandable),
yet, getting its account does not error. (Why?)
If the selected mailbox belongs to a POP account, then the name, account name and directory all exist.
If the selected mailbox belongs to an imap (Me) account and the mailbox is under the heading Inbox or under the account heading,
then the name, account name and directory all exist.
But if the mailbox is under the heading Sent, Trash, Junk or Drafts, then there is neither an account name nor an account directory.
*)
tell application "Mail"
activate
set frontViewer to (some message viewer whose index is 1)
set mailBoxList to (selected mailboxes of frontViewer) as list
if (count items of mailBoxList) ≠ 1 then return
--
set mbox to (item 1 of mailBoxList)
set mailboxName to (name of mbox) as text
try
set acnt to (get account of mbox) -- OnMyMac OK; pop OK; imap OK.
on error
display dialog mailboxName & return & "[has no account]"
return
end try
try
set acntName to (name of acnt) as text
-- no error if imap account is inbox or user mailbox or pop account
on error
set acntName to "[No Name]"
-- error if imap account is sent, trash, junk or drafts
end try
try
set acntDir to (account directory of acnt) as text -- OnMyMac errors; imap errors except inbox and TEST; pop OK.
on error
set acntDir to "[No directory]"
end try
"Mailbox name = " & mailboxName & return & return & "Account name = " & acntName & return & return & "Directory = " & acntDir
display dialog the result
end tell -------------------------------------