Create a new folder based on email subject and save attatchments into it

I have a recurring task in which I get an email with one or more attachments that I have to process into printable artwork. Each email contains a unique order number in the subject line but these also include the end-user customer name and so are of variable length.
At present I copy the order number and customer detail, select save all attachments, navigate to a specific folder on a server, select create folder and rename this new folder using the clipboard content and then save the attachments into that folder.
I have been trying to automate this so that a single keystroke will carry out the whole process - but . . . .
I can extract the order. details for the folder name using regex but get no further than this.
I am working on a macOS Ventura system using Apple Mail, Keyboard Maestro etc.
Any advice would be appreciated.

You can select the subject with a Click and Drag action, clicking to the left of the Subject line and dragging down one line.

Then a Copy action to copy the subject.

Then Search using Regular Expression action to extract teh information.

You can then use the Select a Menu Item action to select File ➤ Save Attachments…

Then Type a Keystroke action to type Command-Shift-G

Then Insert Text by Typing action to go to the desired target location (with a %Return% on the end)

Then Type a Keystroke action to type Command-Shift-N to make a new folder.

Then Insert Text by Typing action to type in the unique order number (with a %Return% on the end).

Then Type a Keystroke action to type Return to save the attachments.

I believe the sequence will work with either one or more then one attachments, but Mail behaves a bit differently in those two cases so double check.

You will definitely need some Pause actions in there to ensure that macro does not get ahead of Mail/the system processing the keys.

Thanks very much for the detailed response; I will work on this over the next few hours and see if I can make it work.

Welcome to the KM Forum.

Even though @peternlewis has given you the means to a solution, I like a challenge and since I haven't automated Apple Mail for a while I thought I'd give it a go.

Here is what I've come up with:

Download Macro(s): Test Create new folder on email subject saving attatchments.kmmacros (11 KB)


  • Macros are always disabled when imported into the Keyboard Maestro Editor.
    • The user must ensure the macro is enabled.
    • The user must also ensure the macro's parent macro-group is enabled.
System Information
  • macOS 10.14.6
  • Keyboard Maestro v10.2

To make it useful to you, here are some notes:

  1. The macro will run only if you have selected an email in Apple Mail. It does this by checking if the current Mail Subject is empty or not.

  2. You mention a Server Folder. The orange action in the macro set the full path to that folder. You'll need to change it to suit your usage but make sure it's got a trailing "/" at the end.

  3. The two yellow actions process the Mail Subject to extract the Order Number and Customer Info. Obviously, I've made something up for testing so you'll have to change that to suit your specific needs.

  4. The macro (magenta) then checks that those two items both have information in them, otherwise the macro does nothing further.

  5. In the two green actions the macro creates the new folder based on the order/customer info on the server.

  6. The last action (in teal colour) is some AppleScript that cleverly extracts the attachment(s) and saves them into the server folder just created.

  7. If you put this macro in an Apple Mail-specific macro group and assign a suitable trigger, it will run for you without having the KM Editor on display. I mention this because you're new to the forum and you might also be new to KM. No offence meant.

There's quite a lot going on in this macro and you might feel the need to just use bits here and there. That's great and entirely up to you, but I hope I've pointed you in the right direction! Let us know how you get on.


I've changed the example macro I originally gave slightly and added the following:

  • The macro now uses the customer info I've used as a test to create a nested hierarchy of folders based on customer name/order number in the two green actions (step 5 above)
  • When finished saving, the macro displays a text message to show what attachments have been saved and where they've been saved to.

Thanks for this as well! You are correct in that I am fairly new to KM although I have been involved with computers for the past fifty years; the last time I scripted anything would have been thirty years ago.
But I want to learn and keep the brain active.

I have been using KM to make life easier in the Affinity suite of software by mapping the keys on a contour shuttle to perform repetitive tasks while not breaking my brain in the process.

1 Like

Sounds all good to me :sunglasses: