'Ghost' emails in Mail causing problems.

G’day scripter.

I have a situation where a mail rule on a certain Inbox causes mail to be diverted to a mailbox '* items to shift".

My app then periodically moves those stored email to a mailbox “* items to store”, processes them, and stores them according to the processed contents.

My problem is that ‘* items to shift’ has a varying number ( at present 78) of ‘ghost’ emails that are counted, and it’s causing my app to think it’s never finished.

Does anyone know of a way to clear these ‘ghosts’, please. They’ve been driving me bonkers!.

‘Rebuild’ does not work. Only quitting and restarting Mail clears them, (a bit impractical).

Regards

Santa


tell application "Mail"
	say (count of messages of mailbox "* items to shift") --< shows as empty, but counts as 78
	set t to every message of mailbox "* items to shift"
	set x to 0
	repeat with eachmessage in t
		set mailbox of eachmessage to mailbox "* items to store"
	end repeat
	say (count of messages of mailbox "* items to store")
end tell

Yeah, there has been those double email references and you’re probably not imagining things. I’ve been trying to isolate when these doubles occur and will try again with the specifics of your post. Hopefully I can duplicate it with your exact procedure.

Edited: firstly, does this occur in your inbox or in every mailbox?

Edited: are you shifting mail from your inbox?

gl,
kel

Believe it or not, I may have a solution. But, need more info.

G’day Kel

I’m shifting mail from my inbox by mail rule, to the first , temporary mailbox.

Then, my app picks up the whole stored list from that first mailbox and resets each message to a second, processing mailbox.

Ghosts can occur for as short while (I think about 10 seconds), in either mailbox after being shifted, but sometimes these short term ghosts become ‘stuck’ permanently, and I haven’t been able to figure out why.

ATM I’m storing the count of the first mailbox’s messages, and then re-counting on a subsequent pass, and if the number is the same, quitting and re-starting ‘Mail’. Very cumbersome, and it usually takes place after every email, and we get over 400 jobs per day.

Response time is critical, so I check every 4 seconds for new mail, and the mail rule also triggers my App. This means the temporary ghosts are found, and the Quit/Restart Mail handler kicks in.

Regards

Santa

Edit

This procedure shows how mail is still counted in the first mailbox after being shifted.


tell application "Mail"
	activate
	say (count of messages of mailbox "* items to shift")
	set t to every message of mailbox "* items to shift"
	 repeat with eachmessage in t
		set mailbox of eachmessage to mailbox "* items to store"
	end repeat
	 say (count of messages of mailbox "* items to shift")
	say (count of messages of mailbox "* items to store")
end tell

Hi Santa,

So what you might do is copy the mail from the inbox. Then, delete the mail from the inbox. Then, just deal with the mail that you have copied to your temp mailbox. When you copy the email to your temp mailbox, you’ll have only copied one message no matter which of the duplicates you have chosen to copy, in your script.

When you delete the mail that you have copied, note that there may be more than one. Thus, you need to delete every mail including the duplicates.

Note that, you can identify an email uniquily by its internet id. Hence, you can distinguish which emails are duplicates.

I would have to review my previous posts on this internet id, but it is somewhere on this site. If you can’t find it then write back.

gl,
kel

Hi Santa,

And this is not a recent bug in Mail. It has been happening since about 2012 at least I think. The main thing is finding the unique internet id. I think they added a way to get the id in these later oses. So it is not that hard.

I think it’s in the System Events dictionary. Not sure. It’s somewhere (maybe Mail).

gl,
kel

G’day Kel

Thank you, your method works well. Initially slows things down a bit, but that’s more than made up for by my being able to remove all the code that was trying to overcome the ghost problem.

I’ve not used the method on the Inbox, as that wasn’t causing a problem, but on my other two mailboxes. I’ve also stuck with resetting the emails mailbox, rather than copying it.

The method for the first is…

Regards

Santa


tell application "Mail"
	#
	try
		if not (exists mailbox "* items to store") then make new mailbox with properties {name:"* items to store"}
	end try
	try
		set tempMail to every message of mailbox "* items to shift"
		repeat with eachmessage in reverse of tempMail
			set theTempID to message id of eachmessage
			set the mailbox of eachmessage to mailbox "* items to store"
			repeat
				try
					if ((exists (some message whose message id is theTempID)) of mailbox "* items to shift") then
						try
							delete ((some message whose message id is theTempID) of mailbox "* items to shift")
						end try
					else
						exit repeat
					end if
				on error
					exit repeat
				end try
			end repeat
		end repeat
	end try
	say (count of messages of mailbox "* items to shift") --< Now always zero
	say (count of messages of mailbox "* items to store")
end tell

Model: Late 2014 retina i7, Yosemite
AppleScript: 2.4
Browser: Safari 600.2.5
Operating System: Other

Hi Santa,

Don’t know what is taking Apple so long to fix this virtual reference bug in the inbox and it’s a big one. But then, Apple is big on security.

Anyway, glad you got it working until the fix.

gl,
kel

Thanks Kel.

I’ve been working on this particular Application for nine years, gradually expanding it from a plain Applescript to an Xcode project.

In that entire time, Mail has always left ghosts behind, and I’ve lodged several bug reports about it.

Regards

Santa

Hi Santa,

I jumped up from about 2003 to about 2013, so the problem with duplicate emails must have started from somewhere in-between those dates. When I first got this machine, there was no problem with duplicate emails in the inbox until Mavericks (I think or Yosemite), but the virtual references started in Mavericks I think. Anyway, Hope Apple fixes this.

Edited: I try not to read emails anyway nowadays. :smiley:

Have a good day,
kel