Skip to content

Instantly share code, notes, and snippets.

@grantavery
Last active January 23, 2022 05:27
Show Gist options
  • Save grantavery/edc6769e71e0ddf5215e469c3a539998 to your computer and use it in GitHub Desktop.
Save grantavery/edc6769e71e0ddf5215e469c3a539998 to your computer and use it in GitHub Desktop.
Random Airtable Dictionary Entry Widget

Random Airtable Dictionary Entry Widget

Built and ran as an iOS homescreen widget using the Scriptable app.

This widget takes an Airtable base key and API user key (the app**************/ and key**************/ fields), and then randomly picks one of the records of the Dictionary table to display in the widget.

// Variables used by Scriptable.
// These must be at the very top of the file. Do not edit.
// icon-color: pink; icon-glyph: magic;
// Airtable request to get 500 records:
let baseURL = "https://api.airtable.com/v0/app**************/Dictionary?maxRecords=500&view=Grid%20view"
let r = new Request(baseURL);
r.headers = { 'Authorization': 'Bearer key**************'};
let json = await r.loadJSON();
let dictionaryEntries = json['records'];
// Choose one record randomly and store data from a couple fields in the record:
let randomEntry = (dictionaryEntries[Math.floor(Math.random() * dictionaryEntries.length)]);
let entryWord = randomEntry['fields']['Word/Phrase'];
let entryMeaning = randomEntry['fields']['Meaning/Translation'];
let entryLanguage = randomEntry['fields']['Language'];
let widget = createWidget();
if (config.runsInWidget) {
let widget = createWidget();
Script.setWidget(widget);
Script.complete();
} else {
let widget = createWidget();
widget.presentLarge();
}
function createWidget() {
let w = new ListWidget();
// Make the widget open Airtable when tapped
w.url = "airtable://"
// The actual widget format and data display:
w.backgroundColor = new Color("#2E608C")
let wordAndLanguage = entryWord + " (" + entryLanguage + ")";
let widgetSubText = w.addText(wordAndLanguage);
widgetSubText.textSize = 13;
let widgetText = w.addText(entryMeaning);
widgetText.textSize = 11;
return w
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment