Apple Mail: EMLX-Datei der ausgewählte Nachricht speichern

29/12/2010
Apple Mail nutzt glaube ich seit Leopard nicht mehr eine fette mbox Datei sondern einzelne .emlx Dateien zum speichern der abgerufenen Emails. So können die Dateien schön einzeln indiziert (Spotlight) werden und können auch recht leicht kopiert werden (TimeMachine). Eine solche EMLX-Datei lässt sich über Quicklook oder Apple Mail betrachten und beinhaltet sogar die in der Email enthaltenen Anhänge.
Dummerweise kann man via AppleScript nicht direkt den Speicherort der Email abfragen, aber da die emlx-Datei nach der ID der Email benannt ist, kann man sie recht leicht im entsprechenden Account-Ordner finden. Das tut dieses Skript also, es sucht sich die emlx-Dateien der ausgewählten Emails kopiert diese auf den Schreibtisch...
Code zum markieren einmal anklicken Code im Skript-Editor öffnen

--hubionmac.com 29.12.2010 EMLX-Dateien ausgewählter Emails in einen Order (Saved_Mails(Acountname) auf dem Schreibtisch kopieren... (4 Chris_Zuerich)


--auswahl aus Apple Mail abfragen

set mymails to my getSelection()

repeat with mymail in mymails

tell application "Mail"

--das Verzeichnis des Email Accounts ausfindig machen

set accountDir to quoted form of POSIX path of ((account directory of account of mailbox of mymail) as alias)

set account_name to (name of account of mailbox of mymail) as text

tell application "Finder" to set temp_dir to quoted form of (POSIX path of (desktop as alias) & "Saved_Mails (" & account_name & ")/")

try

do shell script "mkdir " & temp_dir

end try

--in dem Account-Verzeichnis nach der der Email-Datei suchen und diese kopieren...

set myid to id of mymail

do shell script "find " & accountDir & " -name " & myid & "*.emlx* -exec cp {} " & temp_dir & " \\;"

end tell

end repeat


on getSelection()

tell application "Mail"

set a to selection

if (count of a) < 1 then

error "Eine Mitteilung muss ausgewählt sein."

end if

return a

end tell

end getSelection

Ein Grund für den regen Gebrauch von Wildcards im find-Befehl ist die Tatsache, dass besonder große Emails aufgesplittet werden in .emlx und .emlxpart Dateien. Gemeinsamer Nenner ist aber immer die ID, mit der der Dateiname stets beginnt.
No Comments

Mail Attachments sichern

17/09/2009
.emlx Dateien sind der Speicherort für Emails ab Mac OSX... ehm 10.5? Hier ein dirty-work-around zum automatischen Speichern von Anhängen in Apple Mail, da es direkt via AppleScript noch nicht möglich zu sein scheint siehe Update weiter unten (Automator zählt nicht ;-) ). Geht aber leider nur mit IMAP-Postfächern
Code zum markieren einmal anklicken

--© hubionmac.com 17.09.2009

--Stores attached pdfs (of an email received via IMAP) on your desktop

-- renames them by date received.... !!!MV overwrites existiting files!!!!

tell application "Mail"

set a to selection

end tell

repeat with s in a

tell application "Mail" to set current_date to date received of s

--convert applescript date to date string

set current_date to AppleScriptDateToString(current_date)

tell application "Mail"

--this works only on imap account, because there attachments are stored in a searate folder, not inline in .emlx-files

set attachments_path to do shell script "find ~/Library/Mail/ -name " & (id of s)

set current_attachments to do shell script "find " & quoted form of attachments_path & " -type f -iname \"*.pdf\" -exec mv {} ~/Desktop/" & current_date & ".pdf \\;"

end tell

get current_date

end repeat




on AppleScriptDateToString(a)

set b to current date

set monthnames to {}

repeat with i from 1 to 12

set month of b to i

set monthnames to monthnames & {(month of b) as text}

end repeat

set Y to (year of b)

set M to 0

repeat with t in monthnames

set M to M + 1

if t as text = (month of b) as text then

exit repeat

end if

end repeat

set M to Twodigits(M)

set D to Twodigits(day of b)

set hh to Twodigits(hours of b)

set mm to (minutes of b)

set ss to Twodigits(seconds of b)

--"2009-12-17 01:26:49"

--return Y & "-" & M & "-" & D & " " & hh & ":" & mm & ":" & ss as text

--"2009-12-17 01:28"

--return Y & "-" & M & "-" & D & " " & hh & ":" & mm as text

--"2009-12-17"

return Y & "-" & M & "-" & D as text

end AppleScriptDateToString

on Twodigits(a)

return (characters -2 through -1 of (("0" & a) as text)) as text

end Twodigits

UPDATE

Dieser Code ist deutlich besser, kommt mit bereits vergebenen Dateinamen klar und funktioniert auch mit jeder Art von Apple-Mail-Account =)
Code zum markieren einmal anklicken

(**

hubionmac.com 17.09.2009

save pdfs from seleceted email messages and save them in one folder

renames attachments by date received and from address like:

2009-12-09 from Steve Jobs <steve@apple.com>.pdf

2009-12-09 from Steve Jobs <steve@apple.com> 1.pdf

2009-12-09 from Steve Jobs <steve@apple.com> 2.pdf 


**)

set theAttachmentPath to (path to desktop) as text

tell application "Mail"

set a to selection

end tell

repeat with s in a

tell application "Mail"

set current_date to date received of s

set CurrentSender to sender of s

end tell

set current_date to AppleScriptDateToString(current_date)

tell application "Mail" to set Attached to mail attachments of s

repeat with ThisAttach in Attached

tell application "Mail" to set FileName to name of ThisAttach

if FileName ends with ".pdf" then

set FileName to current_date & " from " & CurrentSender & ".pdf" as text

set FileName to checkname_with_pdf_suffix(FileName, theAttachmentPath, false)

tell application "Mail" to save ThisAttach in theAttachmentPath & (FileName)

end if

end repeat

end repeat



on AppleScriptDateToString(a)

set b to a

set monthnames to {}

repeat with i from 1 to 12

set month of b to i

set monthnames to monthnames & {(month of b) as text}

end repeat

set Y to (year of b)

set M to 0

repeat with t in monthnames

set M to M + 1

if t as text = (month of b) as text then

exit repeat

end if

end repeat

set M to Twodigits(M)

set D to Twodigits(day of b)

set hh to Twodigits(hours of b)

set mm to (minutes of b)

set ss to Twodigits(seconds of b)

--"2009-12-17 01:26:49"

--return Y & "-" & M & "-" & D & " " & hh & ":" & mm & ":" & ss as text

--"2009-12-17 01:28"

--return Y & "-" & M & "-" & D & " " & hh & ":" & mm as text

--"2009-12-17"

return Y & "-" & M & "-" & D as text

end AppleScriptDateToString

on Twodigits(a)

return (characters -2 through -1 of (("0" & a) as text)) as text

end Twodigits



on checkname_with_pdf_suffix(n, D, looped)

tell application "Finder"

set thefiles to name of every item of (D as alias)

end tell

if thefiles contains n then

if looped = false then

set n to ((characters 1 through -5 of n) & " 1" & (characters -4 through -1 of n)) as text

checkname_with_pdf_suffix(n, D, true)

else

set tmp to (last word of ((characters 1 through -5 of n) as text) as integer)

set tmpcount to (count of characters of (tmp as text)) + 5

set tmp to tmp + 1

set n to ((characters 1 through (-1 * tmpcount) of n) & tmp & (characters -4 through -1 of n)) as text

checkname_with_pdf_suffix(n, D, true)

end if

else

return n

end if

end checkname_with_pdf_suffix

No Comments