Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to get sender of selected message in Mail.app

168 views
Skip to first unread message

Eric Raas

unread,
Feb 17, 2003, 8:22:02 AM2/17/03
to
I'm trying to write a script for Mail.app that will move a selected email in
my inbox into a folder whose name is the sender's email address up to the
'@' symbol. This is how I've been organizing email with pine's Fcc rules
for a long time and would like to replicate this functionality in Mail. For
some reason I am having problems getting the sender of the selected message.
The following applescript code fails, with the message

'can't get sender of message 37 in mailbox...'

tell application "Mail"
set selMessage to selection
set senderName to (get sender of selMessage)
end tell

The funny thing is that it works if I explicitly assign selMessage:

set selMessage to (message 47 of mailbox "INBOX" of account "My Account")

For some reason 'selection' is returning an object that won't respond to
'get sender'

Any ideas? I am surprised that Mail.app does not seem to be recordable...
Thanks for any suggestions!

Rainer Erich Scheichelbauer

unread,
Feb 17, 2003, 11:13:12 AM2/17/03
to
Eric Raas <f...@bar.net> wrote:

> 'can't get sender of message 37 in mailbox...'
>
> tell application "Mail"
> set selMessage to selection
> set senderName to (get sender of selMessage)
> end tell
>
> The funny thing is that it works if I explicitly assign selMessage:
>
> set selMessage to (message 47 of mailbox "INBOX" of account "My Account")
>
> For some reason 'selection' is returning an object that won't respond to
> 'get sender'

Selection returns a *list* of messages, since several messages can be
selected at once. You can either loop through every message in the
selection, or only take the first one, as this example shows:

tell application "Mail"
set mymsglist to selection
set mymsg to item 1 of mymsglist
sender of mymsg
end tell

Hope this helps,
Eric.
--
Use mekka at mac dot com to reply.
http://homepage.mac.com/mekka/mekkablue

Eric Raas

unread,
Feb 17, 2003, 12:52:05 PM2/17/03
to
On 2/17/03 11:13 AM, in article
1fqjbnx.1wzixhl1tw5f0gN%mekka@check_my_sig.invalid, "Rainer Erich
Scheichelbauer" <mekka@check_my_sig.invalid> wrote:

Yes! that did the trick. In hindsight the error message was something like

could not get sender of {message 1 of ...}

I presume now that the curly braces indicate a list. Where would this have
been documented? I looked through an awful lot of documentation and never
saw an indication that selection returned a list. I'm just asking because
maybe there's a good source of info I missed.

Thanks for the very helpful reply.

Eric

Eric Raas

unread,
Feb 17, 2003, 1:05:55 PM2/17/03
to
On 2/17/03 11:13 AM, in article
1fqjbnx.1wzixhl1tw5f0gN%mekka@check_my_sig.invalid, "Rainer Erich
Scheichelbauer" <mekka@check_my_sig.invalid> wrote:

While we are on the topic, do you know what applescript command is
appropriate to move a message to another folder?

In my case I need to move the message to an imap folder (I use imap so that
Mail.app can see my pine folders - which happen to be on the local hard
drive - and want to keep storing messages in these folders). Thanks again!

Eric Raas

unread,
Feb 19, 2003, 12:59:32 PM2/19/03
to
On 2/17/03 1:05 PM, in article BA768E6F.DB9%f...@bar.net, "Eric Raas"
<f...@bar.net> wrote:

For the record I finally did come up with a script to get the email address
prefix and move a messsage to an imap folder with that name (e.g. Messages
from jsm...@foo.org go into the folder 'jsmith'). This was basically to
replicate the default fcc and message save functions in pine, so that I can
use both pine and Mail.app to access my inbox on an imap server and also see
a set of mbox folders on my local hard drive. I did not want to import my
folders to the Mail.app format because I have many and it seemed like a pain
to get folder browsing of Mail.app folders to work in pine. My solution was
to run the Wash U imap server on my mac so that Mail.app can see my pine
folders (with the imap home dir set appropriately). Now when I read mail
with Mail.app I file it using the script below (also works on stuff in the
'Sent' folder - messages I send are filed by primary recipient). Anyhow I
hope this will help someone - I did not find a lot on moving Mail.app
messages to different folders using Applescript but it seems to work well.

Here is the script (watch out for line wraps):


(* script to move a message into a folder whose name is the sender's email
address
up to the '@' symbol *)

set userName to "myname"

tell application "Mail"

set messageList to selection

repeat with theMessage in messageList

--set theMessage to item 1 of messageList
set senderName to (sender of theMessage)
set recipientName to (address of recipient 1 in theMessage)

-- get rid of stuff after the '@'
set AppleScript's text item delimiters to {"@"}
set senderName to (get first text item of senderName)
set recipientName to (get first text item of recipientName)

-- get rid of anything before leading '<'
set AppleScript's text item delimiters to {"<"}
set senderName to (get last text item of senderName)
set recipientName to (get last text item of recipientName)

--figure out what to use for dest folder
--if sent by user, put in recipient's folder
if senderName is equal to userName then
set folderGuess to recipientName
else
set folderGuess to senderName
end if

-- check to see if mailbox exists
if (mailbox folderGuess of account "Pine Folders") exists then
set theResult to display dialog "Sender:" default answer
folderGuess
else
set theResult to display dialog "Sender:" default answer "????"
end if

set mailbox of theMessage to (mailbox (text returned of theResult)
of account "Pine Folders")

end repeat

end tell


0 new messages