Well I've got round mail servers that fold Return-path: for no apparent reason. And I've got round mail servers that omit "Mon, " (etc) from Date:
And I've repunctuated VikingOSX's nifty date command so it works in the context of my AppleScript:
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 -i \"Return-path:\" " & the_path)
set the_date to (do shell script "grep -i \"Date: \" " & the_path)
setAppleScript'stext item delimitersto {"<", ">"}
if (count text items of the_sender) = 1 then
set the_sender to paragraph 2 of (read each_file)
set the_sender to text item 2 of the_sender
else
set the_sender to text item 2 of the_sender
endif
setAppleScript'stext item delimitersto"Date: "
set date_string to text item 2 of the_date
setAppleScript'stext item delimitersto" ("
set date_string to text item 1 of date_string
log date_string
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'") -- with thanks to VikingOSX
endtry
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'") -- with further thanks to VikingOSX
endtry
set file_name to ndformat & " " & the_sender & "-.eml"
tellapplication"Finder"
activate
set name of each_file to file_name
endtell
endrepeat
And it has renamed 90+ .eml files in one pass without error, in just a few seconds.
But that doesn't solve the error the OP found when he ran the script, and it doesn't anticipate any other crud that a differently configured mail server is going to turn up.
It works for me, but it may not work for anyone else...
EDIT: (And I think using From: rather than Return-path: may be a better solution...)
H