mail.app: 'filter' unread messages

hi,

i’m trying to extract the sender and subject of any unread messages of the in mailbox. though i’m more a designer than a scripter i modified some scripts i found elsewhere to do what i want. right now the script i put together diplays the sender of all messages, and put each set in a seperate dialog.


tell application "Mail"
	try
		set n to junk mailbox
		repeat with i from 1 to count of messages of n
			set msg to message i of n
			tell msg
				set sndr to sender as string
				set sbjct to subject as string
			end tell
			my rslt(sndr, sbjct)
		end repeat
	on error errstr number errnum
		return errstr & return & "Error number: " & errnum as string
	end try
end tell

on rslt(sndr, sbjct)
	display dialog sndr & return & return & sbjct & return buttons {"OK"} default button "OK" with icon note
end rslt

well, that’s it right now … i’m really proud that i got so far on my own :slight_smile:
but:
→ i have no clue how to get the sender/subject of only the new messages
→ next i’d better like to get the result as a string like this: “sender1”, “subject1”, “sender2”, “subject2”, …

any help available? thnx
.claire

okay, i finally did it :)))


tell application "Mail"
	try
		set n to junk mailbox
		repeat with i from 1 to count of (every message in n whose is read is false)
			set msg to message i of n
			tell msg
				set sndr to sender as string
				set sbjct to subject as string
			end tell
			my rslt(sndr, sbjct)
		end repeat
	end try
end tell

on rslt(sndr, sbjct)
	display dialog sndr & return & return & sbjct & return buttons {"OK"} default button "OK" with icon note
end rslt

→ but how do i get the result into the prefered string right now?

who’s the first to help; or will i succeed again :wink:
ready - steady - go
.claire

If you have a mailbox named “Junk”, this should work:

tell application "Mail" 
    try 
       set n to mailbox "Junk"
       repeat with i from 1 to count of (every message in n whose is read is false) 
          set msg to message i of n 
          tell msg 
             set sndr to sender as string 
             set sbjct to subject as string 
          end tell 
          my rslt(sndr, sbjct) 
       end repeat 
    end try 
 end tell 
  
 on rslt(sndr, sbjct) 
    display dialog sndr & return & return & sbjct & return buttons {"OK"} default button "OK" with icon note 
 end rslt

ooh – i’m sorry for misleading you in the wrong direction: that’s not exactly what i want the script to do.

→ i’d like to display the sender and subject of all new junk mails of every account.
i don’t have created a special mailbox called “Junk”, but as i have enabled junk mail detection Mail automatically created a mailbox-group(?) called “Junk” with a submailbox for each account (pop3 and imap as well). different to my script your modification funnily displays only all junk mails of _one_of my accounts.

→ what i’d like the script to do right now is to not to display a dialog with sender/subject of each new junk mail, but to put the result “rslt” in a string like this: “sender1, subject1, sender2, subject2, sender3, subject3, …”
i apologize if this may be so dumb easy that you can’t believe someone asking this … but as i said scripting usually is too abstract to me; and though i’m still trying i have no clue yet how to change this …

would be great if someone might give a hint
thnx
.claire

Maybe this is closer to what you want. I don’t have ‘junk mailbox’ so it might not work. :slight_smile:

set rslt to ""

tell application "Mail"
	--try
	set n to junk mailbox
	set unreadMessages to every message in n whose is read is false
	if (count of unreadMessages) is 0 then return -- terminate script if no unread messages are present
	repeat with thisMessage in unreadMessages
		tell thisMessage
			set rslt to rslt & (sender & ", " & subject & ", ")
		end tell
	end repeat
	set rslt to text 1 thru -3 of rslt
	--end try
end tell
rslt

Y E A H !
that’s it!
great!
so now this part of my project is complete and i’m ready to advance the next steps.

thanks a lot :slight_smile:
.claire

I’m glad it works. :smiley:

– Rob