Ive setup a Script to rename exported Emails.
Exported means drag them from Apple Mail into a Finder folder.
The Script should look for the receiving date/time an rename the file like this.
A mail with the subject “Test-Subject” will be named “Test-Subject.eml” after dragging it into a Finder folder.
The Script renames the file to “2017-02-28-1412 Test-Subject”
For easy handling he Script is exported to an application an placed in the finder menu bar.
My Problem is:
Id like to replace “incompatible characters” which are in the subject of some Emails.
In my script removing a slash and colon works but if there is a pipe in the file name it remains.
I hope you can help me.
feel free to use, share an modify my script.
here is what my script looks like:
tell application “Finder”
activate
set selected_items to selection
end tell
tell application “Mail”
activate
set theMessages to selection
repeat with theMessage in theMessages
set theText to source of theMessage
set theSubject to (subject of theMessage) as Unicode text
--Sollte die E-Mail ohne Betreff ankommen wird *E-Mail hat keinen Betreff* eingesetzt
if theSubject is "" then set theSubject to ":E-Mail:hat:keinen:Betreff:"
--Doppelpunkte im Betreff durch Unterstrich ersetzen
if theSubject contains ":" then set theSubject to my snr(theSubject, ":", "_")
--Slash im Betreff durch Unterstrich ersetzen
if theSubject contains "/" then set theSubject to my snr(theSubject, "/", "_")
--Pipe im Betreff durch Unterstrich ersetzen
if theSubject contains "|" then set theSubject to my snr(theSubject, "|", "_")
--Empfangsdatum auslesen
set msgDate to date received of theMessage
set {year:y, month:m, day:d, hours:h, minutes:min} to (msgDate)
--Datumsformat erstellen
set msgDate to (y & "-" & my pad2(m as integer) & "-" & my pad2(d) & "-" & my pad2(h) & my pad2(min))
end repeat
end tell
–Einstelligen Tagen und Monaten eine 0 vorstellen
on pad2(n)
return text -2 thru -1 of (“00” & n)
end pad2
–Text im Betreff auf inkompatibele Zeichen scannen und ggf ersetzen
on snr(theString, searchString, replaceString)
tell (a reference to my text item delimiters)
set {oldTID, contents} to {contents, searchString}
set {theString, contents} to {theString’s text items, replaceString}
set {theString, contents} to {theString as Unicode text, oldTID}
end tell
return theString
end snr
tell application “Finder”
try
– wenn keine Finder Fenster geöffnet sind desktop verwenden
set the source_folder to (folder of the front window) as text
on error
set the source_folder to (path to desktop folder) as text
end try
set these_items to the selection
end tell
repeat with i from 1 to the count of these_items
set this_item to (item i of these_items) as alias
set this_info to info for this_item
set {file_name, file_ext} to splitExtension from the name of this_info
set new_name to ((msgDate & " " & file_name & file_ext)) as string
-- prüfen ob datei mit selbem Namen existiert
tell application "Finder"
if (exists item (source_folder & new_name)) then
display dialog "Die Datei " & new_name & " existiert bereits. Weitere Version erstellen?" buttons {"Cancel", "OK"} default button "OK"
if result = {button returned:"OK"} then
-- Versionsnummer für Datei erfragen
set vers to text returned of (display dialog "Bitte Versionsnummer Eingeben -> " & new_name & "_" buttons {"Cancel", "OK"} default button "OK" default answer "")
set vers_name to ((msgDate & " " & file_name & "_" & vers & file_ext)) as string
if (exists item (source_folder & vers_name)) then
display dialog "Die Datei " & vers_name & " existiert auch schon. Bitte nochmal versuchen..." buttons {"Cancel", "OK"} default button "OK"
else
set name of this_item to vers_name
end if
else
end if
else
set name of this_item to new_name
end if
end tell
end repeat
to splitExtension from file_name
set dot to “.”
tell AppleScript
set oT to text item delimiters
set text item delimiters to dot
if (count text items of file_name) > 1 then
set out_name to (text items 1 through -2 of file_name) as string
set ext to last text item of file_name
else
set out_name to file_name
set ext to “”
end if
set text item delimiters to oT
if ext is not “” then set ext to dot & ext
return {out_name, ext}
end tell
end splitExtension