Script to recover blown away emails in Tiger- I need a bit of help!

Tiger Mail suddenly blew away my last five years of emails on both my main and backup systems. I managed to get the raw mailbox emails to show up in the finder as xxxx.emix files but the mailboxes in mail don’t recognize that they exist.

I see that there are many others out on the net who have had this same problem. The folks at the Apple Store Genius bar haven’t been able to help and Apple doesn’t have a published fix for this problem.

I wrote the following script to open a folder’s emails one at a time in the finder and then try to have Mail move them into a recovery mailbox. (This process works when I do it manually but my script crashes on the move section.) Apparently I am not properly selecting the message which shows up as an open window in Mail and I assume is up at the root level of Mail.

I tried figuring out the right code by asking Script Editor to record my actions but Mail doesn’t make any effort to record the steps.

This is my first attempt at writing AppleScript so I assume this is an easy fix for someone but it has me stumped. I have tried several approaches with no success. If anyone can help I will happily offer them my first born offspring.

– Script to try to recover Tiger emails and restore them to a mailbox. REK 1/27/05
– Start by opening one recovered mailbox folder’s worth of xxx.emix emails in the Finder
tell application “Finder”
activate
set the source_folder to “:Users:roger:Desktop:Test Folder” as alias
set the item_list to list folder source_folder without invisibles
end tell
repeat with i from 1 to number of items in the item_list
tell application “Finder”
activate
set this_item to item i of the item_list
open document file this_item of folder “Test Folder” of folder “Desktop” of folder “roger” of folder “Users” of startup disk
end tell
– Now that Mail has an single open email (at it’s top level mailbox??) have Mail move it into a named mailbox
tell application “Mail”
activate
move document first message to Recovery
end tell
end repeat

==========================================
This runs with the errot message log:
get message 1
“Mail got an error: Can’t make message 1 into type reference.”

I also tried the following variant:

-- Now that Mail has an single open email (at it's top level mailbox??) have Mail move it into a named mailbox
using terms from application "Mail"
	tell application "Mail"
		activate
		set theMessage to (get selection)
		move (theMessage) to mailbox ("Recovery")
	end tell
end using terms from

end repeat

This version crashes with the log entry
get selection
{current application}
move {application “Mail”} to mailbox “Recovery”
“Mail got an error: NSInternalScriptError”

It looks like I was trying to move the entire Mail application into its own mailbox. What am I doing wrong? It seems like I am close to recovering my mail but I just can’t quite get that one line correct.

Model: G4 desktop
AppleScript: 1.10.3
Browser: Safari 416.13
Operating System: Mac OS X (10.4)

Roger;

I don’t use Mail and haven’t tried scripting it, but I see this term in it’s dictionary: import Mail mailbox. Have you tried that?

Next, above the window in which you wrote your message to this forum is a button labeled “Applescript”. If you click that, it puts a bbcode in your message like this[code]


[/code]
that I’ve shown here as code so you can see it. By using that as I have below, you can simply click on the link at the top “Open this Scriplet in your Editor” of the box and the code will load into your script editor (with your permission).

display dialog "This is a sample" buttons {"Got It"} default button 1

That’s a huge help to folks who want to help.

Now some general comments:

Opening a file just requires a path to it.

-- where you have:
open document file this_item of folder "Test Folder" of folder "Desktop" of folder "roger" of folder "Users" of startup disk
-- you could say:
open (path to desktop as text) & "Test.txt"

Further, I suspect that you should leave out “document” in move. Just say move first message to mailbox “Recovery”

Welcome to the bbs in any case and don’t hesitate to post further.

Adam-
Thanks for your suggestions. I’ll use the bbcode button in the future.

I tried import OSX mail and import other about 37 times with no success. (Always the optimist I couldn’t believe my eyes the first dozen or so times that it failed! Apple never failed me that badly before and I have been faithful since the days of the Integer Apple ][.) Those menu options bring back the mailboxes and sometimes show that there are messages in them but when you open the mailboxes they show a zero message count.

I also tried recovering the mail using Eudora and using Thunderbird mail. Neither one worked. Both saw the mailboxes and Eudora picked up my old Eudora mail from 1999 and before.

Rebuilding the mailboxes also fails. From what I read in various Apple forums this glitch is known to Apple and has struck other users as well. You are probably wise to not use Apple Mail. I don’t know if the problem arose from an overloaded mailbox but there is no excuse for it to cause an unrecoverable disaster. Shades of Microsoft. It is a major reason to stay away from Apple Mail, though I never had any problems with it before.

As for my scripts:
I tried saying move first message to mailbox (“Recovery”) as you suggested and it crashed the same way. Here’s what the event log showed:


tell application "Mail"
	activate
	move message 1 to mailbox "Recovery"

	"Mail got an error: Can't make message 1 into type reference."

As for the section where I wrote

open document file this_item of folder “Test Folder” of folder “Desktop” of folder “roger” of folder “Users” of startup disk

that was just my naive first attempt at writing a script using only the online AppleScript help and my intuition. I didn’t have an AppleScript reference manual at the time. I’ll do it the way you suggest in the future. (open (path to desktop as text) & “Test.txt”)

Thanks for the suggestions but I am still stuck at the same point in the script. Perhaps someone who has tried scripting Apple Mail can offer some help?

Hi Roger,

You could try this:

Make a copy of one of your mailbox folders (.mbox). With this copy remove the files, but not the message folder (mine had content_index, Info.plist, mbox, table_of_contents). Try importing this copy of the mailbox.

This solution doesn’t need an Applescript, but as it’s an Applescript forum. The following script copies a selected mailbox to the Desktop strips out all metadata files and attempts to import the copy into Mail.app.

set myFolder to (choose folder)
set myName to name of (info for myFolder)
if myName ends with ".mbox" then
	set myDestination to (path to desktop as text)
	tell application "Finder"
		activate
		try
			set myCopy to duplicate myFolder to folder myDestination
			delete every file of myCopy
		on error theErr
			display dialog theErr
		end try
	end tell
	tell application "Mail"
		activate
		try
			import Mail mailbox at alias ((myDestination & myName) as text)
		on error theErr
			display dialog theErr
		end try
		display dialog "Check import folders"
	end tell
else
	display dialog "'" & myName & "' is not a Mail mailbox folder."
end if

Best wishes

John M

Hi, John M.

Thanks for the suggestion. Unfortunately it didn’t solve the problem. (I had already tried a similar exercise manually last week.)

When I ran your script it imported the mailbox just fine and showed that there were 168 messages inside waiting to be read (in the small rounded rectangle in the upper right corner of the mailbox’s button in the mailbox list.) When I then opened the mailbox the header shows

“On My Mac (0 messages, 168 unread)”

When I try to manually import a message folder filled with xxxx.emix files I get the message

“No valid Mail for Mac OS X files were found”

The same thing happens if I try to import OSX mail or “Other”

It was a valiant try which I appreciate but the problem still exists!

Sincerely,
Roger K.

Hi Roger,
I found your question while surfing for a solution to the same problem. I somehow corrupted my mail folder and needed to restore the old mail I lost. The .emix files that Spotlight uses to scan mail are the best back up. It is amazing that Apple has not created a solution for importing these .emix file back into mail. So I looked at your original script and re-wrote it. Like you, I could not find a way to import these files directly. Mail will open them, and once they are opened, they can be copied to a mail box with the Copy to Mailbox command.

Event timing is an issue with some of these scripts, or you get a NSReceiverEvaluationScriptError: 4 I got this error several times debugging the script If my menus were not specified exactly right, I got the error, and if I was moving too fast for the application…I also got the error … so I added in a few delays to get the timing down. Maybe a faster Mac than my Aluminum Powerbook would not need them but they definitely helped.

This script will prompt you for the folder your .emix files are in, and prompt you for the Mac Mail folder you want them copied to. It can take awhile because it takes about 4 seconds an email, so if you point it at a folder with 15,000 .emix files in it, expect it to take a long time to process.

Here is the script I used - I hope it helps you…
Best regards,
Dan

–Recovers your lost email from .emix files in MAC OS 10 Mail
–by Dan Skilken www.sonicswap.com
– the script will prompt you for the folder that contains your .emix files
–it will then open each file and copy the mail message into the Recovered folder and then close the file
– the script will then prompt you for the Mail folder that you want to recover the email to
set this_folder to choose folder
display dialog “Please enter the name of the Mail Folder you want to recover the mail to” default answer “Recovered”
set dialogInfo to result
set mailFolder to text returned of dialogInfo
tell application “Finder”
activate
set the source_folder to this_folder as alias

set the item_list to list folder source_folder without invisibles

end tell
repeat with i from 1 to number of items in the item_list
tell application “Finder”
set this_item to item i of the item_list
open document file this_item of folder source_folder
end tell
– Now that Mail has an single open email have Mail copy it into the designated mailbox
tell application “System Events”
tell application process “Mail”
delay 1
click menu item mailFolder of menu 1 of menu item “Copy To” of menu “Message” of menu bar item “Message” of menu bar 1
click menu item “Close” of menu “File” of menu bar item “File” of menu bar 1

	end tell
	delay 1
end tell

end repeat

Model: Powerbook G4 1.5gh
AppleScript: 1.10.3
Browser: Safari 417.8
Operating System: Mac OS X (10.4)

Now it’s very clear to me why I haven’t switched to Mail from Eudora.