Mailsmith Hierarchical Export Script

by Dave in

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 & ":"
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

-- 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 & "/"
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

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? *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: , , ,