Apple script to rename EML files

I do have all my emails exported to a folder with ".eml" format.


I want rename all those files based on "date received" and "sender's email address" info within the EML file. How can I do this?


example: "20171206-123254 - example@domainname.com -.eml"


--

Kindest Regards,

Paulo Guedes

Wednesday, 6 of December, 2017 - 22h:24m [+0000]

MacBook Pro with Retina display, macOS High Sierra (10.13.1)

Posted on Dec 6, 2017 2:29 PM

Reply
Question marked as Top-ranking reply

Posted on Dec 8, 2017 6:48 PM

One final version before I stop fretting at this. If a sender sends you more than one message at exactly the same time (it happened to me with JustGiving), this adds a 2-digit random number to the end of the file name. Plus better handling of no "Mon..." in the date. Plus better use of text item delimiters:



set the_files to (choose fileof type"eml"with prompt"Choose the emails you want to rename:"withmultiple selections allowed)

repeat with each_file in the_files

set the_path to quoted form of POSIX path of each_file

set the_sender to (do shell script "grep ^\"From: \" " & the_path)

set the_date to (do shell script "grep ^\"Date: \" " & the_path)


setAppleScript'stext item delimitersto {"From:"}

set the_sender to text item 2 of the_sender as string


setAppleScript'stext item delimitersto {"Date: ", "("}

set date_string to text item 2 of the_date


try


set ndformat to (do shell script"date -j -f '%a, %d %b %Y %X %z' " & quoted formof date_string & " +'%Y%m%d - %H%M%S'")


onerror


try


set ndformat to (do shell script"date -j -f '%d %b %Y %X %z' " & quoted formof date_string & " +'%Y%m%d - %H%M%S'")


endtry


endtry

set file_name to ndformat & " " & the_sender & ".eml"


tellapplication"Finder"


activate


try

set name of each_file to file_name

on error number errnum


if errnum = -48then-- identical sender, time & date to a previously processed message results in an identical file name

set the_rand to random number from 10 to 99

set file_name to ndformat & " " & the_sender & the_rand & ".eml"

set name of each_file to file_name


endif


endtry


endtell

endrepeat

45 replies

Dec 7, 2017 2:59 AM in response to VikingOSX

Hello VikingOSX


Thank You so much. Anyway:

- If I understood your solution, I do not need to change the way I view the file.

- I do need to rename the file from the actual name to this pattern "<date received><sender's email>.eml". Both "date received" and "sender's email" are inside the EML file and thats why I need an applescript to extract that information and rename the file it self.


--

Kindest Regards,

Paulo

Thursday, 7 of December, 2017 - 10h:56m [+0000]


PS: EML is a file extension for an e-mail message saved to a file in the MIME RFC 822.

Dec 8, 2017 1:07 AM in response to HD

Hello HD

I do not understand Apple Script nor EML structure but the EML file by default is opened by Apple Mail. It is just a taught, how if you tell Apple Mail to open the file and then grab info from there and then close the file and rename it? It might be stupid but it was an idea I get from something I saw and dont understand if even make any sense.


--

Kindest Regards,

Paulo

Friday, 8 of December, 2017 - 09h:03m [+0000]


(*

tell application "Mail"

--activate

set ListMessage to selection -- take all emails selected

repeat with aMessage in ListMessage -- loop through each message

tell aMessage

set messageFrom to sender

set messageSender to extract address from messageFrom

set messageToNameList to to recipients's name

set messageToAddressList to to recipients's address

set messageCCNameList to cc recipients's name

set messageCCAddressList to cc recipients's address

set messageBCCNameList to bcc recipients's name

set messageBCCAddressList to bcc recipients's address

set messageDateR to (date received) as rich text

set messageDateS to (date sent) as rich text

set messageSubject to subject

set messageID to message id as rich text

set mID to id as rich text

set messageSource to source

set messageAllHeaders to all headers as rich text

set messageReplyTo to reply to

set theText to content of aMessage as rich text

set the_source to source of aMessage as rich text

set thesize to message size of aMessage as rich text

end tell

*)

Dec 10, 2017 3:11 AM in response to pguedes

I found some dates in the emails I tested did not include a day of the week abbreviation. I handled that (sort of) with


try

set ndformat to (do shell script "date -j -f '%a, %d %b %Y %X %z' " & quoted form of date_string & " +'[%Y%m%d-%H%M%S]'")

on error

try

set ndformat to (do shell script "date -j -f '%d %b %Y %X %z' " & quoted form of date_string & " +'[%Y%m%d-%H%M%S]'")

end try

end try

Dec 10, 2017 2:06 PM in response to VikingOSX

1- You are right. I do have all my archive in the EML format. It was just a thought. In fact I do archive my emails in both formats - the EML and the mbox format - just in case.


2 - I did not modify anything because I do not have knowledge for that. I even don't know which is the Version 3.


--

Kindest Regards,

Paulo

Sunday, 10 of December, 2017 - 22h:03m [+0000]

Dec 13, 2017 12:01 PM in response to pguedes

I copied and pasted the posted version 6 AppleScript source from this site — back into Script Editor on macOS 10.13.2. It worked fine with my multiple test files, and without a hint of Finder issues.


As we are running the same version of macOS (assuming you upgraded from 10.3.1 to 10.3.2), AppleScript, Perl, and UNIX date — and I have no issues at all with the code, or results, this suggests either an operational issue on your end, or some martian formatting in your .eml files that I can't possibly code for all possible cases.


I can no longer continue to divert further time from other demands to this solution.

Dec 8, 2017 1:50 PM in response to VikingOSX

Understood - I have found 100-character return paths, but as far as I can see, none contain such characters. As you suggest, they might do, but I wonder if such characters are illegal in email addresses...


My script as it stands can't cope with "folded" return-path lines, which are read from an .eml file (by AppleScript or a Bash shell) as two lines:


"Return-path:

<3nCsdWggTCT8op-sfqmzbddpvout.hpphmf.dpnivhiejyponbd.dpn@gaia.bounces.google.com>



I could parse this with AppleScript's rather clunky text editing tools, but it seems to put


grep -i "Return-path: "


out of reach.


H

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Apple script to rename EML files

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.