-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMailArchiver.scpt
More file actions
138 lines (126 loc) · 5.88 KB
/
MailArchiver.scpt
File metadata and controls
138 lines (126 loc) · 5.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# Account source archive
property archiveAccount : "PROS"
# Mailbox target archive root
property archiveMailbox : "PROS Archive"
# Archive Months timewindow
property archiveMonthsTimeWindow : 12
# Prevent same-day archiving process
property preventSameDayArchiving : true
# Log file name
property logFile : "EmailArchiver.log"
set userLocale to user locale of (system info)
if userLocale is "fr_FR" then
set notificationMsg to " messages archivés"
set notificationTitle to "Emails Archiver"
set notificationCompletedSubtitle to "Archivage terminé"
set ignoreTheseMailboxes to {"Suivis", "Brouillons", "Messages envoyés", "Corbeille", "Spam", "Historique des conversations", "Journal", "Archive", "Tâches", "Notes", "Boîte d'envoi", "Éléments envoyés", "Éléments supprimés", "Courrier indésirable"}
else
set notificationMsg to " messages archived"
set notificationTitle to "Emails Archiver"
set notificationCompletedSubtitle to "Processing is complete"
set ignoreTheseMailboxes to {"all mail", "archive", "archived", "drafts", "junk", "junk e-mail", "sent", "sent items", "sent messages", "spam"}
end if
set archiveDateReference to addMonths onto (current date) by -archiveMonthsTimeWindow
property lastRunTime : missing value
if (preventSameDayArchiving is true and lastRunTime is not missing value) and (my dateFormatYYYYMMDD(current date) is equal to my dateFormatYYYYMMDD(lastRunTime)) then
my logToFile("Emails archiving already executed today")
error number -128
end if
display notification "Emails archiving started" with title notificationTitle
my logToFile("Emails archiving started")
#https://superuser.com/questions/33177/apple-mail-doesnt-apply-rules-unless-i-choose-apply-rules-manually
tell application "Mail"
try
set movedMsgCount to 0
set theMailboxes to every mailbox of account archiveAccount
repeat with eachMailbox in theMailboxes
try
if ignoreTheseMailboxes does not contain name of eachMailbox then
my logToFile("Scanning Mailbox \"" & name of eachMailbox & "\"")
set messagesToArchive to (every message of eachMailbox whose date received ≤ archiveDateReference)
if (count of messagesToArchive) > 0 then
set mailboxMovedMsgCount to 0
# Map mailbox hierarchy
set parentList to {}
set nextContainer to eachMailbox
repeat
set the beginning of parentList to (name of nextContainer)
set nextContainer to container of nextContainer
if (class of nextContainer is not container) then
exit repeat
end if
end repeat
set sourceMailbox to my convertListToString(parentList, "/")
set the beginning of parentList to (archiveMailbox)
set targetMailbox to my convertListToString(parentList, "/")
# Appends / to mailbox end -see https://discussions.apple.com/thread/7330780
make new mailbox with properties {name:targetMailbox & "/"}
repeat with messageToArchive in messagesToArchive
move messageToArchive to mailbox targetMailbox
#my logToFile("Message \"" & subject of messageToArchive & "\" moved to mailbox \"" & (name of mailbox of messageToArchive) & "\"")
set mailboxMovedMsgCount to mailboxMovedMsgCount + 1
end repeat
my logToFile("Scanning Mailbox \"" & sourceMailbox & "\" completed. " & (mailboxMovedMsgCount as string) & " messages moved")
set movedMsgCount to movedMsgCount + mailboxMovedMsgCount
end if
else
my logToFile("Scanning Mailbox \"" & name of eachMailbox & "\" ignored")
end if
on error errStr number errorNumber
display notification errStr & " " & errorNumber
my logToFile(errStr & " " & errorNumber)
end try
end repeat
set lastRunTime to (current date)
display notification (movedMsgCount as string) & " " & notificationMsg with title notificationTitle subtitle notificationCompletedSubtitle
my logToFile("Emails archiving completed: " & (movedMsgCount as string) & " messages archived")
on error errStr number errorNumber
display notification errStr & " " & errorNumber
my logToFile(errStr & " " & errorNumber)
end try
end tell
on logToFile(logData)
#log (logData)
set d to (get current date)
set logData to d & " " & logData
set the logPath to ((path to library folder from user domain) as string) & "Logs:" & logFile
try
set the openFile to open for access file logPath with write permission
write (logData as string) & linefeed to the openFile starting at eof as «class utf8»
close access the openFile
return true
on error
try
close access file logPath
end try
return false
end try
end logToFile
on convertListToString(theList, theDelimiter)
set AppleScript's text item delimiters to theDelimiter
set theString to theList as string
set AppleScript's text item delimiters to ""
return theString
end convertListToString
# from https://macscripter.net/viewtopic.php?id=24737
on addMonths onto oldDate by m -- returns a date
copy oldDate to newDate
-- Convert the month-offset parameter to years and months
set {y, m} to {m div 12, m mod 12}
-- If the month offset is negative (-1 to -11), make it positive from a year previously
if m < 0 then set {y, m} to {y - 1, m + 12}
-- Add the year offset into the new date's year
set newDate's year to (newDate's year) + y
-- Add the odd months (at 32 days per month) and set the day
if m is not 0 then tell newDate to set {day, day} to {32 * m, day}
-- If the day's now wrong, it doesn't exist in the target month
-- Subtract the overflow into the following month to return to the last day of the target month
if newDate's day is not oldDate's day then set newDate to newDate - (newDate's day) * days
return newDate
end addMonths
on dateFormatYYYYMMDD(old_date)
set {year:y, month:m, day:d} to old_date
set ymd to (y * 10000 + m * 100 + d) as string
set new_date to (text items 1 thru 4 of ymd as string) & (text items 5 thru 6 of ymd as string) & (text items 7 thru 8 of ymd as string)
return new_date
end dateFormatYYYYMMDD