unable to move outlook emails based on recipient

I’ve been getting lots of emails to mailing lists of which I’m a part of.

I’ve created the following script:

tell application "Microsoft Outlook"
	
	set myInbox to folder "Inbox" of default account
	set archive to folder "Archive" of default account
	
	set theMessages to messages of myInbox
	
	set countMoved to 0
	
	repeat with theMessage in theMessages
		try
			set theRecipient to email address of recipient of theMessage
			set toAddress to address of theRecipient
			
			# Start of mail specific rules
			
			if toAddress is not "myemail@example.com" then
				move theMessage to folder "Not to me" of archive
				set countMoved to countMoved + 1
				
			end if
			
		end try
	end repeat
	
	
	log "Outlook cleanup ran " & " - " & countMoved & " moved."
	
end tell

Unfortunately it’s not able to pick any emails.

This is a typical reply:

get email address of recipient of incoming message id 938
→ {{name:“Person 1”, address:"person1@example.com", type class:unresolved address}, {name:“Person 2”, address:"person2@example.com", type class:unresolved address}}

thanks in advance

Maybe, instead of recipient you should use to recipient. And, instead of emaiI address should be address. I uninstalled Microsoft Outlook from my Mac, and don’t remember. So, test yourself.


set theRecipient to (to recipient of theMessage)
set toAddress to address of theRecipient

Because recipient of theMessage is equivalent to every recipient of theMessage, that is the result is a list of different recipients {To recipient, Cc recipient, Bb recipient}. And, you want not the list of them but one specific of them.

Unfortunately it doesn’t work. The reply now is:

{to recipient 1 of incoming message id 1468, to recipient 2 of incoming message id 1468}

No addresses appear.

so, you have 2 to recipients of same Message.

So, to get address you should ask for address of item 1 of theRecipients list, then of item 2 of theRecipients list. Your repeat loop should be replaced with this:


repeat with theMessage in theMessages
		set theRecipients to (to recipient of theMessage) as list
		set isToMe to false
		repeat with toRecipient in theRecipients
			set toAddress to address of toRecipient
			if toAddress is "myemail@example.com" then set isToMe to true
		end repeat
		if not isToMe then
			move theMessage to folder "Not to me" of archive
			set countMoved to countMoved + 1
		end if
	end repeat

When running, Outlook shot out an error saying it can’t find the address.

I changed my original script with your (much better) loop like so:

tell application "Microsoft Outlook"
	
	set myInbox to folder "Inbox" of default account
	set archive to folder "Archive" of default account
	
	set theMessages to messages of myInbox
	
	set countMoved to 0
	
	repeat with theMessage in theMessages
		set theRecipient to email address of recipient of theMessage
		set isToMe to false
		repeat with toRecipient in theRecipient
			set toAddress to address of toRecipient
			if toAddress is "parvez.halim@wintechnologies.net" then set isToMe to true
		end repeat
		if not isToMe then
			move theMessage to folder "Not to me" of archive
			set countMoved to countMoved + 1
		end if
		
	end repeat
	
	
	log "Outlook cleanup ran " & " - " & countMoved & " moved."
	
end tell[/AppleScript]

And it works!! Thanks so much. This was driving me crazy.