Simple script to search in Mail.app

Hi,

I’m fumbling around, trying to make a script to run in Automator, as a service, but I can’t manage to make it work. (I don’t know what I’m doing).

I want to have applescript, get the name of the person who’s email is currently selected (either to, or from … I will need 2 scripts). Then I want it to spawn a new window and perform a global search on that person (depending on whether the name was a ‘to’, or ‘from’).

Not much is working, except the new window. This is what I have. Any help will be much appreciated. :slight_smile:

on run {input, parameters}
tell application “Mail”
activate
set theRecipient to (to recipient)
make new message viewer
set search of (message viewer 1) to theRecipient
end tell
return input
end run

Perhaps something like this

tell application "Mail"
	
	set recipientNames to (name of recipients of (item 1 of (get selection)))
	if (count of recipientNames) > 1 then
		set recipientNames to choose from list recipientNames
		if recipientName = false then return false
	end if
	set recipientName to item 1 of recipientNames
	
	set matchingMessages to {}
	repeat with oneMessage in messages of (message viewer 1)
		if (name of recipients of oneMessage) contains recipientName then
			copy oneMessage to end of matchingMessages
		end if
	end repeat
	
	return count of matchingMessages
end tell

Thanks for the reply Mike.

Which bits of that do you think would work?

At the moment, when I run it, nothing much happens. It returns a number that isn’t what I would expect. (Not the number of matching names/messages)