Doppelte Emails filtern

Dieses Skript ist in erster Linie zur Anschauung geschrieben, um einen Eindruck zu bekommen, wie man schnell nach doppelte Einträge suchen kann. Der eigentliche Trick dabei, ist dass die Duplikate nicht direkt via AppleScript sonder über ein standard-Unix-Tool gefunden werden (uniq)

— hubionmac.com 04.02.2011

— you say a folder and a search pattern for sender’s address

— and each duplicated email (sender = subject) will be marked, except the latest one

— I you feel brave you my uncomment the delete 

tell application “Mail” to set thefolder to inbox

set sender_address to “amazon”

tell application “Finder” to set startup_disk to (startup disk as alias) as text

tell application “Mail”

set mysubjects to subject of every message of thefolder whose sender contains sender_address

set AppleScript‘s text item delimiters to

set mysubjects to mysubjects as text

set AppleScript‘s text item delimiters to “”

–save this list to a text tmp text file

my writeToFile(startup_disk & “tmp:mail_subjects.txt”, mysubjects, false)

–now use the unix tool unig to find all duplicated subjects

–this way because uniq is much faster on comparing thousands of lines than applescript

set duplicated_subjects to do shell script “sort </tmp/mail_subjects.txt | uniq -d | uniq”

do shell script “rm /tmp/mail_subjects.txt”

repeat with mydublicated_subject in (every paragraph of duplicated_subjects)

set current_messages to (every message of thefolder whose subject is mydublicated_subject and sender contains sender_address)

set first_loop to true

repeat with current_message in current_messages

if first_loop is true then

set last_message to current_message

set last_date to date sent of current_message

set first_loop to false

else

if last_date ≤ (date sent of current_message) then

set background color of last_message to green

–delete last_message

set last_message to current_message

set last_date to date sent of current_message

else

set background color of current_message to green

–delete current_message

end if

end if

end repeat

end repeat

end tell

on writeToFile(MacFilePathTxt, txt, add2eof)

–lastedit 18.01.2011

if add2eof is false then

try

do shell script “rm ” & quoted form of POSIX path of (MacFilePathTxt as alias)

end try

end if

set RefNum to (open for access file MacFilePathTxt with write permission)

try

if add2eof is false then

write txt to RefNum

else

write txt to RefNum starting at ((get eof RefNum) + 1)

end if

close access RefNum

return true

on error

close access RefNum

return false

end try

end writeToFile