Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Get properties of Mail message in frontmost window Apps
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.]
    •    
  • Currently 2.54 / 5
  You rated: 4 / 5 (13 votes cast)
 
[5,771 views]  

Get properties of Mail message in frontmost window | 1 comments | Create New Account
Click here to return to the 'Get properties of Mail message in frontmost window' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Get properties of Mail message in frontmost window
Authored by: BlackPenguin on Feb 16, '10 06:50:10PM
I haven't tested this extensively, but in my cursory efforts, I found that this script didn't work for me; it displayed an error stating that Mail could not get a particular mailbox. On the other hand...

tell application "Mail"
	set target_message to the selection
	return properties of item 1 of target_message
end tell

...worked just fine for me, so maybe I don't understand the bug to which the original poster was referring. Thoughts?

[ Reply to This | # ]