Get properties of Mail message in frontmost window

Jan 19, '10 07:30:00AM

Contributed by: mr. applescript

There's a bug in Mail that prevents an AppleScript from accessing the properties (including the content) of an existing mail message displayed in its own window, which is frontmost. The following is a workaround that locates the message using the window title in order to determine its unique identifier, and then creates the corresponding AppleScript message object. It's a bit of a hack, but it often works!

tell application "Mail"
  activate
  set the window_title to the name of window 1
  set AppleScript's text item delimiters to " — "
  if the (count of text items of the window_title) is not 1 then
    copy every text item of the window_title to {message_subject, message_mailbox}
  else
    copy every text item of the window_title to {message_subject}
    set message_mailbox to "Drafts"
  end if
  set AppleScript's text item delimiters to ""
  if the message_mailbox is "Inbox" then
    set the target_messages to the every message of inbox of message viewer 1 whose subject is the message_subject
  else if the message_mailbox is "Sent" then
    set the target_messages to the every message of sent mailbox of message viewer 1 whose subject is the message_subject
  else if the message_mailbox is "Trash" then
    set the target_messages to the every message of trash mailbox of message viewer 1 whose subject is the message_subject
  else if the message_mailbox is "Junk" then
    set the target_messages to the every message of junk mailbox of message viewer 1 whose subject is the message_subject
  else if the message_mailbox is "Drafts" then
    set the target_messages to the every message of drafts mailbox of message viewer 1 whose subject is the message_subject
  else
    set the target_messages to the every message of mailbox message_mailbox whose subject is the message_subject
  end if
  set the message_count to the count of target_messages
  if the message_count is greater than 1 then
    display dialog (message_count & " messages have the same subject as the one in the front window." & return & return & "Do you want to browse them to locate the one displayed in the front window?") as string
    repeat with i from 1 to the message_count
      set selected messages of message viewer 1 to {(item i of the target_messages)}
      if i is the message_count then
        set the button_list to {"Cancel", "Select"}
      else
        set the button_list to {"Cancel", "Select", "Continue"}
      end if
      display dialog "Choose the selected message?" buttons button_list default button (last item of button_list)
      if the button returned of the result is "Select" then
        set the target_message to item i of the target_messages
        exit repeat
      end if
    end repeat
  else
    set the target_message to item 1 of the target_messages
  end if
  -- ACTION WITH MESSAGE
  -- examples:
  -- move the target_message to mailbox "Drafts"
  -- get the content of the target_message
  
  return properties of the target_message
end tell
[robg adds: I can't confirm the bug, but the above code did work for me in 10.6.2.]

Comments (1)


Mac OS X Hints
http://hints.macworld.com/article.php?story=20100115103533249