An AppleScript to export Entourage emails

Feb 16, '04 09:12:00AM

Contributed by: golgi_body

Entourage stores email data in a single, monolithic 'Database' file. After time, this file can become too large to fit on a CD-R if one wants to backup his or her emails. This script exports all emails contained in a given mailbox to individual files. Note that I haven't done extensive testing of this script. I wrote this for a coworker who has used it without a hitch but that's not to say others might not have problems with it.

To use this script, copy the Applescript into Script Editor. Save the file as a compiled script. Then move the script file into: your user's Documents -> Microsoft User Data -> Entourage Script Menu Items folder. This will make the script available in Entourage's Script menu.

Read the rest of the hint for the script...


tell application "Microsoft Entourage"
  activate
  set mailFolders to {} as list
  repeat with i in folders
    set mailFolders to (mailFolders & name of i)
  end repeat
    
  set thisFolder to choose from list mailFolders with prompt ¬
  "Pick a mailbox to export" without multiple selections allowed
     
  if ((count of messages of folder named thisFolder) = 0) then
    display dialog "'" & (thisFolder as string) & ¬
    "' contains no emails" buttons "OK" default button 1
          
  else
    set docFolder to (path to documents folder from user domain)
    set fpath to (choose folder default location docFolder with prompt ¬
    "Choose a folder in which to store your emails" ¬
    without multiple selections allowed) as string
    set fcount to 0
               
    repeat with i in messages of folder named thisFolder
      set fcount to fcount + 1
      set newFname to "" as string
      set fname to (subject of i) as string
                 
      if ((count of characters of fname) > 30) then
        set fname to ((characters 1 thru 25 of fname) & "...") as string
      end if
                        
      if (fname contains ":") then
        repeat with j in characters of fname
          if (j as string = ":") then
            set newFname to newFname & "-"
          else
            set newFname to newFname & j
          end if
        end repeat
        set fname to newFname
      end if
                        
      set fname to (fpath & fname) as string
                        
      tell application "Finder"
        if (exists file (fname)) then
          set k to 1
          repeat while (exists file (fname & "-" & (k as string)))
            set k to k + 1
          end repeat
          set fname to (fname & "-" & (k as string))
        end if
      end tell
                      
      save i in fname
      tell application "Finder" to update file fname
                        
    end repeat
            
    tell application "Finder"
      activate
      if (fcount = 1) then
        display dialog "Exported 1 email to folder '" & (name of folder fpath) ¬
        & "'" buttons {"OK"} default button 1 with icon note
      else
        display dialog "Exported " & (fcount as string) & " emails to folder '" & ¬
        (name of folder fpath) & "'" buttons {"OK"} default button 1 with icon note
      end if
      open folder fpath
    end tell

  end if
end tell

Comments (7)


Mac OS X Hints
http://hints.macworld.com/article.php?story=20040208091514777