-
Notifications
You must be signed in to change notification settings - Fork 6
/
Email to Evernote.applescript
1092 lines (901 loc) · 35.1 KB
/
Email to Evernote.applescript
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
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
(*
Advanced Apple Mail to Evernote
Version 1.0
https://github.com/scouture
// ATTRIBUTION
This script is forked from version 2.04 of "Apple Mail to Evernote" script by Veritrope.com
http://veritrope.com/code/apple-mail-to-evernote/
// TERMS OF USE:
This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.
To view a copy of this license, visit http://creativecommons.org/licenses/by-nc-sa/3.0/ or send a letter to Creative Commons, 444 Castro Street, Suite 900, Mountain View, California, 94041, USA.
// IMPORTANT LINKS:
-- Original Project Page: http://veritrope.com/code/apple-mail-to-evernote
-- GROWL (App Store Version) (Optional): http://bit.ly/GrowlApp
-- terminal-notifier (Optional): https://github.com/alloy/terminal-notifier/downloads
-- FastScripts (Optional): http://bit.ly/FastScripts
-- Alfred (Optional): http://www.alfredapp.com
// REQUIREMENTS:
THIS SCRIPT REQUIRES LION OR GREATER (OS X 10.7+) TO RUN WITHOUT MODIFICATION
// INSTALLATION:
-- You can save this script to /Library/Scripts/Mail Scripts and launch it using the system-wide script menu from the Mac OS X menu bar. (The script menu can be activated using the AppleScript Utility application).
-- To use, highlight the email messages you want to archive into Evernote and run this script file;
-- The "User Switches" below allow you to customize the way this script works.
-- You can save this script as a service and trigger it with a keyboard shortcut.
(Optional but recommended)
Easier Keyboard Shortcut with FastScripts
-- Download and Install FastScripts here:
-- http://bit.ly/FastScripts
Assign to Alfred keyword
-- Download and install Alfred here:
-- http://www.alfredapp.com
// CHANGELOG:
* 1.00 (February 16, 2013)
- Fork from v.2.0.4 of "Apple Mail to Evernote" script by Veritrope.com (http://veritrope.com/code/apple-mail-to-evernote/)
- Made GROWL notifications optional
- Added OSX notifications with "terminal-notifier"
- Added the ability to turn off notifications
- Added mail archiving and flagging
- Code cleanup
*)
(*
======================================
// USER SWITCHES
======================================
*)
-- SET THIS TO "OFF" IF YOU WANT TO SKIP THE TAGGING/NOTEBOOK DIALOG
-- AND SEND ITEMS DIRECTLY INTO YOUR DEFAULT NOTEBOOK
property tagging_Switch : "ON"
-- IF YOU'VE DISABLED THE TAGGING/NOTEBOOK DIALOG,
-- TYPE THE NAME OF THE NOTEBOOK YOU WANT TO SEND ITEM TO
-- BETWEEN THE QUOTES IF IT ISN'T YOUR DEFAULT NOTEBOOK.
-- (EMPTY SENDS TO DEFAULT)
property EVnotebook : ""
-- IF TAGGING IS ON AND YOU'D LIKE TO CHANGE THE DEFAULT TAG,
-- TYPE IT BETWEEN THE QUOTES ("Email Message" IS DEFAULT)
property defaultTag : ""
-- SET THIS "ON" IF YOU WISH TO ACTIVATE ARCHIVING OF PROCESSED MESSAGES IN '<year> Archive' MAILBOX
property archiving : "ON"
-- SET THIS "ON" IF YOU WISH TO FLAG PROCESSED MESSAGES
property flagging : "ON"
-- SET THIS TO "GROWL", "OSX" OR "OFF". FOR OSX NOTIFICATIONS, YOU MUST INSTALL 'terminial-notifier.app' AND SET COMMAND PATH IN 'terminal_notifier_path' PROPERTY
property notifications : "OSX"
(*
======================================
// OTHER PROPERTIES
======================================
*)
-- Global properties
property successCount : 0
property growl_Running : "false"
property osxNotifications_Available : "false"
property myTitle : "Mail Item"
property theMessages : {}
property thisMessage : ""
property itemNum : "0"
property attNum : "0"
property errNum : "0"
property userTag : ""
property EVTag : {}
property multiHTML : ""
property theSourceItems : {}
property mySource : ""
property decode_Success : ""
property finalHTML : ""
property myHeaders : ""
property mysource_Paragraphs : {}
property base64_Raw : ""
-- Archive properties
property archive_mailbox_label : "Archive" -- Will generate "<year> <label>"
property archive_flag : 3
-- Notification properties
property terminal_notifier_path : "/usr/local/bin/terminal-notifier.app/Contents/MacOS/terminal-notifier"
property notificationAppName : "Apple Mail to Evernote"
property notificationAction : "com.apple.Mail"
property notificationIcon : "Mail"
(*
======================================
// MAIN PROGRAM
======================================
*)
--RESET ITEMS
set successCount to "0"
set errNum to "0"
set AppleScript's text item delimiters to ""
try
-- Check for Growl
if notifications is "GROWL" then
-- Activate Grown
my Growl_Check()
end if
-- Set up activites
my item_Check()
-- Check for selected messages
if theMessages is not {} then
-- Get messages count
my item_Count(theMessages)
-- Announce the export of items
my process_Notification(itemNum, attNum)
-- Process mail items for export
my mail_Process(theMessages)
else
-- No messages selected
set successCount to -1
end if
-- Show results notification
my processed_Notification(successCount, errNum)
-- Error handling
on error errText number errNum
if growl_Running is true then
if errNum is -128 then
-- Failure notification for cancel
notification("Failure Notification", "User Cancelled", "Failed to export!", notificationAppName, notificationAction, notificationIcon)
else
-- Failure notification for error
notification("Failure Notification", "Import Failure", "Failed to export " & return & myTitle & "\" due to the following error: " & return & errText, notificationAppName, notificationAction, notificationIcon)
end if
-- Non notification error message
else if growl_Running is false and osxNotifications_Available is false then
display dialog "Item Failed to Import: " & errNum & return & errText with icon 0
end if
end try
(*
======================================
// PREPARATORY SUBROUTINES
=======================================
*)
-- App detect
on appIsRunning(appName)
tell application "System Events" to (name of processes) contains appName
end appIsRunning
-- Set up activities
on item_Check()
set myPath to (path to home folder)
tell application "Mail"
try
set theMessages to selection
end try
end tell
end item_Check
-- Get count of items and attachments
on item_Count(theMessages)
tell application "Mail"
set itemNum to count of theMessages
set attNum to 0
repeat with theMessage in theMessages
set attNum to attNum + (count of mail attachment of theMessage)
end repeat
end tell
end item_Count
(*
======================================
// TAGGING AND NOTEBOOK SUBROUTINES
=======================================
*)
-- Tagging and notebook selection dialog
on tagging_Dialog()
try
display dialog "" & Â
"Please Enter Your Tags Below:
(Multiple Tags Separated By Commas)" with title "Veritrope.com | Apple Mail to Evernote Export" default answer defaultTag buttons {"Create in Default Notebook", "Select Notebook from List", "Cancel"} default button "Create in Default Notebook" cancel button Â
"Cancel" with icon path to resource "Evernote.icns" in bundle (path to application "Evernote")
set dialogresult to the result
set userInput to text returned of dialogresult
set ButtonSel to button returned of dialogresult
set theDelims to {","}
on error number -128
set errNum to -128
end try
-- Assemble tag list
set theTags to my Tag_List(userInput, theDelims)
-- Reset, final check and formating of tags
set EVTag to {}
set EVTag to my Tag_Check(theTags)
-- Select Notebook
if ButtonSel is "Select Notebook from List" then set EVnotebook to my Notebook_List()
end tagging_Dialog
-- Get Evernote's default Notebook
on default_Notebook()
tell application "Evernote"
set get_defaultNotebook to every notebook whose default is true
if EVnotebook is "" then
set EVnotebook to name of (item 1 of get_defaultNotebook) as text
end if
end tell
end default_Notebook
-- Tag selection subroutine
on Tag_List(userInput, theDelims)
set oldDelims to AppleScript's text item delimiters
set theList to {userInput}
repeat with aDelim in theDelims
set AppleScript's text item delimiters to aDelim
set newList to {}
repeat with anItem in theList
set newList to newList & text items of anItem
end repeat
set theList to newList
end repeat
set AppleScript's text item delimiters to oldDelims
return theList
end Tag_List
-- Creates tags if they don't exist
on Tag_Check(theTags)
tell application "Evernote"
set finalTags to {}
repeat with theTag in theTags
if (not (tag named theTag exists)) then
try
set makeTag to make tag with properties {name:theTag}
set end of finalTags to makeTag
end try
else
set end of finalTags to tag theTag
end if
end repeat
end tell
return finalTags
end Tag_Check
-- Evernote Notebook selection subroutine
on Notebook_List()
tell application "Evernote"
activate
set listOfNotebooks to {} (*PREPARE TO GET EVERNOTE'S LIST OF NOTEBOOKS *)
set EVNotebooks to every notebook (*GET THE NOTEBOOK LIST *)
repeat with currentNotebook in EVNotebooks
set currentNotebookName to (the name of currentNotebook)
copy currentNotebookName to the end of listOfNotebooks
end repeat
set Folders_sorted to my simple_sort(listOfNotebooks) (*SORT THE LIST *)
set SelNotebook to choose from list of Folders_sorted with title "Select Evernote Notebook" with prompt Â
"Current Evernote Notebooks" OK button name "OK" cancel button name "New Notebook" (*USER SELECTION FROM NOTEBOOK LIST *)
if (SelNotebook is false) then (*CREATE NEW NOTEBOOK OPTION *)
set userInput to Â
text returned of (display dialog "Enter New Notebook Name:" default answer "")
set EVnotebook to userInput
else
set EVnotebook to item 1 of SelNotebook
end if
end tell
end Notebook_List
(*
======================================
// UTILITY SUBROUTINES
=======================================
*)
-- Extraction subroutine
on extractBetween(SearchText, startText, endText)
set tid to AppleScript's text item delimiters
set AppleScript's text item delimiters to startText
set endItems to text of text item -1 of SearchText
set AppleScript's text item delimiters to endText
set beginningToEnd to text of text item 1 of endItems
set AppleScript's text item delimiters to tid
return beginningToEnd
end extractBetween
-- Sort subroutine
on simple_sort(my_list)
set the index_list to {}
set the sorted_list to {}
repeat (the number of items in my_list) times
set the low_item to ""
repeat with i from 1 to (number of items in my_list)
if i is not in the index_list then
set this_item to item i of my_list as text
if the low_item is "" then
set the low_item to this_item
set the low_item_index to i
else if this_item comes before the low_item then
set the low_item to this_item
set the low_item_index to i
end if
end if
end repeat
set the end of sorted_list to the low_item
set the end of the index_list to the low_item_index
end repeat
return the sorted_list
end simple_sort
(*
======================================
// PROCESS MAIL ITEMS SUBROUTINE
=======================================
*)
on mail_Process(theMessages)
--CHECK DEFAULT NOTEBOOK
my default_Notebook()
tell application "Mail"
try
if tagging_Switch is "ON" then my tagging_Dialog()
repeat with thisMessage in theMessages
try
-- Get message info
set myTitle to the subject of thisMessage
set myContent to the content of thisMessage
set mySource to the source of thisMessage
set ReplyAddr to the reply to of thisMessage
set EmailDate to the date received of thisMessage
set allRecipients to (every to recipient of item 1 of thisMessage)
-- Assemble all to : resipients for header
set toRecipients to ""
repeat with allRecipient in allRecipients
set toName to (name of allRecipient)
set toEmail to (address of allRecipient)
set toCombined to toName & space & "(" & toEmail & ")<br/>"
set toRecipients to (toRecipients & toCombined as string)
end repeat
-- Create mail message URL
set theRecipient to ""
set ex to ""
set MsgLink to ""
try
set theRecipient to ""
set theRecipient to the address of to recipient 1 of thisMessage
set MsgLink to "message://%3c" & thisMessage's message id & "%3e"
if theRecipient is not "" then set ex to my extractBetween(ReplyAddr, "<", ">") -- extract the Address
end try
-- HTML email functions
set theBoundary to my extractBetween(mySource, "boundary=\"", "\"")
set theMessagestart to (return & "--" & theBoundary)
set theMessageEnd to ("--" & theBoundary & return & "Content-Type:")
set paraSource to paragraphs of mySource
set myHeaderlines to paragraphs of (all headers of thisMessage as rich text)
-- Get content type
repeat with myHeaderline in myHeaderlines
if myHeaderline starts with "Content-Type: " then
set myHeaders to my extractBetween(myHeaderline, "Content-Type: ", ";")
end if
end repeat
set cutSource to my stripHeader(paraSource, myHeaderlines)
set evHTML to cutSource
end try
-- Make header template
set the_Template to "
<table border=\"1\" width=\"100%\" cellspacing=\"0\" cellpadding=\"2\">
<tbody>
<tr BGCOLOR=\"#ffffff\">
<td valign=\"top\"><font color=\"#797979\"><strong>From: </strong> </td>
<td valign=\"top\" ><a href=\"mailto:" & ex & "\">" & ex & "</a></td>
</tr>
<tr BGCOLOR=\"#ffffff\">
<td valign=\"top\"><font color=\"#797979\"><strong>Subject: </strong> </td>
<td valign=\"top\" ><strong>" & myTitle & "</strong></td>
</tr>
<tr BGCOLOR=\"#ffffff\">
<td valign=\"top\"><font color=\"#797979\"><strong>Date / Time: </strong></td>
<td valign=\"top\">" & EmailDate & "</td>
</tr>
<tr BGCOLOR=\"#ffffff\">
<td valign=\"top\"><font color=\"#797979\"><strong>To:</strong></td>
<td valign=\"top\">" & toRecipients & "</td>
</tr>
</tbody>
</table>
<hr />"
-- Sent item to Evernote subroutine
my make_Evernote(myTitle, EVTag, EmailDate, MsgLink, myContent, mySource, theBoundary, theMessagestart, theMessageEnd, myHeaders, thisMessage, evHTML, EVnotebook, the_Template)
-- Run message post process subroutine
my mail_post_Process(theMessages)
end repeat
end try
end tell
end mail_Process
-- Archiving and flagging of processed emails
on mail_post_Process(theMessages)
tell application "Mail"
repeat with m in theMessages
-- Flag message
if flagging is "ON" then
set flag index of m to archive_flag as integer
end if
-- Archive message
if archiving is "ON" then
set mb to mailbox of m
set acc to account of mb
set archive_mailbox to get (the (year of (current date)) as string) & " " & archive_mailbox_label
log "here"
log archive_mailbox
try
set archive to acc's mailbox archive_mailbox
on error
display alert "No '" & archive_mailbox & "' mailbox found for account '" & acc & "'."
return
end try
try
move m to archive
on error
display alert "Error"
return
end try
end if
end repeat
end tell
end mail_post_Process
(*
======================================
// MAKE ITEM IN EVERNOTE SUBROUTINE
=======================================
*)
on make_Evernote(myTitle, EVTag, EmailDate, MsgLink, myContent, mySource, theBoundary, theMessagestart, theMessageEnd, myHeaders, thisMessage, evHTML, EVnotebook, the_Template)
tell application "Evernote"
try
-- Is it a text email?
if myHeaders contains "text/plain" then
set n to create note with html the_Template title myTitle notebook EVnotebook
if EVTag is not {} then assign EVTag to n
tell n to append text myContent
set creation date of n to EmailDate
set source URL of n to MsgLink
-- Is it multipart alternative?
else if myHeaders contains "multipart/alternative" then
-- Check for Base64
set base64Detect to my base64_Check(mySource)
-- If message if Base64 encoded
if base64Detect is true then
set multiHTML to my extractBetween(mySource, "Content-Transfer-Encoding: base64", theBoundary)
-- Strip out content-disposition, if necessary
if multiHTML contains "Content-Disposition: inline" then set multiHTML to my extractBetween(multiHTML, "Content-Disposition: inline", theBoundary)
if multiHTML contains "Content-Transfer-Encoding: 7bit" then set multiHTML to my extractBetween(multiHTML, "Content-Transfer-Encoding: 7bit", theBoundary)
-- Decode Base64
set baseHTML to do shell script "echo " & (quoted form of multiHTML) & "| openssl base64 -d"
-- Make note in Evernote
set n to create note with html the_Template title myTitle notebook EVnotebook
if EVTag is not {} then assign EVTag to n
tell n to append html baseHTML
set creation date of n to EmailDate
set source URL of n to MsgLink
else
-- If message is not Base64 encoded
set finalHTML to my htmlFix(mySource, theBoundary, myContent)
if decode_Success is true then
-- Make note in Evernote
set n to create note with html the_Template title myTitle notebook EVnotebook
if EVTag is not {} then assign EVTag to n
tell n to append html finalHTML
set creation date of n to EmailDate
set source URL of n to MsgLink
else
-- Make note in Evernote
set n to create note with html the_Template title myTitle notebook EVnotebook
if EVTag is not {} then assign EVTag to n
tell n to append text myContent
set creation date of n to EmailDate
set source URL of n to MsgLink
end if
end if
-- Is it multipart mixed?
else if myHeaders contains "multipart" then
if mySource contains "Content-Type: text/html" then
-- Check for Base64
set base64Detect to my base64_Check(mySource)
-- If message is Base64 encoded
if base64Detect is true then
set baseHTML to my base64_Decode(mySource)
-- Make note in Evernote
set n to create note with html the_Template title myTitle notebook EVnotebook
if EVTag is not {} then assign EVTag to n
tell n to append html baseHTML
set creation date of n to EmailDate
set source URL of n to MsgLink
-- If message is not Base64 encoded
else if base64Detect is false then
set finalHTML to my htmlFix(mySource, theBoundary, myContent)
if decode_Success is true then
-- Make note in Evernote
set n to create note with html the_Template title myTitle notebook EVnotebook
if EVTag is not {} then assign EVTag to n
tell n to append html finalHTML
set creation date of n to EmailDate
set source URL of n to MsgLink
else
-- Make note in Evernote
set n to create note with html the_Template title myTitle notebook EVnotebook
if EVTag is not {} then assign EVTag to n
tell n to append text myContent
set creation date of n to EmailDate
set source URL of n to MsgLink
end if
end if
else if mySource contains "text/plain" then
-- Make note in Evernote
set n to create note with html the_Template title myTitle notebook EVnotebook
if EVTag is not {} then assign EVTag to n
tell n to append text myContent
set creation date of n to EmailDate
set source URL of n to MsgLink
end if
-- Multipart mixed
-- Other types of HTML-encoding
else
-- Check for Base64
set base64Detect to my base64_Check(mySource)
-- If message is Base64 encoded
if base64Detect is true then
set finalHTML to my base64_Decode(mySource)
else
set multiHTML to my extractBetween(evHTML, "</head>", "</html>")
set finalHTML to my htmlFix(multiHTML, theBoundary, myContent) as text
end if
-- Make note in Evernote
set n to create note with html the_Template title myTitle notebook EVnotebook
if EVTag is not {} then assign EVTag to n
tell n to append html finalHTML
set creation date of n to EmailDate
set source URL of n to MsgLink
-- End of message processing
end if
-- Start of attachment processing
tell application "Mail"
-- If attachment present, run attachment subroutine
if thisMessage's mail attachments is not {} then my attachment_process(thisMessage, n)
end tell
-- Item has finished. Count as success
set successCount to successCount + 1
end try
end tell
log "successCount: " & successCount
end make_Evernote
(*
======================================
// ATTACHMENT SUBROUTINES
=======================================
*)
-- Folder exists?
on f_exists(ExportFolder)
try
set myPath to (path to home folder)
get ExportFolder as alias
set SaveLoc to ExportFolder
on error
tell application "Finder" to make new folder with properties {name:"Temp Export From Mail"}
end try
end f_exists
-- Attachment processing
on attachment_process(thisMessage, n)
tell application "Mail"
-- Make sure text item delimiters are default
set AppleScript's text item delimiters to ""
-- Temp files processed on the Desktop
set ExportFolder to ((path to desktop folder) & "Temp Export From Mail:") as string
set SaveLoc to my f_exists(ExportFolder)
-- Process attachments
set theAttachments to thisMessage's mail attachments
set attCount to 0
repeat with theAttachment in theAttachments
set theFileName to ExportFolder & theAttachment's name
try
save theAttachment in file theFileName
end try
tell application "Evernote"
tell n to append attachment file theFileName
end tell
-- Silent delete of temp file
set trash_Folder to path to trash folder from user domain
do shell script "mv " & quoted form of POSIX path of theFileName & space & quoted form of POSIX path of trash_Folder
end repeat
-- Silent delete of temp folder
set success to my trashfolder(SaveLoc)
end tell
end attachment_process
-- Silent delete of temp folder
on trashfolder(SaveLoc)
try
set trashfolderpath to ((path to trash) as Unicode text)
set srcfolderinfo to info for (SaveLoc as alias)
set srcfoldername to name of srcfolderinfo
set SaveLoc to quoted form of POSIX path of SaveLoc
set counter to 0
repeat
if counter is equal to 0 then
set destfolderpath to trashfolderpath & srcfoldername & ":"
else
set destfolderpath to trashfolderpath & srcfoldername & " " & counter & ":"
end if
try
set destfolderalias to destfolderpath as alias
on error
exit repeat
end try
set counter to counter + 1
end repeat
set destfolderpath to quoted form of POSIX path of destfolderpath
set command to "ditto " & SaveLoc & space & destfolderpath
do shell script command
-- this won't be executed if the ditto command errors
set command to "rm -r " & SaveLoc
do shell script command
return true
on error
return false
end try
end trashfolder
(*
======================================
// HTML CLEANUP SUBROUTINES
=======================================
*)
-- Header strip
on stripHeader(paraSource, myHeaderlines)
-- Find the last non-empty header line
set lastheaderline to ""
set n to count (myHeaderlines)
repeat while (lastheaderline = "")
set lastheaderline to item n of myHeaderlines
set n to n - 1
end repeat
-- Compare header to source
set sourcelength to (count paraSource)
repeat with n from 1 to sourcelength
if (item n of paraSource is equal to "") then exit repeat
end repeat
-- Strip out headers
set cutSourceItems to (items (n + 1) thru sourcelength of paraSource)
set oldDelims to AppleScript's text item delimiters
set AppleScript's text item delimiters to return
set cutSource to (cutSourceItems as text)
set AppleScript's text item delimiters to oldDelims
return cutSource
end stripHeader
-- Base64 check
on base64_Check(mySource)
set base64Detect to false
set base64MsgStr to "Content-Transfer-Encoding: base64"
set base64ContentType to "Content-Type: text"
set base64MsgOffset to offset of base64MsgStr in mySource
set base64ContentOffset to offset of base64ContentType in mySource
set base64Offset to base64MsgOffset - base64ContentOffset as real
set theOffset to base64Offset as number
if theOffset is not greater than or equal to 50 then
if theOffset is greater than -50 then set base64Detect to true
end if
return base64Detect
end base64_Check
-- Base64 decode
on base64_Decode(mySource)
-- Use TID to quickly isolate Base64 data
set oldDelim to AppleScript's text item delimiters
set AppleScript's text item delimiters to "Content-Type: text/html"
set base64_Raw to second text item of mySource
set AppleScript's text item delimiters to linefeed & linefeed
set base64_Raw to second text item of base64_Raw
set AppleScript's text item delimiters to "-----"
set multiHTML to first text item of base64_Raw
set AppleScript's text item delimiters to oldDelim
-- Decode Base64
set baseHTML to do shell script "echo " & (quoted form of multiHTML) & "| openssl base64 -d"
return baseHTML
end base64_Decode
-- HTML fix
on htmlFix(evHTML, theBoundary, myContent)
set oldDelims to AppleScript's text item delimiters
set multiHTML to evHTML as string
-- Test for / strip out header
set paraSource to paragraphs of multiHTML
if item 1 of paraSource contains "Received:" then
set myHeaderlines to (item 1 of paraSource)
set multiHTML to my stripHeader(paraSource, myHeaderlines)
end if
-- Trim ending
if multiHTML contains "</html>" then
set multiHTML to my extractBetween(multiHTML, "Content-Type: text/html", "</html>")
else
set multiHTML to my extractBetween(multiHTML, "Content-Type: text/html", theBoundary)
end if
set paraSource to paragraphs of multiHTML
-- Test for / strip out leading semi-colon
if item 1 of paraSource contains ";" then
set myHeaderlines to (item 1 of paraSource)
set multiHTML to my stripHeader(paraSource, myHeaderlines)
set paraSource to paragraphs of multiHTML
end if
-- Test for empty line / clean subsequent encoding info, if necessary
if item 1 of paraSource is "" then
-- Test for / strip out content-transfer-encoding
if item 2 of paraSource contains "Content-Transfer-Encoding" then
set myHeaderlines to (item 2 of paraSource)
set multiHTML to my stripHeader(paraSource, myHeaderlines)
set paraSource to paragraphs of multiHTML
end if
-- Test for / strip out charset
if item 2 of paraSource contains "charset" then
set myHeaderlines to (item 2 of paraSource)
set multiHTML to my stripHeader(paraSource, myHeaderlines)
set paraSource to paragraphs of multiHTML
end if
end if
-- Test for / strip out content-transfer-encoding
if item 1 of paraSource contains "Content-Transfer-Encoding" then
set myHeaderlines to (item 1 of paraSource)
set multiHTML to my stripHeader(paraSource, myHeaderlines)
set paraSource to paragraphs of multiHTML
end if
-- Test for / strip out charset
if item 1 of paraSource contains "charset" then
set myHeaderlines to (item 1 of paraSource)
set multiHTML to my stripHeader(paraSource, myHeaderlines)
set paraSource to paragraphs of multiHTML
end if
-- Clean content
set AppleScript's text item delimiters to theBoundary
set theSourceItems to text items of multiHTML
set AppleScript's text item delimiters to ""
set theEncoded to theSourceItems as text
set AppleScript's text item delimiters to "%"
set theSourceItems to text items of theEncoded
set AppleScript's text item delimiters to "&#" & "37;" as string
set theEncoded to theSourceItems as text
set AppleScript's text item delimiters to "="
set theSourceItems to text items of theEncoded
set AppleScript's text item delimiters to "%"
set theEncoded to theSourceItems as text
set AppleScript's text item delimiters to "%\""
set theSourceItems to text items of theEncoded
set AppleScript's text item delimiters to "=\""
set theEncoded to theSourceItems as text
set AppleScript's text item delimiters to "%" & (ASCII character 13)
set theSourceItems to text items of theEncoded
set AppleScript's text item delimiters to ""
set theEncoded to theSourceItems as text
set AppleScript's text item delimiters to "%%"
set theSourceItems to text items of theEncoded
set AppleScript's text item delimiters to "%"
set theEncoded to theSourceItems as text
set AppleScript's text item delimiters to "%" & (ASCII character 10)
set theSourceItems to text items of theEncoded
set AppleScript's text item delimiters to ""
set theEncoded to theSourceItems as text
set AppleScript's text item delimiters to "%0A"
set theSourceItems to text items of theEncoded
set AppleScript's text item delimiters to ""
set theEncoded to theSourceItems as text
set AppleScript's text item delimiters to "%09"
set theSourceItems to text items of theEncoded
set AppleScript's text item delimiters to ""
set theEncoded to theSourceItems as text
set AppleScript's text item delimiters to "%C2%A0"
set theSourceItems to text items of theEncoded
set AppleScript's text item delimiters to " "
set theEncoded to theSourceItems as text
set AppleScript's text item delimiters to "%20"
set theSourceItems to text items of theEncoded
set AppleScript's text item delimiters to " "
set theEncoded to theSourceItems as text
set AppleScript's text item delimiters to (ASCII character 10)
set theSourceItems to text items of theEncoded
set AppleScript's text item delimiters to ""
set theEncoded to theSourceItems as text
set AppleScript's text item delimiters to "="
set theSourceItems to text items of theEncoded
set AppleScript's text item delimiters to "&#" & "61;" as string
set theEncoded to theSourceItems as text
set AppleScript's text item delimiters to "$"
set theSourceItems to text items of theEncoded
set AppleScript's text item delimiters to "&#" & "36;" as string
set theEncoded to theSourceItems as text
set AppleScript's text item delimiters to "'"
set theSourceItems to text items of theEncoded
set AppleScript's text item delimiters to "'"
set theEncoded to theSourceItems as text
set AppleScript's text item delimiters to "\""
set theSourceItems to text items of theEncoded
set AppleScript's text item delimiters to "\\\""
set theEncoded to theSourceItems as text
set AppleScript's text item delimiters to oldDelims
set trimHTML to my extractBetween(theEncoded, "</head>", "</html>")
set theHTML to myContent
try
set decode_Success to false
-- UTF-8 conversion
set NewEncodedText to do shell script "echo " & quoted form of trimHTML & " | iconv -t UTF-8 "
set the_UTF8Text to quoted form of NewEncodedText
-- URL decode conversion
set theDecodeScript to "php -r \"echo urldecode(" & the_UTF8Text & ");\"" as text
set theDecoded to do shell script theDecodeScript
-- Fix for apostrophe / percent / equal issues
set AppleScript's text item delimiters to "'"
set theSourceItems to text items of theDecoded
set AppleScript's text item delimiters to "'"
set theDecoded to theSourceItems as text
set AppleScript's text item delimiters to "&#" & "37;" as string
set theSourceItems to text items of theDecoded
set AppleScript's text item delimiters to "%"
set theDecoded to theSourceItems as text
set AppleScript's text item delimiters to "&#" & "61;" as string
set theSourceItems to text items of theDecoded
set AppleScript's text item delimiters to "="
set theDecoded to theSourceItems as text
--RETURN THE VALUE
set finalHTML to theDecoded
set decode_Success to true
return finalHTML
end try
end htmlFix
(*
======================================
// NOTIFICATIONS SUBROUTINES
=======================================
*)
-- Check for Growl and initialize
on Growl_Check()
if appIsRunning("Growl") then
set growl_Running to true
tell application "GrowlHelperApp"
set allNotificationsFiles to {"Import Notification", "Success Notification", "Failure Notification"}
set enabledNotificationsFiles to {"Import Notification", "Success Notification", "Failure Notification"}
register as application Â
notificationAppName all notifications allNotificationsFiles Â
default notifications enabledNotificationsFiles Â
icon of application notificationIcon
end tell
end if
end Growl_Check