forked from DonovanChan/fmfunctions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAppleScriptSendEmail.calc
142 lines (138 loc) · 5.36 KB
/
AppleScriptSendEmail.calc
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
Let ( [
path = Get ( FilePath ) ;
dbName = Middle ( path ; Position ( path ; "/" ; Length ( path ) ; -1 ) + 1 ; 999 ) ;
fieldSplit = Substitute ( resultFieldName ; "::" ; ¶ ) ;
table = GetValue ( fieldSplit ; 1 ) ;
field = GetValue ( fieldSplit ; 2 ) ;
fmpVersion = "FileMaker Pro" & If ( PatternCount ( Get ( ApplicationVersion ) ; "ProAdvanced" ) ; " Advanced" )
] ;
"set fmresult to missing value¶
set theSubject to " & Quote ( subject ) & "¶
set theSender to " & Quote ( from ) & "¶
set theRecipients to split(" & Quote ( to ) & ",\"\¶\")¶
set theCCRecipients to split(" & Quote ( cc ) & ",\"\¶\")¶
set preview to " & GetAsBoolean ( preview ) & " as boolean¶
set theHTML to " & Quote ( htmlBody ) & "¶
set theHTML to searchReplaceText(theHTML,\"\¶\",return)¶
tell application \"Mail\"¶
set theMessage to make new outgoing message with properties {subject:theSubject, visible:preview, sender:theSender}¶
if text 1 thru 1 of theHTML is \"<\" then¶
set html content of theMessage to theHTML¶
else¶
set content of theMessage to theHTML¶
end¶
set theMessageID to id of theMessage¶
tell theMessage¶
repeat with thisRecipient in theRecipients¶
set thisName to my textBetween(thisRecipient,\"\",\" <\")¶
if thisName is \"\" then¶
set thisAddr to thisRecipient¶
else¶
set thisAddr to my textBetween(thisRecipient,\"<\",\">\")¶
end if¶
make new to recipient at end of to recipients with properties {name:thisName, address:thisAddr}¶
end repeat¶
repeat with thisCCRecipient in theCCRecipients¶
set thisName to my textBetween(thisCCRecipient,\"\",\" <\")¶
if thisName is \"\" then¶
set thisAddr to thisCCRecipient¶
else¶
set thisAddr to my textBetween(thisCCRecipient,\"<\",\">\")¶
end if¶
make new to recipient at end of cc recipients with properties {name:thisName, address:thisAddr}¶
end repeat¶
end tell¶
if preview is false then¶
send theMessage¶
close outgoing message id theMessageID saving no¶
end¶
end tell¶
¶
-- Send result to FileMaker¶
if fmresult is not missing value then¶
setField(\"" & dbName & "\",\"" & table & "\",\"" & field & "\",fmResult)¶
end if¶
¶
------------------------------------------------¶
-- HANDLERS¶
------------------------------------------------¶
¶
-- HANDLER: Sets FileMaker field value¶
on setField(databaseName, tableName, fieldName, theValue)¶
tell application " & Quote ( fmpVersion ) & "¶
tell database (databaseName as text)¶
tell table (tableName as text)¶
set field fieldName to theValue¶
end tell¶
end tell¶
end tell¶
end setField¶
¶
-- HANDLER: Splits string into array by delimiter¶
to split(someText, delimiter)¶
set AppleScript's text item delimiters to delimiter¶
set someText to someText's text items¶
set AppleScript's text item delimiters to {\"\"}¶
return someText¶
end split¶
¶
-- HANDLER: Returns text between first occurrences of openDelim and closeDelim¶
on textBetween(theText, openDelim, closeDelim)¶
if openDelim is \"\" then¶
set oStart to 1¶
else¶
set oStart to offset of openDelim in theText¶
end if¶
if oStart = 0 then return \"\"¶
set oStart to oStart + (length of openDelim)¶
if closeDelim is \"\" then¶
set oEnd to (length of theText)¶
else¶
set oEnd to offset of closeDelim in (text (oStart + 1) thru (length of theText) of theText)¶
if oEnd = 0 then return \"\"¶
set oEnd to oEnd + oStart - 1¶
end if¶
set result to text oStart thru oEnd of theText¶
end textBetween¶
¶
-- HANDLER: Searches and replaces string within text block¶
-- Accepts lists in searchString and replaceString¶
to searchReplaceText(theText, searchString, replaceString)¶
set searchString to searchString as list¶
set replaceString to replaceString as list¶
set theText to theText as text¶
¶
set oldTID to AppleScript's text item delimiters¶
repeat with i from 1 to count searchString¶
set AppleScript's text item delimiters to searchString's item i¶
set theText to theText's text items¶
set AppleScript's text item delimiters to replaceString's item i¶
set theText to theText as text¶
end repeat¶
set AppleScript's text item delimiters to oldTID¶
¶
return theText¶
end searchReplaceText¶"
)
/* __________________________________________________
NAME: AppleScriptSendEmail ( resultFieldName ; subject ; from ; to ; cc ; htmlBody ; preview )
PURPOSE: Generates AppleScript that sends HTML email.
EXAMPLES:
AppleScriptSendEmail (
GetFieldName ( global::applescript_result_gt ) ;
"Size matters!" ;
"[email protected], Mr. Spam <[email protected]>" ;
"<p>Just kidding.</p>" ;
False
) // Sends HTML email to multiple recipients without displaying message in Mail
NOTES:
preview parameter must currently be set to False for HTML emails to work.
htmlBody will assume content plain text unless the first character is "<"
Email drafts may occassionally appear when closing Mail 6 in OS X 10.8.
HISTORY:
Created: 2012-04-04 11:26 PT - Donovan Chandler
Modified: 2012-04-24 11:49 PT - Donovan Chandler : Added cc parameter
Modified: 2013-01-30 11:42 PT - Donovan Chandler : Sets message as visible so it can be closed. (Mail 6 on Mountain Lion doesn't close the message otherwise).)
Modified: 2013-02-06 11:01 PT - Donovan Chandler : Enabled preview of plain text emails; Reverted previous change after realizing that HTML doesn't send when message is visible.
*/