Need help scripting Mail.app in Mavericks

One of the biggest problems with Mavericks seems to be meaningless, random changes to the scripting backbone, and an important script of mine has broken. The scripts basically just grabs the text content of an email that’s arrived with a certain subject. It is:

tell application “Mail”
set the clipboard to (content of first message of inbox whose subject contains “2013-11-22”) as string
end tell

which normally works fine. However, now I get

Mail got an error: Can’t get message 1 of inbox whose subject contains “2013-11-22”. Invalid index.

as an error. I’ve spent 6-7 hours at least trying to find a workaround, e.g. go through the first 200 items in the in box looking for a certain subject, but nothing works.

Can anyone put me on the right path?

Hi ppayne,

It worked for me:

tell application "Mail"
	set the clipboard to (content of first message of inbox whose subject contains "Music") as string
end tell

gl,
kel

Model: MBP - OS10.9
AppleScript: AS 2.3
Browser: Safari 7.0
Operating System: Other

And me which suggests it cannot find it…

So taking any question of human error out of the equation… :slight_smile:

what do you get with

tell application “Mail”
set theSub to (subject of first message of inbox) as string

set the clipboard to (content of first message of inbox whose subject contains theSub) as string

end tell

Hello Mark.

This doesn’t work for me neither, but I guess I’m having some accounts that I can’t/doesn’t log into, and that that is the culprit in my case. :confused:

Hi Mark,

I’m not sure if you can coerce all rtf into text. That might be it. I’ll try your tester.

Edited: I just deleted all my mail and have no new ones. :frowning: :slight_smile: I’d hate to send myself email with attachments or pictures.

gl,
kel

I’m not on Mavericks (yet), but
it’s always a good habit to keep all Standard Additions commands out of application tell blocks if possible


tell application "Mail"
	set theSub to (subject of first message of inbox) -- subject is always text
	set theContent to (content of first message of inbox whose subject contains theSub) as text
end tell

set the clipboard to theContent


Hi Mark,

I sent myself a message with just text and it worked:

tell application "Mail"
	set theSub to (subject of first message of inbox) as string
	
	
	set the clipboard to (content of first message of inbox whose subject contains theSub) as string
	
end tell

clipboard pasted result: This is a test.

gl,
kel

I’m wondering how it works with just a text email with no attachments or pictures.

Stefan is right. You shouldn’t use standard additions in tell blocks as a rule of thumb.

Mail Version 7.0 (1822)

In itself, the rule of thumb is an interesting. How did that expression come about? The thumb is so important. Without the opposing thumb mankind wouldn’t be here I think.

Are you using Mavericks? That gives the same error for me, even adjusting the “contains” part to match something I know is there.

For me, I also get an error, Mail got an error: Can’t get message 1 of inbox. Invalid index. Just in case I’ll delete the spotlight index and rebuild in case that changes anything.

Strange. I built os10.9 over os10.8.5 if that means anything. Maybe something remained set.

gl,
kel

Another thing is that I’m not using iCloud if that means anything. Only On My Mac.

gl,
kel

Odd, it looks so reasonable and yet I still get Mail got an error: Can’t get message 1 of inbox. Invalid index. This is after I rebuilt my Spotlight index, though I didn’t expect that to fix anything.

Tomorrow I’ll try this on some other computers, in case I’ve got something strange about my Mac setup.

invalid index means that the mailbox inbox is empty.
Mail.app retrieves the messages and mailbox hierarchy directly from the mail database.
Only the (virtual) contents of the smart mailboxes are affected by Spotlight.

Does this code display a non-zero value?

tell application "Mail" to set inboxCount to count messages of inbox
display dialog inboxCount as text

If you mean in the error where it says something like

“Mail got an error: Can’t get message 1 of inbox whose subject contains “xxxx”. Invalid index.”

I think that is misleading. It really means it cannot find anything to return.
My mailboxes are not empty and the code works. But does return this error if I search for something that is not found.

Of course you’re right, Mark.
Actually I meant the filter results

Thanks everyone for your help. This is definitely odd.

So this does work for me:

tell application “Mail”
set the clipboard to (content of first message of inbox whose subject contains “music”) as string
end tell

but if I change the string to be found to “reports” I get an error, even though I’m looking at an email with that in the subject.

If I try

tell application “Mail” to set mymsg to (messages of inbox whose subject contains “J-List”)

I can get the list of messages that start with (not “contains” it seems) the phrase J-List, and access them, open them, get contents from them. But if I try to specifically do this for mails whose subject contains “J-List Reports” then it breaks again, even though I am looking at the mail with that in the subject.

Sadly it seems to just be broken.

The only way I can think of accessing the mails I need is to loop it and check each message. The following works okay

tell application “Mail”
set inboxCount to count messages of inbox
repeat with i from 1 to inboxCount
set the clipboard to (subject of message i of inbox) – subject is always text
set theSub to (subject of message i of inbox) – subject is always text
set myoffset to offset of “J-List” in theSub
if myoffset is not 0 then
say “found report”
set theContent to (content of first message of inbox whose subject contains theSub) as string – rich text
open message i of inbox
end if
end repeat
end tell

but if I change my search string from “J-List” to “J-List reports” then I get nothing.

So it seems there may be two errors

a) a huge weirdness in indexing that causes some Applescript logic to be unrealiable (can anyone else confirm?)

and

b) since the mails I’m trying to grab content from are plain text, not HTML or anything, there seems to be a “blindness” via Applescript. No script I make which carefully checks each message for my criteria will “see” the mails I want, but seem to be okay with seeing normal HTML mails.