-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMailArchiver.scpt
117 lines (95 loc) · 3.19 KB
/
MailArchiver.scpt
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
(*
Delfim Machado - dbcm@profundos.org
XPTO:: Portuguese OpenSource Community
*)
-- Archive POP mails to IMAP account?
property ArchivePOP2IMAP : true
-- What is the name of the IMAP Account to move POP mails?
property MainIMAPAccount : "Profundos"
-- Do log?
property DoLog : true
on run {input, parameters}
using terms from application "Mail"
my logThis("---------- Mail.app Archiver starting")
tell application "Mail"
set selectedMails to the selection
set nCount to count of selection
my logThis("Starting loop...")
repeat with eachMessage in selectedMails
my logThis("repeat, msg: " & subject of eachMessage)
-- generic info about the message
set msgDate to date received of eachMessage
set msgMonth to month of msgDate as integer
set msgYear to year of msgDate as integer
-- message endpoint
set mboxName to "Archive/" & msgYear & "/" & msgYear & "-" & msgMonth
try
set msgAccount to name of account of mailbox of eachMessage as string
set msgAccountType to account type of account of mailbox of eachMessage
on error errText
-- no account, lets move them to IMAP
set msgAccountType to "local"
end try
my logThis("accountType " & msgAccountType)
if msgAccountType is imap or msgAccountType is iCloud then
-- mails alive in a IMAP account
tell account msgAccount
my logThis("with account: " & msgAccount)
try
set mbox to mailbox named mboxName
get name of mbox
on error
make new mailbox with properties {name:mboxName}
set mbox to mailbox named mboxName
end try
try
move eachMessage to mbox
on error errText number errNum
my logThis("Err: " & errNum & " . " & errText)
end try
end tell
else
-- mails sent and received from a pop account
try
my logThis("mail is on pop or local")
if ArchivePOP2IMAP then -- Archive POP or LOCAL mails to IMAP account
my logThis("ArchivePOP2IMAP is active")
tell account MainIMAPAccount
if the mailbox mboxName exists then
else
make new mailbox with properties {name:mboxName}
end if
set mbox to mailbox named mboxName
end tell
move eachMessage to mbox
end if
on error myErr
display dialog "Something gone mad (" & myErr & ")" buttons "OK"
end try
(*
else
my logThis("Account type: " & (msgAccountType as string) & " _ " & subject of eachMessage)
*)
end if
end repeat
end tell
end using terms from
return input
end run
on logThis(str)
if DoLog then -- only log if this is true (WOW)
set LogFile to a reference to (path to desktop as string) & "Mail.app Archiver.log"
set tStamp to current date
set myNow to ((year of tStamp as string) & (month of tStamp as string) & (day of tStamp as string))
try
open for access file LogFile with write permission
-- set eof of file logFile to 0
write myNow & " : " & str & return to file LogFile starting at eof
close access file LogFile
log str
on error errText number errNum
close access file LogFile
--display dialog logThiserrText
end try
end if
end logThis