Find mail by subject and get content to clipboard

So just like the title says, I need get my script to find a message in Mail by the subject line and then pull the content from it and set it to the clipboard so that AS can parse thru the text.

I don’t if this helps, but I’m thinking something like this:

Tell application “Mail”
if theMsg contains “this subject” with less than or equal to 7 days of current date then set msgContent to content
else display dialog “No such message exists”
end if
end tell

Right now, this script works, but I have to select the message and switch to mail before the script runs. Eventually, this will go into AS studio so the whole thing can be triggered by a button in IB. But thats for later.

delay 2

--  XXX  This section tells mail to get data XXX  --
tell application "Mail"
	set foo to a reference to item 1 of (get selection)
	tell foo
		set jazzSubject to subject
		set jazzDate to date sent
		set allJazzStats to content
	end tell
end tell

return jazzSubject & jazzDate & allJazzStats

I’ve looked around for a couple day now, and haven’t gotten it to work so any input would be beyond appreciated.

Hi esamuels,

If you need to find eMails that contain a certain search string in their subject and which are not older than 7 days, you could also use the following AppleScript code utilizing the mdfind command:

set searchstring to "Apple"
-- mdfind -onlyin ~/Library/Mail '(kMDItemContentCreationDate >= $ && (kMDItemSubject == "*Google*")'
set command to "mdfind -onlyin ~/Library/Mail '(kMDItemContentCreationDate >= $ && (kMDItemSubject == \"*" & searchstring & "*\")'"
set emlxfilepaths to paragraphs of (do shell script command)

You could then further process the found *.emlx files with grep to search the contents of the eMails for certain keywords.

Just an idea :wink:

If you’d like to get the messages directly from the mail application instead you can use this… which will get all the messages in the Inbox. Then you can use an if statement to find what you need.

tell application "Mail"
	set inboxEmails to messages in inbox
	repeat with anEmail in inboxEmails
		set dateReceived to date received of anEmail
		set theSubject to subject of anEmail
		set theContent to content of anEmail
	end repeat
end tell

thanks for the help fellas. this is what ive been using and it works great.

tell application "Mail"
	tell inbox
		set theMessage to first message whose subject contains "Mail Subject line/title"
		set theSource to source of theMessage
		set theSubject to subject of theMessage
	end tell
end tell