Rename .eml File With Email's Date and Sender With KM

Good evening everyone.

Is there any way to rename an .eml mail file according to its date and sender (from the headers) with keyboard maestro?

Anyone did something similar or can give some hint?

Thank you.

Good evening to you, too @tsolignani!

It's easy to get the sender and date information if you have an email in eml format.

I've created an example macro for you that takes the contents of an eml file and performs the extraction using a couple of KM Search actions.

This is not a stand-alone macro insofar as you have to get the contents of an eml file into a KM variable; I do this in the example macro by explicitly setting the KM variable Local__EMLtext to the raw text of an email.

The macro then extracts the two fields into the KM variables Local__Date and Local__Sender. In the example these come out as:

= Local__Date =
Fri, 29 Jul 2022 13:16:04 +0100 (BST)

= Local__Sender =
OWL Messaging <noreply@owl.co.uk>

Which might not be the format you're looking for but the information is there to rename the file.

I leave it up to you to produce a complete macro using mine as a starting point, but do ask further questions if you have them.

Here is the example macro for you:

Test - Get Date and Sender from eml.kmmacros (11 KB)

Click to see macro

1 Like

Hot on @tiffle's heels (but beaten again, dammit!), here's one where you can select an .eml file in the Finder then trigger via hotkey (change to suit):

Rename EML.kmmacros (4.0 KB)

Click for image

You can (and should!) error-proof this! For example, it assumes you are only going to run it on an elm file, and sets the file extension accordingly -- a better approach would be to extract then reapply the extension, at least then you'd retain app association if you selected the wrong file, or do a file extension test before proceeding.

As @tiffle points out, the information you've told us you want may not be what you actually want to see in the file name! But this should be enough to get you started.

And do note that "Sender" and "From" in an email's headers are not the same thing. We've both assumed you actually want the "From" header address -- if you do want "Sender" then change to suit.

1 Like

Thank you. Please forgive me it took so long for a follow up. Thanks again.

1 Like

I'm looking to reuse this to extract the first line in the body of emails that only contain a singe URL in the body text. I'm stuck with getting the RegEx correct.

Here is the last attempt which returns nothing:

This:
image

gives me a file named =.eml

One tricky piece is the colon must be stripped as it can not be included in a file name.

All the rest of the macro remains the same except for disabling the Display text action.

Here is a typical URL in the email body text that I'm looking to use as a file name:
https://open.substack.com/pub/robertreich/p/how-to-win-?r=lw3j2&utm_medium=ios

Best to avoid other characters, such as &, $, and ?, that could also cause problems at the command line or limit file portability. Obviously, the /s can create issues!

So I'd start by rethinking the whole approach, and not using the URL as the file name at all...

On top of that, OP wanted to extract info from the plain text header. You're looking to get it from the message body and, depending on the source, there's a good chance that the body is encoded in something like base64 (although that may vary from sender to sender).

Given all that, this might work to get the first URL in an eml file, assuming it is the first thing on a line, and it should handle both plain and encoded text:

URL from eml.kmmacros (5.6 KB)

Image

It isn't immediately clear how much of the URL you want for the file name, but it's much easier to regex the whole thing then use KM actions to process down!

With all the above -- why do you want to do this in this way? It assumes the file is already exported from Mail -- could you not do the same as part of, for instance, an AppleScripted export?

Thank you for this.

This does work, as the URL is the first and only thing in the email’s body. I hoped to avoid a longish read describing the whole story, but I fell into the pitfall of not providing enough initial info.

I save many articles using Safari’s share function. Often, the sites I share from only insert the URL for the article into the generated email. I then manually click into the To field, add my address, and send. I then drag the email from Mail into a Finder folder. This is my basic workflow to save and file readings, and I read a lot.

This macro is to then go back to Finder filed email and rename it using the enclosed URL that has at least some inking of content rather then having a file named "(No Subject) 2.eml" as some sites do not include content into the subject line.

I could name the file before sending to self but that breaks the flow of engaging with the conversation the reading and link following provide.

An AppleScripted export, while possible, is above my pay grade, leaving me to ask someone to develop it for me. Rather, I have a nice set of Keyboard Maestro palettes (see below) and actions that move the emails into the needed Finder folder "sort buckets,” ready to be renamed (hopefully) by this macro.

The following hybrid Regex and Keyboard Maestro actions work. I failed when I tacked on the pieces to get the actions to actually rename a file to the version you sent. It simply did nothing. Given this fuller picture I'm not sure troubleshooting yours is the best way forward.

I added the Get Substring to remove everything up to //, but it does not, and the macro fails without having it there and is set to 0. Also, the Search and Replace action does not remove the first /. I have no idea why these two things happen.

This macro on this email body:

Produces a file named: [??https/--open.substack.com-pub-philosophors-p-the-wisdom-letter-068?r=3Dlw3j2=?&utm_medium=3Dios??Bern=??].eml

Hope this clarifies and not exhausts :wink:.

Easier to "split" the URL on :// and take the second half -- everything from the domain onwards. You can then replace "problem" characters with - and create a file name to use when you write the file out. Here's an example, broken out into simple steps:

File Name from URL.kmmacros (4.0 KB)

Image

I've left in the [ and ] to match your macro -- but I'd still recommend not using them, to save yourself headaches if you ever want to do things via command line or shell actions!

Thank you! I know enough to get and agree with your thinking. I do not know enough to implement it successfully. Too many questions arise to work this out in the medium of this forum. It’s a bit like getting directions to drive a car at the height of rush hour in NYC by playing whisper down the lane between four people. While possible, the chances for everyone remaining intact are minimal, and I don’t want to stretch the generosity you’ve been providing.

Again, thank you for your time and input. I have gained by it and much appreciate it and you.

Which bit? You should be able to drop the 2nd, 3rd, and 4th actions of my macro into yours, replacing the actions that come after the regex that extracts the URL to Local_theURL. You can then change that last "Move or Rename File..." action to it is "to: %Variable%Local_theFileName%".

Here's my implementation of your directions and questions.

Rename .EML to URL1

Keyboard Maestro Actions.kmactions (9.5 KB)

Step 2: Yes. If the variable is in "All:" then it's the entire match, including the two linefeeds at the beginning. If it is "1:" it will be the first capture group, between the ( and ) in the pattern -- every character after the two linefeeds to the end of the string. It doesn't matter which you do in this case.

But something's gone a bit squiffy after that -- I obviously didn't explain myself very well!

Here's a complete version that does what I think you want to do -- it's a bit difficult to test because I'm behind an Exchange server that wraps every URL in a pile of "is this safe?" crap redirects.

URL from eml v2.kmmacros (5.8 KB)

Image

This worked! Thank you! I have a few questions in the comments; otherwise, I understand what is happening.

Thanks again!

Keyboard Maestro Actions.kmactions (13 KB)

The email's contents may or may not be encoded. The test I sent myself was so I added the decode step. You can see if yours are by opening an .eml file in a text editor and seeing if the contents section makes sense or is a bunch of seemingly random characters.

We "Try" to do action 3. If it errors (no match) then the email contents are encoded, so we decode and try the search again.

Say Local_theURL is

https://www.example.com/path/to/file.html

You want

www.example.com/path/to/file.html

...to be followed with some extra processing.

You can treat a string as an array using the format Local_var[<index>]<sep>, where <index> is an integer indicating the element of the array -- 1 is the first, 2 the second, etc -- and <sep>is the characters or sequence of characters that separate each array element.

You can split the URL on the string ://, giving you two parts:

  1. https
  2. www.example.com/path/to/file.html

...and note that the string you are splitting on is not included. You want the second part, element 2, which is why it is Local_theURL[2]://.

Yes, we're just updating the variable in place, adding ".eml". Force of habit for me -- it would be more obvious to use the KM "Append Variable with Text" action:

image

Clear!

Thank you for giving me your time, thinking, and work! It was generous, and I appreciate it!

As an aside, while Keyboard Maestro goes a remarkable way into block-based programming, there is still a hell of a lot to learn about the blocks. Both the Wiki and help systems go a long way, and there is still a long way to go for those without a drive to learn the language of coding.

It'd be nice if someday AI could pick up the difference, and clearly, we're not there and may never be. Meanwhile, there's generosity like yours that fills the gap.

Thanks again!