Search Subject line in Apple Mail from text string provided

Hi,
I’m looking to take a string of text from another program, open Mail, and set the search area line of mail with the clipboard contents (or field fed from a database).
This would be the same thing as human copies a text string, and pastes in into the search field at top of Mail window as “subject includes”, and be left with a list of emails to peruse, likely a singe conversation thread.

I looked at the post id=26365 that returns the subject results as text or series of .EML files to process, but couldn’t rework into above. That was the closest searching the forum yielded.

I plan to include the option to specify the mailbox, or smartmailbox name, instead of Inbox. Maybe work in some smarter search elements, but for start I just hope to have “1234” as subject content (not date, nor sender).
Thanks!

This should help you get started.


set theSearch to the clipboard as text

activate application "Mail"

	tell application "System Events"
		tell process "Mail"
			tell window 1
				keystroke "f" using {command down, option down}
				keystroke (theSearch)
			end tell
		end tell
	end tell

Hi. The problem with using the search field in this way—besides the fact that GUI scripting is inherently awful—is that that method searches whatever depths Mail feels like searching; it’s not limited to just the subject without the subject token. Avoid the GUI:

set filter to "orientation" --or whatever
set text item delimiters to return

tell application "Mail" to (mailbox ((choose from list (get mailboxes's name))'s item 1)'s messages whose subject contains filter)'s subject as rich text