mail.app subfolder path

Hello, I have been working on this for about a week now and haven’t found anything that works. I hope I am just missing the obvious.

I have an IMAP account on mail.app that needs to be copied to the local PC.

What I am trying to do is maintain the folder hierarchy.

I wrote a script to do everything I want so far, but instead of pulling the full folder name, (Folder, Subfolder1, Subfolder2) it just pulls the name of the Subfolder the message is in.

This is a problem because the owner of my company has well over 1000 folders in his IMAP account and over 14gigs of email.

Does anybody know of a command to pull the full folder name, or path, or something as opposed to just the name of the subfolder the email is in?

Below is my code, I have looked at close to a hundred scripts over the course of the past week and borrowed from several of them to get this far.

Thanks very much,

Brian


on run
	tell application "Mail"
		set x to 1
		set theMSGS to selection
		repeat with eachMessage in theMSGS
			
			set numMSGS to count of theMSGS
			set theMSG to item x of theMSGS
			set theSubject to subject of theMSG
			set theSender to sender of theMSG
			set theSelection to the name of mailbox of theMSG --ONLY DISPLAYS LOCAL MAILBOX NAME
			set msgAccount to name of account of mailbox of theMSG
			
                                                        --set theContainerPath to container of theMSG
			--set theContainerPath to containerPath of theSelection & "/" & name of theSelection
			--set theContainerPath to containerPath of theMSG
			--set theContainerPath to the pathname of theSelection
			--set theContainerPath to the pathname of theMSG
			
			if the mailbox theSelection exists then
				duplicate theMSG to mailbox theSelection
			else
				make new mailbox with properties {name:theSelection}
				duplicate theMSG to mailbox theSelection
			end if
			set x to (x + 1)
			display dialog x
		end repeat
	end tell
end run


Hi,

it’s a lack of mail.app that you can’t get the path of the mailbox directly,
but it’s possible to determine the full name (path) with an forced error


tell application "Mail"
	set x to 1
	set theMSGS to selection
	set numMSGS to count of theMSGS
	repeat with eachMessage in theMSGS
		set theSubject to subject of eachMessage
		set theSender to sender of eachMessage
		try
			set theSelection to the path of mailbox of eachMessage -- forces an error
		on error r
			set {ASTID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, quote}
			set theSelection to text item 2 of r
			set AppleScript's text item delimiters to ASTID
		end try
		if the mailbox theSelection exists then
			duplicate eachMessage to mailbox theSelection
		else
			make new mailbox with properties {name:theSelection}
			duplicate eachMessage to mailbox theSelection
		end if
		set x to (x + 1)
		display dialog x
	end repeat
end tell

Thanks very much, that worked perfectly as long as you start with the root folder and then do subfolders. Does not seem to work in reverse. Won’t copy the message. Not a problem though.

Will I be able to setup a loop to go through each folder starting at the top of my IMAP account and going to the end, or will it not go into subfolders on it’s own?

If it won’t I can make do with this, it will just be a lot easier if I could get it to loop through each folder and subfolder while keeping this current naming convention.

BTW, what is ASTID?

Thanks again,

Brian

(A)pple(S)cript’s (T)ext (I)tem (D)elimiters
is just a custom variable to save the status of text item delimiters.

I really have no idea, what’s the purpose of your script.
In this version it will never create any new mailbox and duplicate every message into its own (sub)mailbox

Thanks,

What it is doing right now, is I highlight all the emails in a folder in the IMAP section, and it recreates the exact same folder on the actual “ON MY MAC” section of the computer.

Then I move to the first subfolder, and do the same thing, and again with each subsequent subfolder and folder.

I want to mirror the IMAP section of the mail.app email by email onto the local “ON MY MAC” section of the computer, and retain the hierarchy.

IMAP has stopped working for him and he wants to move back to POP. I guess the 14 gigs of email has something to do with that.

Thanks,

Brian

:lol:

it could be easier to choose “Keep copies of messages for offline viewing: All messages and their attachments”
in Preferences > Accounts > Advanced of the IMAP account

PS: 14 GB of mails is crazy. I recommend “Archive Messages” of A. Amann’s Mail Scripts

The thing is I do have it setup to keep a copy on the laptop, but it is not solving my problem. I am not entirely sure how the mail.app client works, it still can take a very long time to load a simple email (even with a local cached copy on the laptop), and sometimes it just sits there syncing with the server for up to 10 minutes on a folder that has no new mail and only a handful of messages in it to begin with.

My boss refuses to change from his current setup of folders for each client. He maintains one year of email on his laptop at all times. Which as of last week was 14 gigs of email over the last 12 months.

It has to be the size of his account. I use the same server, same mail.app on Leopard and I have no problems. But I only have two gigs of mail in my folders since the last year.

So I am hoping that a return to POP will eliminate the email load times, and it will definitely eliminate the server sync times.

It is the best solution I can come up with that works for everyone.

Thanks,

Brian

Unfortunately I have not been able to find code to loop through a series of IMAP folders (all one account), only local folders.

Does anyone know anything about this?

Thanks in advance,

Brian

Actually If I select multiple emails across multiple folders it seems to do exactly what I need it to do. I think I am just going to go with this and give it a trial run. Will see how it goes.

Thanks very much for your help figuring out that earlier problem.

Brian

something like this


tell application "Mail"
	repeat with oneAccount in (get imap accounts)
		repeat with oneMailbox in mailboxes of oneAccount
			set theMessages to messages of oneMailbox
			-- do something
		end repeat
	end repeat
end tell

I’m posting a message late to this tread because I just spent a bunch of time trying to figure this one out myself, and discovered that most of the info is most for older versions, and things have apparently changed.

In macOS 10.10 with Mail.app 8.2 (2104), this works to burrow into subfolders…

on run
	tell application "Mail"
		set firstBox to mailbox "Clients"
	end tell
	checkMailbox(firstBox)
end run

on checkMailbox(aMailbox)
	local aMailbox, i, aBox
	tell application "Mail"
		tell aMailbox
			repeat with aBox in mailboxes
				say ((name of aBox) as rich text) -- do actual work here!
				if ((mailboxes of aBox) is not {}) then
					tell me to checkMailbox(aBox)
				end if
			end repeat
		end tell
	end tell
end checkMailbox

What is really weird to me is that this looks perfectly logical, yet my first attempts at creating this just wouldn’t work. I don’t know what I was doing wrong before, but this works, and so I figured I’d post it for posterity. (Or until a new version of Mail.app breaks it.)

Model: Mac mini (Late 2014)
Browser: Safari 603.3.8
Operating System: Mac OS X (10.10.5)