- Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathTemplate.js
More file actions
Latest commit
224 lines (207 loc) · 8.27 KB
/
Copy pathTemplate.js
File metadata and controls
224 lines (207 loc) · 8.27 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
// Variables used by Scriptable.
// These must be at the very top of the file. Do not edit.
// always-run-in-app: true; icon-color: purple;
// icon-glyph: puzzle-piece; share-sheet-inputs: plain-text;
/*
* Copyright 2018 Paul Sidnell
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// V1.0.0
// https://github.com/psidnell/Scriptable
// Whole bunch of little date formatting functions
constWEEKDAYS=['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'];
constMONTHS=['January','February','March','April','May','June','July','August','September','October','November','December'];
functiongetYear(d){returnd.getFullYear();}
functiongetMonth(d){return("0"+(d.getMonth()+1)).slice(-2);}
functiongetDate(d){return("0"+d.getDate()).slice(-2);}
functiongetHHMM(d){return("0"+d.getHours()).slice(-2)+":"+("0"+d.getMinutes()).slice(-2);}
functiongetDayName(d){returnWEEKDAYS[d.getDay()];}
functiongetMonthName(d){returnMONTHS[d.getMonth()];}
// Date formatting for display
functionformatNiceDateTime(d){returngetDayName(d)+' '+getDate(d)+' '+getMonthName(d)+' '+getHHMM(d);}
functionformatNiceDate(d){returngetDate(d)+' '+getMonthName(d)+' '+getYear(d);}
// Date formatting for OmniFocus parsing
functionformatOFDateTime(d){returngetYear(d)+'-'+getMonth(d)+'-'+getDate(d)+' '+getHHMM(d);}
functionformatOFDate(d){returngetYear(d)+'-'+getMonth(d)+'-'+getDate(d);}
// Expand all the variables on a single line
functionprocessLine(line,variables){
letvariableNames=Object.keys(variables);
for(leti=0;i<variableNames.length;i++){
letvariableName=variableNames[i];
letvariable='${'+variableName+'}'
letvalue=variables[variableName];
letregexp=newRegExp('\\$\\{'+variableName+'\\}','g')
line=line.replace(regexp,value);
}
returnline;
}
// Extract the target from a line "... <<target>> ..."
functionextractTarget(line){
letpattern=/.*<<(.*)>>.*/;
letmatch=pattern.exec(line);
returnmatch.length>=2 ? match[1] : null;
}
// Extract a list of the unique ${...} variables from the template
functionextractVariables(text){
lettextOnOneLine=text.split('\n').join('');
letvarMap={};
letpattern=/\$\{([^}]*)\}/g;
letmatch=pattern.exec(textOnOneLine);
while(match){
letvariable=match[1];
varMap[variable]=variable;
match=pattern.exec(textOnOneLine);
}
letvariables=Object.keys(varMap);
variables.sort();
returnvariables;
}
functionaddAddressField(buffer,field){
buffer+=(field&&field.length>0) ? field+'\n' : '';
returnbuffer;
}
functiongetAddressStr(address){
letpostalAddress=address.postalAddress;
letresult='';
result=addAddressField(result,postalAddress.street);
result=addAddressField(result,postalAddress.city);
result=addAddressField(result,postalAddress.state);
result=addAddressField(result,postalAddress.country);
result=addAddressField(result,postalAddress.postalCode);
returnresult;
}
// Create a map of the variables and their values using predfined
// rules or asking for values
asyncfunctiongetVariableValues(variableNames){
letvariables={};
for(leti=0;i<variableNames.length;i++){
letvariableName=variableNames[i];
if('DATE'===variableName){
letvalue=formatNiceDate(newDate());
variables[variableName]=value;
}elseif('TIME'===variableName){
letvalue=getHHMM(newDate());
variables[variableName]=value;
}elseif('DAY'===variableName){
letvalue=getDayName(newDate());
variables[variableName]=value;
}elseif('MONTH'===variableName){
letvalue=getMonthName(newDate());
variables[variableName]=value;
}elseif('YEAR'===variableName){
letvalue=getYear(newDate());
variables[variableName]=value;
}elseif('HERE'===variableName){
letpromise=Location.current();
console.log('Fetching location, please wait a few seconds...');
letlocation=awaitpromise;
console.log('Fetching address...');
letaddress=(awaitLocation.reverseGeocode(location.latitude,location.longitude))[0];
letmapLink='http://maps.apple.com/?daddr='+location.latitude+','+location.longitude;
variables[variableName]=getAddressStr(address)+'\n'+mapLink;
}else{
// Not a predifined variable, ask the user
letalert=newAlert();
alert.message=variableName;
alert.addTextField('value')
alert.addAction('OK');
letpromise=alert.presentAlert();
awaitpromise;
letvalue=alert.textFieldValue(0);
variables[variableName]=value;
}
}
returnvariables;
}
functionhandleErr(val){
console.error(val);
}
// Create an Omnifocus entry
asyncfunctioncreateEntry(target,taskpaper){
letofUrl=null;
leturl=newCallbackURL('omnifocus://x-callback-url/paste');
url.addParameter('target',target);
url.addParameter('content',taskpaper);
// Confirmation alert
letalert=newAlert();
alert.title='Expand OmniFocus Template';
alert.message='To '+target;
alert.addAction('OK');
alert.addCancelAction('Cancel');
letalertPromise=alert.present();
letselId=awaitalertPromise;
if(selId===0){
// console.log('Generated URL: ' + url.getURL())
letofPromise=url.open();
letresult=awaitofPromise;
if(result){
// Haven't timed out.
// result is of the form {"result":"omnifocus:///task/h8s1lykVkjM"}
ofUrl=result.result;
}
}
// console.log('Response from OmniFocus: ' + ofUrl
returnofUrl;
}
// Expand the template, open in OmniFocus
functionexpand(text){
letvariableNames=extractVariables(text);
// console.log('Variable names extracted: ' + variableNames);
getVariableValues(variableNames).then((variables)=>{
// console.log('Variable values: ' variables);
letlines=text.split('\n');
if(lines.length>=1){
lettarget=extractTarget(lines[0]);
// console.log('Target: ' + target);
if(target){
letbuffer='';
letfirstLine=lines[0].replace(/<<.*>>/,'');
buffer+=processLine(firstLine,variables)+'\n';
for(leti=1;i<lines.length;i++){
buffer+=processLine(lines[i],variables)+'\n';
}
// console.log('Expanded template: ' + buffer);
leturlPromise=createEntry(target,buffer);
urlPromise.then((taskUrl)=>{
if(taskUrl){
Safari.open(taskUrl);
}
});
}
}
});
}
// Open the shared template if provided or a test one if run directly from scriptable.
if(args&&args.plainTexts.length>0){
expand(args.plainTexts[0]);
}else{
expand(
'- Test Template<<projects>> @parallel(true) @autodone(true)\n'+
' - This is a test of expansion. It uses all built in variables @parallel(true) @autodone(false)\n'+
' ${VAR}\n'+
' \n'+
' ${DATE}\n'+
' \n'+
' ${TIME}\n'+
' \n'+
' ${DAY}\n'+
' \n'+
' ${MONTH}\n'+
' \n'+
' ${YEAR}\n'
// Slow:
//' \n' +
//' ${HERE}\n'
);
}