- Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathProcessMailMessageInfo.applescript
More file actions
Latest commit
516 lines (410 loc) · 17.7 KB
/
Copy pathProcessMailMessageInfo.applescript
File metadata and controls
516 lines (410 loc) · 17.7 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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
-- Create UUID text file and Definitions for MPM
propertyoutputAsJSON : false
propertyoutputAsDefinitions : false
propertyoutputAsPlist : false
propertyoutputFileName : "CompleteMailInfo"
propertyoutputUUIDListFileName : "SupportableUUIDList"
propertyoutputUUIDDefinitionsFileName : "CompleteUUIDDefinitions.plist"
propertystartingMailComparator : 50
propertystartingMessageComparator : 1000
propertyendingComparator : 999999
propertyoutputComments : true
onrun argv
-- Handle parameters
setstartingOSto"10.6"-- default value
setendingOSto""-- default value
if ((countof argv) >0) then
repeatwithparamin argv
if (param starts with"lt=") then
setTIDtoAppleScript's text item delimiters
setAppleScript's text item delimitersto"="
setendingOStotextitem2of param
setAppleScript's text item delimitersto TID
end if
if (param starts with"10.") then
setstartingOSto param
end if
if (param contains"nc") then
setoutputCommentstofalse
end if
if (param contains"defs") then
setoutputAsDefinitionstotrue
end if
if (param contains"plist") then
setoutputAsPlisttotrue
end if
end repeat
end if
-- Should get the contents from the current directory
tellapplication"Finder"
setscriptPathtopath tome
setinfoFolderto container of scriptPath
setplistFolderto ((infoFolder asstring) &"PlistFolder:"asalias)
end tell
-- Get the info lists, sort it and filter based on OS requirements
setinfoListto buildInfoListfromFolder(plistFolder)
setinfoListto sortByVersionNumber(infoList)
setfilteredInfoListto filterForOSesWith(infoList, startingOS, endingOS)
-- Create sorted, unique lists of Mail and Message info
setcompleteMailInfoto filterMailInfo(filteredInfoList)
setcompleteMessageInfoto filterMessageInfo(filteredInfoList)
-- Export a list of all of the UUIDs for both parts as plist file
if (outputAsPlist and outputUUIDListFileName is not equal to"") then
-- Write the contents of all uuids as a plist
if outputComments then
setmailCommentto"<string># All Mail UUIDs</string>"&return
setmessageCommentto"<string># All Message UUIDs</string>"&return
else
setmailCommentto""
setmessageCommentto""
end if
setoutputContentsto"<array>"
setoutputContentsto outputContents & messageComment & convertListToUUIDPList(completeMessageInfo)
setoutputContentsto outputContents & mailComment & convertListToUUIDPList(completeMailInfo)
setoutputContentsto outputContents &"</array>"
setoutFilePathto (infoFolder asstring) & outputUUIDListFileName &".plist"
writeFileWithContents(outFilePath, outputContents)
-- Export a list of all of the UUIDs for both parts as simple file
else if (outputUUIDListFileName is not equal to"") then
-- Write the contents of all uuids as a simple list
if outputComments then
setmailCommentto"# All Mail UUIDs"&return
setmessageCommentto"# All Message UUIDs"&return
else
setmailCommentto""
setmessageCommentto""
end if
setoutputContentsto messageComment & convertListToUUIDStringList(completeMessageInfo)
setoutputContentsto outputContents & mailComment & convertListToUUIDStringList(completeMailInfo)
setoutFilePathto (infoFolder asstring) & outputUUIDListFileName &".txt"
writeFileWithContents(outFilePath, outputContents)
end if
-- If we should create the UUID Definitions file
if (outputAsDefinitions and outputUUIDDefinitionsFileName is not equal to"") then
-- Then refilter the info without OS filtering
setcompleteMailInfoto filterMailInfo(infoList)
setcompleteMessageInfoto filterMessageInfo(infoList)
-- Build plist content sections for each
setmailSectionto convertListToPlistSection(completeMailInfo)
setmessageSectionto convertListToPlistSection(completeMessageInfo)
-- Then stick them together
setoutputContentsto createPlistOutputWithSections({mailSection, messageSection} aslist)
-- And write them out
setoutFilePathto (infoFolder asstring) & outputUUIDDefinitionsFileName
writeFileWithContents(outFilePath, outputContents)
end if
-- Do JSON if desired
if (outputAsJSON) then
-- Write the contents of the JSON out to the current folder as well
setoutputContentsto convertListToJSON(infoList)
setoutFilePathto (infoFolder asstring) & outputFileName &".json"
writeFileWithContents(outFilePath, outputContents)
end if
endrun
onbuildInfoListfromFolder(theInfoFolder)
-- Get the list of info files
tellapplication"Finder"
setinfoFilesto (filesof theInfoFolder whosename extension is"plist") asaliaslist
end tell
setTIDtoAppleScript's text item delimiters
-- Our list
setinfoListto {} aslist
repeatwithaFilein infoFiles
-- Get the file's name
tellapplication"Finder"
setfileNametonameof aFile
end tell
-- Ensure that we skip any plist files that don't end with "info.plist"
if (fileName ends with"-info.plist") then
-- Break that into the keys we need...
setAppleScript's text item delimitersto"-"
setosVersiontotextitem1of fileName
setinfoTypetotextitem2of fileName
if (countoftextitemsof fileName) >3then
setotherDescriptiontotextitem3of fileName
else
setotherDescriptionto""
end if
setAppleScript's text item delimitersto TID
-- Ensure that this file is the correct type
setexecutableNametodo shell script ("defaults read ""e& (POSIX pathof aFile) "e&" CFBundleExecutable")
if (executableName is not equal to infoType) then
log"Mismatched type ['"& infoType &"'] for executable ['"& executableName &"']"
else
-- First see if the file has a PluginCompatibilityID
setuuidto""
try
setuuidtodo shell script ("defaults read ""e& (POSIX pathof aFile) "e&" PluginCompatibilityUUID")
on error errMsg
end try
if (uuid is not"") then
-- Then use defaults to get values out of each file
setbundleIDtodo shell script ("defaults read ""e& (POSIX pathof aFile) "e&" CFBundleIdentifier")
setshortVersiontodo shell script ("defaults read ""e& (POSIX pathof aFile) "e&" CFBundleShortVersionString")
setversionNumbertodo shell script ("defaults read ""e& (POSIX pathof aFile) "e&" CFBundleVersion")
-- Then branch for the other values based on the type
setexpectedVersionto"n/a"
if (infoType is"Mail") then
try
setexpectedVersiontodo shell script ("defaults read ""e& (POSIX pathof aFile) "e&" ExpectedMessageVersion")
on error errMsg
end try
setminimumOSVersiontodo shell script ("defaults read ""e& (POSIX pathof aFile) "e&" LSMinimumSystemVersion")
else
try
setexpectedVersiontodo shell script ("defaults read ""e& (POSIX pathof aFile) "e&" ExpectedMailVersion")
on error errorMsg
end try
setminimumOSVersionto""
end if
-- Add the record to our list
setendof infoList to ({fileName:fileName, osVersion:osVersion, otherDescription:otherDescription, bundleID:bundleID, shortVersion:shortVersion, versionNumber:versionNumber, uuid:uuid, expectedVersion:expectedVersion} asrecord)
end if
end if
end if-- Skipping files that aren't "-Info.plist"
end repeat
return infoList
end buildInfoListfromFolder
onfilterMailInfo(theList)
return FilterInfo(theList, "com.apple.mail", "mail")
end filterMailInfo
onfilterMessageInfo(theList)
return FilterInfo(theList, "com.apple.MessageFramework", "message")
end filterMessageInfo
onFilterInfo(theList, bundleMatch, typeKey)
setversionListto {}
setstartOSto""
setpreviousRecordto {osVersion:"", uuid:""} asrecord
repeatwithaRecordin theList
if (bundleID of aRecord is equal to bundleMatch) then
-- log "osVersion=" & osVersion of aRecord & " -- version=" & shortVersion of aRecord & " -- uuid=" & uuid of aRecord
-- Ensure a basic start OS
if startOS is""then
setstartOSto osVersion of aRecord
end if
-- If the UUID has changed then also set the startOS
if ((uuid of aRecord) is not equal to (uuid of previousRecord)) or ((shortVersion of aRecord) is not equal to (shortVersion of previousRecord)) then
-- Write out previous grouping
if (uuid of previousRecord is not"") then
-- log {startOS:startOS, endOS:endVersionFromVersions(osVersion of previousRecord, osVersion of aRecord), type:typeKey, displayVersion:(shortVersion of previousRecord), uuid:uuid of previousRecord}
setendof versionList to ({startOS:startOS, endOS:endVersionFromVersions(osVersion of previousRecord, osVersion of aRecord), type:typeKey, displayVersion:(shortVersion of previousRecord), uuid:uuid of previousRecord, versionNumber:versionNumber of previousRecord, otherDescription:otherDescription of previousRecord} asrecord)
end if
-- Establish new values for this group
setstartOSto osVersion of aRecord
end if
setpreviousRecordto aRecord
end if
end repeat
-- log {startOS:startOS, endOS:osVersion of aRecord, type:typeKey, displayVersion:(shortVersion of previousRecord), uuid:uuid of previousRecord}
setendof versionList to ({startOS:startOS, endOS:osVersion of aRecord, type:typeKey, displayVersion:(shortVersion of previousRecord), uuid:uuid of previousRecord, versionNumber:versionNumber of previousRecord, otherDescription:otherDescription of previousRecord} asrecord)
return versionList
end FilterInfo
onendVersionFromVersions(previousVersion, currentVersion)
if (currentVersion is equal to previousVersion) then
return currentVersion
end if
if (currentVersion ends with".0") then
if (previousVersion is equal to"") then
return currentVersion
else
return previousVersion
end if
end if
setTIDtoAppleScript's text item delimiters
setAppleScript's text item delimitersto"."
setversionPartstotextitemsof currentVersion
setthelastitemof versionParts to ((lastitemof versionParts) -1)
setmyVersionto versionParts asstring
setAppleScript's text item delimitersto TID
return myVersion
end endVersionFromVersions
onsortByVersionNumber(theList)
repeatwithifrom1to (countof theList) -1
repeatwithjfrom i +1tocountof theList
if versionNumber ofitem j of theList < versionNumber ofitem i of theList then
settemptoitem i of theList
setitem i of theList toitem j of theList
setitem j of theList to temp
end if
end repeat
end repeat
return theList
end sortByVersionNumber
onfilterForOSesWith(theList, firstOSToSupport, lessThanOSToSupport)
-- Just return the list of there is no criteria
if ((firstOSToSupport is"") and (lessThanOSToSupport is"")) then
return theList
end if
-- Otherwise set starting values
setresultListto {} aslist
setfoundStarttofalse
-- Look through list until we find a match and then add all the rest
repeatwithinfoItemin theList
if ((osVersion of infoItem is equal to lessThanOSToSupport) or (osVersion of infoItem begins with lessThanOSToSupport)) then
exit repeat
end if
if (foundStart or (osVersion of infoItem is equal to firstOSToSupport) or (osVersion of infoItem begins with firstOSToSupport)) then
setfoundStarttotrue
setendof resultList to infoItem
end if
end repeat
return resultList
end filterForOSesWith
onconvertListToUUIDStringList(theList)
-- use a repeat loop to loop over a list of something
setinfoDatato""
setuuidListto {} aslist
repeatwithmailInfoin theList
if (uuidList does not contain (uuid of mailInfo)) then
-- Build out the string contents
if (outputComments) then
setbuildInfoto""
if ((otherDescription of mailInfo) is not"") then
setbuildInfoto" (build "& (otherDescription of mailInfo) &")"
end if
setinfoDatato infoData &"'# For "& (type of mailInfo) &" version "& (displayVersion of mailInfo) &" ("& (versionNumber of mailInfo) &") on OS X Version "& (startOS of mailInfo) & buildInfo &"'"&return
end if
setinfoDatato infoData & (uuid of mailInfo) &return
setendof uuidList to (uuid of mailInfo)
end if
end repeat
return infoData
end convertListToUUIDStringList
onconvertListToUUIDPList(theList)
-- use a repeat loop to loop over a list of something
setinfoDatato""
setuuidListto {} aslist
repeatwithmailInfoin theList
if (uuidList does not contain (uuid of mailInfo)) then
-- Build out the plist contents
if (outputComments) then
setbuildInfoto""
if ((otherDescription of mailInfo) is not"") then
setbuildInfoto" (build "& (otherDescription of mailInfo) &")"
end if
setinfoDatato infoData &"<string># For "& (type of mailInfo) &" version "& (displayVersion of mailInfo) &" ("& (versionNumber of mailInfo) &") on OS X Version "& (startOS of mailInfo) & buildInfo &"</string>"&return
end if
setinfoDatato infoData &"<string>"& (uuid of mailInfo) &"</string>"&return
setendof uuidList to (uuid of mailInfo)
end if
end repeat
return infoData
end convertListToUUIDPList
onconvertListToPlistSection(theList)
-- use a repeat loop to loop over a list of something
setinfoDatato""
setuuidListto {} aslist
setmailComparatorto startingMailComparator
setmessageComparatorto startingMessageComparator
-- Rebuild the list consolidating any duplicate records
setstartInfoto {uuid:"empty"} asrecord
setnewListto {} aslist
repeatwithanInfoin theList
-- For the first time, just assign the current record to the startInfo
if (uuid of startInfo is equal to"empty") then
setstartInfoto anInfo
end if
-- If we have a change of UUIDs, then write the last one
if (uuid of startInfo is not equal to uuid of anInfo) then
-- Add the record to the list
setendof newList to startInfo
-- log startInfo
-- Reset the start to the current one
setstartInfoto anInfo
else
-- Else set the endOS from the start to that of the current
setthe endOS of startInfo to endOS of anInfo
setthe displayVersion of startInfo to displayVersion of anInfo
end if
end repeat
-- Add the last record to the list
-- log startInfo
setendof newList to startInfo
-- Then write out the new list
repeatwithmailInfoin newList
if (uuidList does not contain (uuid of mailInfo)) then
setcomparatorto0
if (type of mailInfo is"mail") then
setmailComparatorto mailComparator +1
setcomparatorto mailComparator
else
setmessageComparatorto messageComparator +1
setcomparatorto messageComparator
end if
-- Build out the string contents
setinfoDatato infoData &"<key>"& uuid of mailInfo &"</key><dict>"
setinfoDatato infoData &"<key>earliest-os-version-display</key><string>"& startOS of mailInfo &"</string>"
setinfoDatato infoData &"<key>latest-os-version-display</key><string>"& endOS of mailInfo &"</string>"
setinfoDatato infoData &"<key>latest-version-comparator</key><integer>"& comparator &"</integer>"
setinfoDatato infoData &"<key>type</key><string>"& type of mailInfo &"</string>"
setinfoDatato infoData &"<key>type-version-display</key><string>"& displayVersion of mailInfo &"</string>"
setinfoDatato infoData &"<key>type-version</key><real>"& versionNumber of mailInfo &"</real>"
setinfoDatato infoData &"</dict>"
setendof uuidList to (uuid of mailInfo)
end if
end repeat
return infoData
end convertListToPlistSection
onconvertListToJSON(theList)
-- use a repeat loop to loop over a list of something
setinfoDatato"{"
setcounterto0
repeatwithmailInfoin theList
-- Build out the json contents
setcommato","
if counter is0then
setcommato""
end if
setinfoDatato infoData & comma &return"e& (fileName of mailInfo) "e&": {"&return&"\"osVersion\": \""& (osVersion of mailInfo) &"\","&return&"\"otherDescription\": \""& (otherDescription of mailInfo) &"\","&return&"\"bundleID\": \""& (bundleID of mailInfo) &"\","&return&"\"shortVersion\": \""& (shortVersion of mailInfo) &"\","&return&"\"version\": "& (versionNumber of mailInfo) &","&return&"\"uuid\": \""& (uuid of mailInfo) &"\","&return&"\"otherExpectedVersion\": "& (expectedVersion of mailInfo) &return&"}"
-- Increment our counter
setcounterto counter +1
end repeat
setinfoDatato infoData &return&"}"
return infoData
end convertListToJSON
oncreatePlistOutputWithSections(sectionList)
setinfoDatato"<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\"\"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">
<plist version=\"1.0\">
<dict>
<key>contents</key>
<dict>"
repeatwithaSectionin sectionList
setinfoDatato infoData & aSection
end repeat
setinfoDatato infoData &return&" </dict>
<key>date</key>
<string>[(REPLACE THIS DATE AND SURROUNDING MARKUP USING date INSTEAD OF string)]</string>
</dict>
</plist>"
-- Date Format => 2012-11-02T16:00:00Z
return infoData
end createPlistOutputWithSections
onwriteFileWithContents(fileName, theContents)
ifmy checkExistence(fileName) then--Attempt to delete existing file
my deleteFile(fileName)
end if
try
settheFiletoopen for access fileName withwrite permission
write theContents to theFile astext
close access theFile
on error
try
close access theFile
end try
end try
end writeFileWithContents
oncheckExistence(fileOrFolderToCheck)
try
alias fileOrFolderToCheck
returntrue
on error
returnfalse
end try
end checkExistence
ondeleteFile(theFilePath)
try
setptoPOSIX pathoffile theFilePath
do shell script"rm ""ed formof p
end try
end deleteFile