Mailsmith Hierarchical Export Script

If you’ve been following the conversation here (or over on HawkWings), you’ll note that one of my biggest concerns with having all my mail *in* Mailsmith was that I couldn’t easily get it all *out*, especially not while preserving the hierarchy of messages. That’s no longer a concern, thanks to a little bit of AppleScript mojo, a few hours of “free” time, and the seeds planted by BareBones flat-export script. With great fanfare, but clearly no guarantees, representations, or warrantees by the author or anyone else, I present to you my Mailsmith Hierarchical Export Script (I’ve pasted it here “in the open” so folks searching will have a better chance of finding it, though WordPress doesn’t make this easy. Any advice is welcomed).

-- This script is an enhancement of the BareBones-published
-- Mailsmith Export script.  BareBones' script leaves all the mbox files in
-- one directory, whereas this now creates a folder hierarchy
-- that matches the hierarchy within Mailsmith.
-- Note that the author of the modifications makes no guarantees that
-- this won't completely corrupt your mail store and render it more
-- useless than a leftover chicken.

global export_folder

on doTask(mbox)
tell application "Mailsmith"

set pathadd to ""
set unix_folder to ""

set mboxpath to path of mbox
set pathlength to length of mboxpath

-- Check to see if we're down one or more levels.
-- If so, fill 'pathadd' with the appropriate hierarchy.
-- Since Mailsmith uses the colon ':' to separate folder names,
-- we'll convert slashes to colons in the process.

if pathlength > 0 then
repeat with i from 2 to (pathlength)
if (item i of mboxpath = "/") then
set pathadd to pathadd & ":"
else
set pathadd to pathadd & item i of mboxpath
end if
end repeat
set pathadd to pathadd & ":"
end if

set full_folder to (export_folder as string) & pathadd

-- Now we'll use the Finder to see if the destination folder exists.
-- If it does, do nothing.  If not, we gotta create it.

tell application "Finder"
if folder full_folder exists then
else

-- Now we need to convert *all* of the colons back to
-- slashes since we'll be using unix's 'mkdir -p' command
-- to create the subdirectory

repeat with l from 1 to length of full_folder
if (item l of full_folder = ":") then
set unix_folder to unix_folder & "/"
else
set unix_folder to unix_folder & item l of full_folder
end if
end repeat

set mkdirStr to "mkdir -p '/Volumes/" & unix_folder & "'"

-- Of course, since Mailsmith lets us store both messages AND
-- folders within the same mailbox, it's entirely possible that
-- we'll have a mailbox with both.  When exporting as mbox files,
-- that situation is impossible, and the creation of the folder
-- will fail since the mbox file of the same name will be created first.
-- However, since everything else will want to be in that folder, the folder 'wins'
-- rights to the name.  In that case, rename the mbox to '{name} - mailbox'
-- and then create the folder

try
do shell script mkdirStr
on error errText
if (errText contains "File exists") then
set unix_filename to ""

-- Strip the trailing slash from the folder name.
-- Yes, I'm sure there's a more efficient way to do this.

repeat with g from 1 to ((length of unix_folder) - 1)
set unix_filename to unix_filename & item g of unix_folder
end repeat

-- rename the file

set mvStr to "mv '/Volumes/" & unix_filename & "' '/Volumes/" & unix_filename & " - mailbox'"
do shell script mvStr
end if

-- Now create the subdirectory.  If it doesn't work this time, let the script die.

do shell script mkdirStr

end try
end if
end tell

-- increase the timeout while exporting since 180 seconds isn't nearly enough for larger mailboxes

with timeout of 3600 seconds
export mbox to full_folder as alias
end timeout

end tell
end doTask

on run
tell application "Mailsmith"
set export_folder to choose folder with prompt ¬
"Choose a folder to export mailboxes into."
set ct to count mailboxes
repeat with i from 1 to ct
set obj to mailbox i
my process_mailbox(obj)
end repeat
end tell
end run

on process_mailbox(mbox)
tell application "Mailsmith"

-- Skip creating an empty mailbox for each folder (but, of course
-- still export if mailbox contains both messages and mailboxes)

if not (((count mailboxes of mbox) > 0) and ((count messages of mbox) = 0)) then
my doTask(mbox)
end if

set ct to count mailboxes of mbox
repeat with i from 1 to ct
set obj to mailbox i of mbox
my process_mailbox(obj)
end repeat
end tell
end process_mailbox

But the question remains… where (if anywhere) do I put it? Mail.app *also* seems to limit ones ability to export (to anything!)… if that’s the case, well, I ain’t going there! Oh this is a joy.

Technorati Tags: , , ,

22 Responses to “Mailsmith Hierarchical Export Script”

  1. bronson Says:

    Dave:

    You are able to get your mail out of Mail.app AND keep your mailbox hierarchy. There is a wonderful set of tools by Andreas Amann called ‘Mail Scripts’.

    One of the tools in Mail Scripts is the ability to archive your messages into MBX format with the option of maintaining your mailbox hierarchy.

    You can find Mail Scripts at http://homepage.mac.com/aamann/Mail_Scripts.html although his site has been down because he just released a new version and he’s maxing out his .Mac bandwidth allotment.

    You can get a copy via VersionTracker at http://www.versiontracker.com/dyn/moreinfo/macosx/16217. You can also read more about Mail Scripts at the Hawkwings website: http://www.hawkwings.net/2006/08/22/mail-scripts-2711-an-outstanding-resource-gets-better/

    Good luck and please let us know which direction you go with your email client. This is a very interesting topic.

  2. bronson Says:

    Dave:
    You are able to get your mail out of Mail.app AND keep your mailbox hierarchy. There is a wonderful set of tools by Andreas Amann called ‘Mail Scripts’.

    One of the tools in Mail Scripts is the ability to archive your messages into MBX format with the option of maintaining your mailbox hierarchy.

    You can find Mail Scripts at http://homepage.mac.com/aamann/Mail_Scripts.html although his site has been down because he just released a new version and he’s maxing out his .Mac bandwidth allotment.

    You can get a copy via VersionTracker at http://www.versiontracker.com/dyn/moreinfo/macosx/16217. You can also read more about Mail Scripts at the Hawkwings website: http://www.hawkwings.net/2006/08/22/mail-scripts-2711-an-outstanding-resource-gets-better/

    Good luck and please let us know which direction you go with your email client. This is a very interesting topic.

  3. bronson Says:

    Dave:

    Another email client that you might look at trying is GyazMail (http://gyazsquare.com/gyazmail/), I’ve only used it in a very limited capacity so I don’t know how it will handle a load but it stores messages as single files (RFC 822 format) so it would be easily exportable.

  4. bronson Says:

    Dave:
    Another email client that you might look at trying is GyazMail (http://gyazsquare.com/gyazmail/), I’ve only used it in a very limited capacity so I don’t know how it will handle a load but it stores messages as single files (RFC 822 format) so it would be easily exportable.

  5. Dave Hamilton / Dave The Nerd » Blog Archive » Is Mail.app The Right Choice For Me? Says:

    […] « Man… time really flies Mailsmith Hierarchical Export Script » […]

  6. Dave Hamilton / Dave The Nerd » Blog Archive » Is Mail.app The Right Choice For Me? Says:

    […] « Man… time really flies Mailsmith Hierarchical Export Script » […]

  7. Dave Says:

    Thanks for the tip, bronson. I still can’t download it, but I’ll try again tomorrow. Is there an alternate link?

  8. Dave Says:

    Thanks for the tip, bronson. I still can’t download it, but I’ll try again tomorrow. Is there an alternate link?

  9. bronson Says:

    You download it from MacUpdate – http://macupdate.com/info.php/id/9178

    I just tried it and it works.

  10. bronson Says:

    You download it from MacUpdate – http://macupdate.com/info.php/id/9178
    I just tried it and it works.

  11. bronson Says:

    I also found it at ScriptBuilders – http://files.macscripter.net/ScriptBuilders/E-Mail/mailscripts.dmg

    Good luck

  12. bronson Says:

    I also found it at ScriptBuilders – http://files.macscripter.net/ScriptBuilders/E-Mail/mailscripts.dmg
    Good luck

  13. Hawk Wings » Blog Archive » Script to export email from Mailsmith Says:

    […] If you are using and thinking about a move to Mail.app (or anywhere else), David Hamilton has written a script that exports emails from Mailsmith in a smarter way than the default that comes with Mailsmith itself. […]

  14. Hawk Wings » Blog Archive » Script to export email from Mailsmith Says:

    […] If you are using and thinking about a move to Mail.app (or anywhere else), David Hamilton has written a script that exports emails from Mailsmith in a smarter way than the default that comes with Mailsmith itself. […]

  15. Milan Says:

    It would be the best if somebody would make an export which simulates a POP3 server. That you could export mails to anything you want.

  16. Milan Says:

    It would be the best if somebody would make an export which simulates a POP3 server. That you could export mails to anything you want.

  17. Robert Says:

    Did any of you consider Mailsteward. I’m using it for some months now. First in combination with Mailsmith, but now with Mail.app. One can schedule mailsteward (Cronjobs) to backup mail from Mail.app. Up until now it’s rock solid and does just what it needs to do: http://www.mailsteward.com/

  18. Robert Says:

    Did any of you consider Mailsteward. I’m using it for some months now. First in combination with Mailsmith, but now with Mail.app. One can schedule mailsteward (Cronjobs) to backup mail from Mail.app. Up until now it’s rock solid and does just what it needs to do: http://www.mailsteward.com/

  19. Jesse Smith Says:

    This could use an update.

    Syntax Error

    Expected end of line but found identifer.

    Then mbox in

    export mbox to full_folder as alias

    is highlighted.

  20. Jesse Smith Says:

    This could use an update.
    Syntax Error

    Expected end of line but found identifer.

    Then mbox in

    export mbox to full_folder as alias

    is highlighted.

  21. Dave Says:

    I just checked with Rich Siegel, Mailsmith’s author and programmer extraordinaire, and he said he just tried the script today and it worked as is. Is it possible you’ve pasted something wrong? I don’t have Mailsmith around to test, but I do trust Rich. 😉

  22. Dave Says:

    I just checked with Rich Siegel, Mailsmith’s author and programmer extraordinaire, and he said he just tried the script today and it worked as is. Is it possible you’ve pasted something wrong? I don’t have Mailsmith around to test, but I do trust Rich. 😉

Leave a Reply