Mac Mail - Filing Message Thread

Hi, I have this Macro to file messages to various mailboxes.
Could someone please suggest a tweak /mod which allows me to file or move the entire thread of the message—message+reply—to a specified folder.
Thank You.

Hey @dev,

How many messages do you have in the source mailbox?

Do you mind installing an AppleScript Extension (osax) to provide regular expression support in AppleScript?

-Chris

Hi Chris,

Few messages. Usually not more than 6.
Yes, I can.

With the existing Macro – I have some duplicates of these which point to other mailboxes – I select the message in Mac Mail Inbox and can file it to the desired mailbox.

Thank You

  • dev

I’m not good enough with AppleScript to identify why your script may not file multiple messages compared to the one I use, but I never have issues with the following code:

tell application "Mail"
	activate
	with timeout of 30 seconds
		set theSelectedMessages to selection
		repeat with theMessage in theSelectedMessages
			set theMailbox to "Business/Customers/Invoices"
			tell application "Mail"
				move the theMessage to mailbox theMailbox of account "Invoices"
			end tell
		end repeat
	end timeout
end tell

Someone like ccstone might be able to identify the difference and/or suggest alternatives.

1 Like

Hey @dev,

Install the Satimage.osax from here:

Smile Beta Page

Satimage osax 3.7.0 (build 420) direct download

Don’t worry about it being on the Smile beta page – it’s the most up-to-date version available and is stable on macOS 10.12.3.

It has an installer and only installs one file here::

/Library/ScriptingAdditions/Satimage.osax

After installing it restart any AppleScript editors you are using and the Keyboard Maestro Engine.

Here’s how to get the thread of the selected message:

------------------------------------------------------------------------------

tell application "Mail"
   
   set selectedMessageList to selection
   set firstSelectedMessage to item 1 of selectedMessageList
   
   tell firstSelectedMessage
      set containerMailbox to its mailbox
      set msgSubject to its subject
   end tell
   
   set msgSubject to cng("^(Re|Fwd): *", "", msgSubject) of me
   
   set messageList to messages of containerMailbox whose subject contains msgSubject
   
end tell

------------------------------------------------------------------------------
--» HANDLERS
------------------------------------------------------------------------------
on cng(_find, _replace, _data)
   change _find into _replace in _data with regexp without case sensitive
end cng
------------------------------------------------------------------------------

Whose clauses are a trifle slow in Mail, but if there aren’t that many messages in the mailbox it should be acceptably fast.

The cng handler uses the Satimage.osax’s change command, and I’m using that to prune junk off of the subject before searching with the whose clause.

You’ll have to loop through the found messages when moving them, because Mail doesn’t accept a list of messages as a parameter for the move command. It will only take a single message reference at a time.

Holler if you need any more help.

-Chris

1 Like

Hi Chris,

Fabulous! Thanks for this!
I installed Saltimage and will try this out.

There is a commercial app - MailHub which does something like this.
But I am trying to stay away from Mail Bundles and move everything to KM.

Thank You again,

  • dev

This works very well. And perhaps better than the one I had posted above.
What I was looking for was a way to move my reply (which is in Sent Mail) into the specified folder as well. Essentially so I have all project related mail – incoming and outgoing – in one folder.

Hey @dev,

I strongly recommend MailHub. I use it and Mail Act-On both and find them indispensable. (Since 2010 or so.)

You’d be amazed at how much more convenient Mail is to use with those two plugins (despite the feature overlap).

You’ve got to spell that stuff out.  :wink:

Simple enough…

------------------------------------------------------------------------------

tell application "Mail"
   
   set selectedMessageList to selection
   set firstSelectedMessage to item 1 of selectedMessageList
   
   tell firstSelectedMessage
      set containerMailbox to its mailbox
      set msgSubject to its subject
   end tell
   
   set msgSubject to cng("^(Re|Fwd): *", "", msgSubject) of me
   
   # Thread in current mailbox.
   set messagesToMove to messages of containerMailbox whose subject contains msgSubject
   
   # Related messages in Sent mailbox.
   set messagesToMove to messagesToMove & (get messages of sent mailbox whose subject contains msgSubject)
   
end tell

------------------------------------------------------------------------------
--» HANDLERS
------------------------------------------------------------------------------
on cng(_find, _replace, _data)
   change _find into _replace in _data with regexp without case sensitive
end cng
------------------------------------------------------------------------------

-Chris

1 Like

@dev: Also note that if there aren’t too many Mail folders you’re using for filing messages, the Favorites Bar can be helpful.

Favorites Bar Overview

Not detailed in that Apple Support document is the key fact that by default Mail keeps count of how many folders you have in the Favorites Bar. This is key, because for the first nine folders you have there, you then have built-in shortcuts for accessing them or moving messages to them.

Command-[1–9] opens a mailbox in the Favorites Bar (counting from left to right)

Command-Control-[1–9] moves the selected message(s) to the specific mailbox in the Favorites Bar

@ccstone: I’ve always wanted a means to do what you provided with that script. Thank you.

1 Like

Oh this is brilliant! Thank you. I am using the Favorites Bar but was not aware of this keyboard shortcut.

I used to use Mail Act-On and am a recent convert to KM. So I was exploring all ways to move to KM. But reading what you say makes me rethink and perhaps I will go back to Mail Act-On and try MailHub as well.

Thank you for this!! I’ll try this out as well. :+1:

Hey @Dev,

MailHub eats Mail Act-On’s lunch when it comes to filing messages.

The UI is better, and it learns – so you can often use one keystroke to correctly file whatever is selected.

But – Mail Act-On adds other features to Mail that are indispensable to me – so I can’t eliminate it.

If you want to know more PM me, and we’ll talk off the forum.

If you’ve followed me at all on the forum you know I’m a big proponent of Keyboard Maestro and AppleScript and other scripting methods. Mail scripting just isn’t good enough to replace those plugins.

I know this, because I’ve tried.

I’ve written and use 50 some odd AppleScripts to bludgeon Mail into doing things more way I want it to.

-Chris

I’m sold! :slight_smile:

Sounds Good. I’ll do that. Thank you!

@dev @ccstone

Now with Mojave released, do you still use mail hub and mail act-on?

I like the features of both, and have the current version purchased.
Like @dev however, I am also inclined to try and use Keyboard Maestro abilities going forward

  • because MailHub appears to not be supported much in Mojave (based on activity on their forum) and
  • because mail act-on now only comes as a bundle

Hey @sims,

I won't upgrade to Mojave for some time. It has too many problems.

So I'll keep using MailHub and Mail Act-On.

You may note that @dev was sold on the idea of plug-ins in post #14.

The MailHub site says “MailHub for Mojave is now Available”:

http://dervishsoftware.com

That's upsetting and may end up being a costly business decision for them. People won't pay $60.00 U.S. for Mail plug-ins unless they really need them.

I already own the bundle, so it's not as big a deal for me.

The fact is that scripting Mail cannot catch up to what the plug-ins can do. Unfortunately.

That said I script the heck out of Mail – I have about 60 scripts for it – and about 35 KM macros.

-Chris

Hi @sims and @ccstone,

I did upgrade to Mohave on one project specific machine (not my main machine) and MailHub works well there. I did not install the MailSuite from Small Cubed on that machine as yet. After my chat with @ccstone I tried MailHub and it's been excellent and very stable for me on both Mohave and High Sierra. That was a terrific suggestion.

I do have Mail Act-On on a machine running High Sierra (which is my main machine).

@ccstone I'd love to know more about the 60 scripts and 35KM macros you have. :slight_smile:

  • dev
1 Like

Hey @dev

Here's a listing.

-Chris


AppleScripts:

Mail:
    DEVONthink:
      Add attachments to DEVONthink.scpt
      Add mailbox(es) & attachments to DEVONthink.scpt
      Add mailbox(es) to DEVONthink.scpt
      Add message(s) & attachments to DEVONthink.scpt
      Add message(s) to DEVONthink.scpt
   Bundles ⇢ Disable.scpt
   Bundles ⇢ Enable.scpt
   Change Outgoing Address:
      Change Outgoing Address to geode.scpt
      Change Outgoing Address to gmail.scpt
      Change Outgoing Address to junksieve.scpt
      Change Outgoing Address to listmeister.scpt
      Change Outgoing Address to suddenlink.scpt
   Click “All” Button when Search Field is Active.scptd:
   Close Mailbox Hierarchy in Sidebar.scpt
   Date Message Sent ⇢ Unix Date.scpt
   Deselect Messages in Front Mail Viewer.scpt
   Edit ⇢ Change meta-data offset for edited Mail message.scpt
   Edit ⇢ Message in BBEdit.scpt
   Edit ⇢ Reply using Rules.scpt
   Extract Urls from Selected Messages.scpt
   Filter ⇢ Yahoo Groups Email Header.scpt
   Find Links on the Applescript Users List (asul).scptd:
   Images ⇢ Toggle.scpt
   Links ⇢ Mail-Message-URLs from selected messages.scpt
   Macscripter ⇢ Digest Scraper.scpt
   Mail Act-On Long Version to Clipboard.scpt
   Mark as Junk.scptd:
   Nav ⇢ Select Mailbox Today.scptd:
   OFFLIST ⇢ Preface Subject and Filter.scptd:
   Open ⇢ Dir ⇢ Bundles Folders.scpt
   Open ⇢ File ⇢ 'Temp_Script ⇢ Mail.scpt'.scpt
   Open ⇢ File ⇢ Mail Salutation File.scpt
   Open ⇢ List-Archive Header in Safari.scpt
   Open ⇢ Pop-Up Menu ⇢ From.scpt
   Preview Pane ⇢ Toggle Visibility.scpt
   Print-To ⇢ Web-Receipts.scpt
   Reply ⇢ To Sender.scptd:
   Rule ⇢ Add Address to Promotions Rule.scpt
   Rule ⇢ Add Address to Subscriptions Rule.scpt
   Rule ⇢ Send Values of Open Rule to BBEdit.scpt
   Salutation ⇢ Paste from LookUpTable.scpt
   Save ⇢ Attachments to Designated Folder.scpt
   Select ⇢ Inbox.scpt
   Selected Messages ⇢ Send Content to Script Debugger.scpt
   Selected Messages ⇢ Send Headers & Content to BBEdit.scpt
   Selected Messages ⇢ Send Source to BBEdit.scpt
   Selected Messages ⇢ Send Text Content to BBEdit.scpt
   Temp_Script ⇢ Mail.scpt
   Test -- UI Script.scptd:
   Toggle ⇢ Inbox Open-Closed.scptd:
   Unread Messages ⇢ Count.scptd:
   Window ⇢ Activity ⇢ Toggle.scptd:
   Window ⇢ Center Message Viewer.scpt
   Window ⇢ Center Message.scpt

Keyboard Maestro Macros:
Case ⇢ Lower
Execute AppleScript ⇢ Temp_Script ⇢ Mail.scpt
Expand Thread ⇢ Sort by Date Sent (Ascending)
Filter Text ⇢ Yahoo Groups Email Header
Get New Mail and don't change Activity Window
Get New Mail and Open Activity Window
Horizontal Line
Horizontal Rule ⇢ Paste ⇢ HR
Is Activity Window Open?  If NOT then List Windows
Link ⇢ Make from Selected URL
Menu ⇢ Bulleted List
Menu ⇢ Forward Message with Option-Key
Menu ⇢ Make Plain Text
Menu ⇢ Mark (Un)Read
Menu ⇢ Message ⇢ Move-To
Menu ⇢ Remove Attachments
Menu ⇢ Show Related Messages
Menu ⇢ Sort ⇢ Column ⇢ Date Sent
Menu ⇢ Sort ⇢ Column ⇢ Date Sent → Sorted Descending
Menu ⇢ Sort ⇢ Column ⇢ From
Menu ⇢ Sort ⇢ Column ⇢ Mailbox
Menu ⇢ Sort ⇢ Column ⇢ Subject
Nav ⇢ Message List
Nav ⇢ Message List without Down-Arrow
Nav ⇢ Move Focus to Mailbox List Pane { script }
Nav ⇢ Scroll Message Preview Down
Nav ⇢ Scroll Message Preview Up
Paste RTF ⇢ AppleScript properly reformatted for Mail v1.00
Paste Select Wiki Link
Pop-up Menu ⇢ Open ⇢ Outgoing Message ⇢ FROM
Print { PDF to Web-Receipts dir }
Print ⇢ PDF to ~/Desktop
Remove Read Messages from Mailbox ⇢ Script
Reply to From-Address
Reply using Rules
Salutation
Save Attachments of Selected Message v1.10
Select ⇢ Current Line
Select or Show a Menu Item
Selected Email Links to TaskPaper
Selected Text ⇢ Label Yellow
Snippet -- M**** -- New Member Announcement
TEST_MAIL_01
TEST_SCRIPT_FILE
1 Like

Thank you for your detailed and helpful reply. Somehow, I only saw the reply now.

I did upgrade to Mojave a few days back and I installed mailhub. Works well - I think I don't leverage it enough.
I also have the previous version of act-on and mailtags, but the bundle approach did not make sense.

Thanks for sharing the list of your macros. Will browse to see if some might be useful.

Thanks again for sharing your knowledge here.