forked from andyngo/conversable-for-scriptable
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Conversable.js
224 lines (182 loc) · 5.47 KB
/
Conversable.js
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.
// icon-color: orange; icon-glyph: grin-squint;
// initialize contacts
const contacts_list = [
{
name: "Placeholder",
phone: "+0123456789",
type: "sms",
photo: "1.png",
},
{
name: "Placeholder",
phone: "+0123456789",
type: "facetime",
photo: "2.png",
},
{
name: "Placeholder",
phone: "+0123456789",
type: "call",
photo: "3.png",
},
{
name: "Placeholder",
phone: "+0123456789",
type: "whatsapp",
photo: "4.png",
},
];
const SETTINGS = {
BG_COLOR: "#151515",
BG_IMAGE: {
SHOW_BG: false,
IMAGE_PATH: "bg.png",
},
BG_OVERLAY: {
SHOW_OVERLAY: false,
OVERLAY_COLOR: "#111111",
OPACITY: 0.5,
},
PADDING: 8,
TITLE_FONT_SIZE: 18,
PHOTO_SIZE: 60,
NAME_FONT_SIZE: 11,
RANDOMIZE_CONTACTS: false,
NO_OF_CONTACTS_TO_SHOW: 4,
};
// check if RANDOMIZE_CONTACTS is enabled. If it's set to `true`, randomize the contacts_list array.
if (SETTINGS.RANDOMIZE_CONTACTS == true) {
contacts = [...contacts_list]
.sort(() => 0.5 - Math.random())
.slice(0, SETTINGS.NO_OF_CONTACTS_TO_SHOW);
} else {
contacts = [...contacts_list].slice(0, SETTINGS.NO_OF_CONTACTS_TO_SHOW);
}
// A function to download images
async function getImg(image) {
let folderName = "Conversable";
let fm = FileManager.iCloud();
let dir = fm.documentsDirectory();
let path = fm.joinPath(dir + "/" + folderName, image);
let download = await fm.downloadFileFromiCloud(path);
let isDownloaded = await fm.isFileDownloaded(path);
if (fm.fileExists(path)) {
return fm.readImage(path);
} else {
console.log("Error: File does not exist.");
}
}
async function CreateAction(contact) {
let { phone, email, twitter_id, telegram_username } = contact;
let serviceUrl;
let icon;
switch (contact.type) {
case "sms":
serviceUrl = `sms://${phone}`;
icon = "icons/sms.png";
break;
case "call":
serviceUrl = `tel://${phone}`;
icon = "icons/phone.png";
break;
case "mail":
serviceUrl = `mailto://${email}`;
icon = "icons/mail.png";
break;
case "facetime":
serviceUrl = `facetime://${phone}`;
icon = "icons/facetime.png";
break;
case "facetime-audio":
serviceUrl = `facetime-audio://${phone}`;
icon = "icons/facetime.png";
break;
case "whatsapp":
serviceUrl = `whatsapp://send?text=&phone=${phone}`;
icon = "icons/whatsapp.png";
break;
case "twitter":
serviceUrl = `twitter://messages/compose?recipient_id=${twitter_id}`;
icon = "icons/twitter.png";
break;
case "telegram":
serviceUrl = `tg://resolve?domain=${telegram_username}`;
icon = "icons/telegram.png";
break;
}
return { serviceUrl, icon };
}
// A function to create contacts (to be displayed in the widget).
async function CreateContact(contact, row) {
let { PHOTO_SIZE, NAME_FONT_SIZE } = SETTINGS;
let { photo, name } = contact;
let { serviceUrl, icon } = await CreateAction(contact);
let contactStack = row.addStack();
contactStack.layoutVertically();
contactStack.url = serviceUrl;
let photoStack = contactStack.addStack();
photoStack.addSpacer();
let img = await getImg(photo);
let contactPhoto = photoStack.addImage(img);
contactPhoto.imageSize = new Size(PHOTO_SIZE, PHOTO_SIZE);
contactPhoto.cornerRadius = PHOTO_SIZE / 2;
contactPhoto.applyFillingContentMode();
photoStack.addSpacer();
contactStack.addSpacer(4);
let nameStack = contactStack.addStack();
nameStack.addSpacer();
let iconPath = await getImg(icon);
let appIcon = nameStack.addImage(iconPath);
appIcon.imageSize = new Size(12, 12);
nameStack.addSpacer(4);
let contactName = nameStack.addText(name);
contactName.font = Font.mediumSystemFont(NAME_FONT_SIZE);
contactName.lineLimit = 1;
nameStack.addSpacer();
}
async function CreateWidget(contacts) {
let { BG_COLOR, BG_IMAGE, BG_OVERLAY, PADDING, TITLE_FONT_SIZE } = SETTINGS;
let w = new ListWidget();
w.backgroundColor = new Color(BG_COLOR);
w.setPadding(PADDING, PADDING, PADDING, PADDING);
// Show background image if SHOW_BG is set to `true`.
if (BG_IMAGE.SHOW_BG == true) {
let bg = await getImg(BG_IMAGE.IMAGE_PATH);
w.backgroundImage = bg;
}
// Show overlay if SHOW_OVERLAY is set to `true`. For light background images, it is recommended that you turn overlay on so that the contact names and text remain legible.
if (BG_OVERLAY.SHOW_OVERLAY == true) {
let overlayColor = new Color(
BG_OVERLAY.OVERLAY_COLOR,
BG_OVERLAY.OPACITY || 0.3
);
let gradient = new LinearGradient();
gradient.colors = [overlayColor, overlayColor];
gradient.locations = [0, 1];
w.backgroundGradient = gradient;
}
w.addSpacer();
let containerStack = w.addStack();
containerStack.layoutVertically();
let titleStack = containerStack.addStack();
titleStack.addSpacer();
let title = titleStack.addText("Start a conversation with");
title.font = Font.boldRoundedSystemFont(TITLE_FONT_SIZE);
titleStack.addSpacer();
containerStack.addSpacer(16);
let contactRowStack = containerStack.addStack();
contactRowStack.centerAlignContent();
contactRowStack.addSpacer();
contacts.map((contact) => {
CreateContact(contact, contactRowStack);
});
contactRowStack.addSpacer();
w.addSpacer();
Script.setWidget(w);
return w;
}
let w = await CreateWidget(contacts);
w.presentMedium();
Script.complete();