Archive Gmail messages in Mail.app

I’m trying to archive selected messages in Mail.app using the AppleScript below. While it works for iCloud email messages, it does not work for Gmail. Every post in this forum I could find on archiving messages used the move command, and it must have worked for Gmail in the past, but it seems that’s no longer the case. Does anyone know how to archive Gmail messages in the macOS stock Mail app?

property gmailArchive : "[Gmail]/All Mail"

tell application "Mail"
	set selectedMailboxes to selected mailboxes of front message viewer
	set isInbox to my isInbox(selectedMailboxes)
	set selectedMessages to selection

	repeat with m in selectedMessages
		set m's flagged status to true
		-- The second condition is necessary for Gmail, since every Gmail message's mailbox's name is "All Mail".
		if m's mailbox's name = "INBOX" or isInbox then
			set theAccount to m's mailbox's account
			if exists mailbox gmailArchive of theAccount then
				move m to theAccount's mailbox gmailArchive
			else if exists mailbox "Archive" of theAccount then
				move m to theAccount's mailbox "Archive"
			else
				error "No All Mail or Archive mailbox found"
			end if
		end if
	end repeat
end tell


using terms from application "Mail"
	on isInbox(theMailboxes)
		if theMailboxes is missing value then return false
		repeat with m in theMailboxes
			if m's name ≠ "All Inboxes" and m's name ≠ "INBOX" then return false
		end repeat
		return true
	end isInbox
end using terms from

When I run the script, the selected message is cleared from the right most pane of the message viewer, but remains in the Inbox list. Sometimes, if I select another mailbox and then select Inbox again, the message disappears from the list. Even then, however, the message still remains in Inbox in the Gmail web app and the iOS Mail app—it disappears from Inbox only in the macOS Mail app. Bizarre.

I discovered that, using the script below, every Gmail message is basically filed under All Mail, even though they show up under a folder (Gmail label) in the Mail app. Even the sent messages belong to All Mail. I suspect this is why the “move” command does not work on Gmail messages. Is there another command I can try to archive messages? Just for the record, the Mail app’s Archive command (^⌘A) works well for me.

set mailboxNames to {}
tell application "Mail"
	set selectedMessages to selection
	repeat with m in selectedMessages
		copy m's mailbox's name to end of mailboxNames
	end repeat
end tell
return mailboxNames

This works fine for me:

tell application id "com.apple.mail"
   set SelectedEmails to selection
   repeat with theEmail in SelectedEmails
      move theEmail to mailbox "[Gmail]/All Mail" of account "b@gmail.com"
   end repeat
end tell

Please use human words. And MacScripter is not Stackoverflow - I hope.

So you want to move messages in the “Gmail” account of the Mail.app on your Mac, and move them accordingly in the “Gmail” account of the Mail.app on your mobile phone?

It is possible that the transfer has already occurred and there is no problem. It’s just that Mail.app shows this movement visually only after updating the viewer window on iPhone. Close then reopen the viewer to see if the messages disappear from INBOX.

Glad that works for you. That looks exactly the same as my code if I’m not mistaken—I just use property gmailArchive : "[Gmail]/All Mail". Sadly, it doesn’t work for me.

No, I want to move the selected emails in my Gmail account’s inbox to my Gmail account’s archive regardless of the device I am using. I may be archiving Gmail messages on a Mac using AppleScript, but since Mail.app is accessing my Gmail via IMAP, I expect that movement to be propagated to the Gmail server.

I mentioned the iOS Mail app and Gmail web app in my post because I assumed that those emails still remaining in the Gmail inbox were a sign that my AppleScript wasn’t working properly. When I archive emails manually in macOS Mail.app, those emails are always moved out of my Inbox across all my devices—expected behavior given IMAP.

And like your line of thinking in the second paragraph, I also initially thought it was a delay or device caching issue. But no, it wasn’t, those emails stayed in my Gmail inbox no matter what I do—kill and restart the Mail apps, log in to Gmail on a fresh Private browser window that should not have any cached data, etc.

That’s an interesting idea. Can you elaborate a little bit?

What you suggested might work in my use case. I didn’t mention the full context in my OP but I’m trying to use AppleScript to flag and archive emails for which I created Things to-dos. And there’s a second AppleScript that unflags those emails once those to-dos are completed in Things.

Sounds like a smart mailbox can be used instead of flagging, although I’m not sure if that would be a macOS-specific thing or something that propagates to all my Apple devices. How might I go about moving those emails out of my inbox and put them in a smart mailbox?

The problem with moving or deleting emails is that there is no error message. Things to test would be:

  • Does my script work for you for normal mailboxes?
  • Does my script work for you for the Inbox?

Then you can combine my and your script to see where the problem occurs.

I’m not sure what you mean by “normal mailboxes” or “the Inbox”, but I’m only interested in archiving the selected emails, i.e., moving them from Inbox to Archive, a.k.a., [Gmail]/All Mail. Also, as I mentioned in my OP, my script works perfectly for iCloud email messages. So this is a Gmail-specific problem.

I only tested for a mailbox that was not the Inbox, Sent or Trash. Sometimes those mailboxes behave differently. I just did a test for the Inbox and the emails was archived fine with my script.

The behaviour of iCloud and Gmail is always different.

Yeah, again, glad it works for you. I’m not exactly sure what it is, but for some reason it doesn’t work for my Gmail account.

I’m sorry that I wasn’t clearer. Did you test with my script, too?

Thanks for the elaboration, @Fredrik71 . I understand your idea much better now but think it isn’t a solution for my problem. I’m flagging and archiving emails that have been “converted” to Things to-dos (or Reminders, if you’re not a Things user). For example, I get an email with a document, I create a to-do titled “Review XXX’s doc and send feedback”, and the script flags and archives the email. Or I get an email that has a question I can’t immediately answer, I’d create a to-do titled “Get back to XXX on YYY”, and the script takes care of the flagging and archiving. So this can’t really be done based on a set of rules. :frowning:

Yes. And it does not work.

Yes I could. I actually am in a way, since I’m using the “Flagged” mailbox, an Apple-provided smart mailbox.

@Fredrik71 , thanks for the idea. A “To do list” smart mailbox will make the intention clearer than the Flagged mailbox for sure.

What I’d like to achieve now is to get those flagged email messages out of my inbox so I can have some peace of mind.

Can you move any of your Gmail emails to any other mailbox?

If you can’t get this to work with AppleScript there is the MailTags plugin which is really good for setting up filters. I have a free license because my app supports MailTags but I have no affiliation with them. I recently did a review of MailTags on my blog. The plugin is pricey, though.

Thanks. I should’ve added that I’d like to get them out automatically. Call me lazy if you will, but I’ve been doing this with ^⌥Space (Things keyboard shortcut) + ⇧⌘L (flag) + ^⌘A (archive) for a long time and now I’ve decided to replace those three keystroke combinations with a single keyboard shortcut combination (via Shortcuts + AppleScript).

Thanks for your ideas. Appreciate them. But I’d like to keep the focus on the problem I’d like to solve, which is archiving Gmail messages using AppleScript.

I did it like this: first move the message locally and then move the message remotely to another mailbox (e.g, “To be deleted”). Finally you can delete everything in To be deleted from the web UI.