Extracting Mail.app message elements

I’m trying to extract sender, recipient, to recipient, cc recipient and bcc recipient info from the frontmost window (assuming it’s an open message window). I can’t, however, figure out the syntax to access anything other than these properties of the window: modal, closeable, name, resizable, index, miniaturizable, floating, bounds, zoomed, visible, document, class, titled, zoomable, id, miniaturized.

I thought i might be able to come at the problem using the id, but i can’t figure out how to get any info from any message. For example, this code:
tell application “Mail” to return content of first message
produces this error: “Error: 10. NSCannotCreateScriptCommandError” :cry:

Claris eMailer was a scripter’s dream; Mail.app is a scripter’s nightmare! Sagely words of wisdom, or other profound insights, or even a little black magic, would be greatly appreciated at this point. And i’m not above making a sacrifice of a goat or two at midnight, if that’s what this takes. TIA.

I don’t normally mess with Mail and I generally become frustrated when I do. Here’s some basic stuff that was revealed only because Script Debugger is so user-friendly. There is likely a better way to refer to the frontmost message.

tell application "Mail"
	set foo to a reference to item 1 of (get selection)
	tell foo
		set subject_ to subject
		set sender_ to sender
		set replyto to reply to
		set content_ to content
		set source_ to source
		set recipients_ to to recipients -- list
		
		set cc_ to cc recipients -- list
		set ccNames to {}
		set ccAddresses to {}
		repeat with recip_ in cc_
			copy name of recip_ to end of ccNames
			copy address of recip_ to end of ccAddresses
		end repeat
		
		set bcc_ to bcc recipients -- list
		set sent_ to date sent
		set size_ to message size
	end tell
end tell

– Rob (a Eudora user)

Thanks Rob, those were the sagely words of wisdom i was looking for. Now how many goats do i owe you? :smiley:

I’m sure that you’ll repay me one day with an answer to one of my questions. :wink:

– Rob

Looks as though i spoke too quickly. That doesn’t reference the frontmost window, only the message which is selected in the message browser (which may or may not be the frontmost window). :?

Guess you won’t be getting those goats after all. It’s lookin’ more and more like i have to make a pact with the devil to solve this one. Guess that maybe you just can’t get there from here. Why is it that Apple has such piss-poor AppleScript support in core MacOS X technologies? I really wish that NeXT, er, i mean Apple, would support AppleScript better. :rolleyes:

Your post is misleading. If you want window properties, then something like this:

tell application “Mail”
set window_props to ¬
{modal, closeable, name, resizable, index, miniaturizable, floating, ¬
bounds, zoomed, visible, document, class, titled, zoomable, id, miniaturized} of front window
end tell

or this:

tell application “Mail”
set window_props to ¬
properties of front window
end tell

or you can just get one property

tell application “Mail”
set modal_prop to modal of front window
end tell

I really don’t think my post is misleading at all. I don’t want the properties of the window (as your example provides). As i said in my post, i’m after the elements of the message of the frontmost window (and one property of that message). Where i seem to be having problems is referencing the message associated with the window.

I’m beginning to think it just can’t be done, and that ticks me off just a bit cause Apple has done a very poor job of AppleScript support in MacOS X in general, in my opinion. And adding insult to injury, they gave us NeXT Mail instead Claris eMailer (which Apple also owns). eMailer was a scripter’s dream, and a darn good eMail client with a refined UI. But it just sits on the shelf and rots while we have to put up with crummy ol’ regurgitated NeXT Mail. Apple politics, pure and simple. :x

Sorry for the rant. I’m still hoping someone can prove me wrong, but i’m really starting to think this just can’t be done. (Would be a piece of cake in eMailer.) Very frustrating!

Hi Rainy Day,

I think I know what you’re trying to do now. You’re trying to get a reference to the outgoing message of New Message window, right? It can be done. I have 2 similar ideas so far, sideways. I show you how when I get home unless someone else comes up with a solution.

gl,
Kel.

Yes, that’s exactly right. :smiley: Or any other open window, for that matter.

What i failed to mention up until now, and i now understand has been a cause of some confusion here, is that i use Mail in two pane mode (if you count the mailbox drawer as a pane), rather than the default three pane mode. Long ago i dragged the horizontal bar separating the browser from the message area all the way to the bottom of the window (thus collapsing the message area completely, and requiring that messages be opened in their own independent windows for viewing.) I had forgotten that most folks probably don’t use Mail in this mode.

Ideally i’d like to be able to access either a newly created outgoing message, or any other independently open message window (independent of the browser window, that is), whatever happens to be window 1 at the moment.

Hi Rainy Day,

The basic idea is names of message windows are the subjects of messages. However, messages without subjects may have a default name or any name that you can set with AppleScript. Anyway here’s a rough draft that you can fix up. You can probably find many shortcuts once you get the idea and there may be a bug with certain outgoing messages.

set temp_subject to “r1nurm7uy”
tell application “Mail”
activate
– build messages list
set mb_list to {in mailbox, out mailbox, drafts mailbox, sent mailbox, trash mailbox}
set mailbox_names to {name of in mailbox, ¬
name of out mailbox, ¬
name of drafts mailbox, ¬
name of sent mailbox, ¬
name of trash mailbox}
set user_mailboxes to (every mailbox)
repeat with this_mailbox in user_mailboxes
if name of this_mailbox is not in mailbox_names then
set end of mb_list to (contents of this_mailbox)
end if
end repeat
set message_list to {}
repeat with this_mailbox in mb_list
set these_messages to every message of this_mailbox
set message_list to message_list & these_messages
end repeat
– get a reference to the message of front window
set the_message to missing value
repeat with this_message in message_list
set this_subject to subject of this_message
– if this_subject is “”, then this message is outgoing or draft with no subject
– if the front window contains a draft message, then it is outgoing
if name of front window is this_subject then
set the_message to (contents of this_message)
exit repeat
end if
end repeat
– drafts become outgoing when double clicked
if the_message is missing value then
set out_messages to every outgoing message whose subject is “”
set window_name to name of front window
repeat with this_message in out_messages
set subject of this_message to temp_subject
delay 2
if name of front window is temp_subject then
set the_message to (contents of this_message)
end if
set subject of this_message to “”
set name of front window to window_name
delay 2
if the_message is not missing value then exit repeat
end repeat
end if
if the_message is missing value then
display dialog “There is no message connected to the browser window!”
end if
end tell
the_message

gl,

I almost forgot. You need to check for more than one messgae with same subjects as the window name.

gl,

Hi,

I just thought of a better way maybe. First, you make a list of every message in every mailbox with the same subject as the name of the front window and include the outgoing messages. Then, you change the subject of each of these messages and check if the window name changed until you find the right message. Lastly, you reset everything. Maybe some rest might be good.

gl,
Kel.

Hi,

I think you are right. You can’t get a reference to an in message of a window where there are more than 1 message with the same subject. My idea will only work with outgoing messages even if they have the same subject.

Sorry about that. I tried all night.

gl,

Kel,

Thanks for the valiant effort! I think it’s probably time to just give up on this one, though. It’s just not worth the time to pursue this any further. :frowning: I really appreciate your efforts, however (and everybody else too).

I’ve concluded that Mail’s AppleScript support is very poor (like too many other MacOS X apps from Apple). :rolleyes: Too bad Apple doesn’t appreciate the value of AppleScript! (How’s that for irony?) I think we need to send some NeXT engineers to AppleScript school. They really ought to take a peek at the old Claris eMailer code to see an example of really good AppleScript support.


On the applescript-users mailing list, one of the members of the Mail dev team took a lot of heat about Mail’s AppleScript support. I think that he/she must have unsubscribed or just quit responding. Hopefully Panther’s Mail will be much improved as a result of the bitchi… er… feedback.

– Rob