Exporting Mail mailboxes

(Luciano Di Croce) #1

Thanks Mark!!!
Inspired by this I have tried to export all mailboxes in my “iCloud” account. Some of them have subfolder and thus I would like to have the option to select the “Export all subfolder” (rather than exporting each subfolder …).
This is what I have done so far …

set FilterFoldersOut to {"Archive", "Apple Mail To Do", "Notes", "INBOX", "Drafts", "Sent Messages", "Deleted Messages", "Junk"} -- I dont need those mailboxes
set TopLevelMailboxes to {}

    tell application "Mail"
       set allMailboxes to every mailbox of account "iCloud"
       repeat with aMailboxes in allMailboxes
           set myName to (name of aMailboxes's container) as rich text
           if name of aMailboxes's container = missing value then -- it means that it is a top level folder, it might or might not have subfolder(s), but for sure IT IS NOT a subfolder
               if name of aMailboxes is not in FilterFoldersOut then --remove those folders I dont need
                   set theM to mailbox (name of aMailboxes) of account "iCloud" -- I can select the folder in case I can trigger the "Export" command by UI...
                   set end of TopLevelMailboxes to (name of aMailboxes)
               end if
           end if
       end repeat
    end tell

I have now a list of folders I want to export. Some of them have subfolders, and for those I would like to have the “Export all subfolder” option activated.

Any suggestion on how to continue?


Creating Mail message with attachments
(Mark Alldritt) #2

I’m going to split this discussion into its own topic since its not directly related to creating Mail messages with an attachment

I’m not sure if I understand your question fully, but here’s some code that goes through all the messages and sub mailboxes:

use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions

property iCloidAccountName : "iCloud"
property filterFoldersOut : {"Archive", "INBOX", "Drafts", "Sent Messages", "Deleted Messages", "Junk"} -- I dont need those mailboxes

on exportMailbox(aMailbox, dumpSubMailboxes)
	tell application "Mail"
		tell aMailbox
			--	Export the messages in aMailbox
			repeat with aMessage in messages
				--	Export a message
				local messageSubject, messageContent, messageSender -- so SD can "see" these variables
				set {messageSubject, messageContent, messageSender} ¬
					to {aMessage's subject, aMessage's content, aMessage's sender}
				--	Export the message in whatever fashion you like.  I'm just logging the information I've obtained for now.
				--	See <https://forum.latenightsw.com/t/saving-mail-message-attachments/1012> for info on how to save
				--	aMessage's attachments.
				log "Subject: " & messageSubject & ", from: " & messageSender & ", content: " & messageContent
			end repeat
			--	Export the messages in any mailboxes within aMailbox?
			if dumpSubMailboxes then
				repeat with aSubMailbox in mailboxes
					my exportMailbox(aSubMailbox, dumpSubMailboxes)
				end repeat
			end if
		end tell
	end tell
end exportMailbox

tell application "Mail"
	tell account iCloidAccountName
		repeat with aMailbox in every mailbox
			if name of aMailbox's container = missing value then -- it means that it is a top level folder, it might or might not have subfolder(s), but for sure IT IS NOT a subfolder
				if name of aMailbox is not in filterFoldersOut then --remove those folders I dont need
					my exportMailbox(aMailbox, true)
				end if
				log "Skipped mailbox: " & name of aMailbox
			end if
		end repeat
	end tell
end tell

(Luciano Di Croce) #3

Sorry If I wasn’t clear on my goal.

I have several mailboxes in my iCloud account. I want to export them (see screenshot #1).

Considering that several of them have, sub folders (=nested mailboxes), I would like to activate the option “Export all subfolder” (see screenshot #2).

I managed so far to get a list of the mailboxes I want to export (in my script is the “TopLevelMailboxes” list. But now I don’t know how to trigger the “export” command, with the option of “including subfolders”.


PS: the screenshots I included might not be 100% visible unless you click on them.

(Mark Alldritt) #4

Mail’s scripting interface does not offer an export command. This is why my example spins through all the messages in the mailbox and extracts data for export/saving. I tried applying the save verb to a mailbox and it failed.

I suppose you could resort to GUI scripting as a way of invoking this through the GUI.

(Luciano Di Croce) #5

Thanks Mark for your help.
I was afraid of the lack of possibility to execute an “export” command directly.
Anyway, I built this little script which shod do the job:

tell application "System Events" to tell process "Mail"
set frontmost to true
tell its window 1
	if name is "Activity" then # we need to be sure that window "Activity" window does NOT pop up
		keystroke "0" using {command down, option down}
		delay 1
	end if
end tell
tell menu bar item "Mailbox" of menu bar 1
	-- click
	click menu item "Export Mailbox…" of menu 1
	delay 1
end tell

tell its window 1
	repeat until exists sheet 1
		delay 0.5
	end repeat
	tell its sheet 1
		tell its button "Options"
		end tell
		# we need to be sure that there are subfolders
			tell its checkbox "Export all subfolders"
			end tell
		end try
		keystroke "g" using {command down, shift down}
		set filesavepath to "/Users/ldicroce/Desktop/Test Folder"
		--tell application "Finder" to copy filesavepath to clipboard
		set the clipboard to filesavepath
		delay 2
		keystroke "v" using {command down}
		--keystroke filesavepath
		tell its sheet 1
			click button "Go"
		end tell
		click button "Choose"
		-- click at {780, 500}
	end tell
end tell

end tell