Skip to content

Instantly share code, notes, and snippets.

@somelinguist
Last active May 11, 2021 19:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save somelinguist/5519b06816687488c8f233a8ef6983d0 to your computer and use it in GitHub Desktop.
Save somelinguist/5519b06816687488c8f233a8ef6983d0 to your computer and use it in GitHub Desktop.
// Variables used by Scriptable.
// These must be at the very top of the file. Do not edit.
// icon-color: red; icon-glyph: check;
// Call widget creating function
let widget = await createWidget()
if (!config.runsInWidget) {
await widget.presentMedium()
}
// Tell the system to show the widget.
Script.setWidget(widget)
Script.complete()
async function createWidget(items) {
let fm = FileManager.iCloud()
let docsdirectory = fm.documentsDirectory()
// load two theme files
let lightthemepath = fm.joinPath(docsdirectory, "goodtasklight.json")
let lightthemefile = fm.readString(lightthemepath)
let lighttheme = JSON.parse(lightthemefile)
let darkthemepath = fm.joinPath(docsdirectory, "goodtaskdark.json")
let darkthemefile = fm.readString(darkthemepath)
let darktheme = JSON.parse(darkthemefile)
// Scriptable's UIColor uses hex colors
function parseColorString(input) {
let colorcode = input.substring(1, input.length-2).split(", ").map( function (x) {
let n = Math.floor(parseFloat(x) * 255)
let hex = n.toString(16)
if (hex.length < 2) {
hex = "0" + hex;
}
return hex;
})
let hexColor = "#" + colorcode.join("")
console.log(hexColor)
return new Color(hexColor)
}
// Make a dictionary that contains the values from both themes' colors
let combinedColors = {}
for (const key in lighttheme) {
if (key.endsWith("Color")) {
console.log(key)
let light = parseColorString(lighttheme[key])
// If the light and dark themes both specify the given key, make a dynamic color
if (darktheme.hasOwnProperty(key)) {
let dark = parseColorString(darktheme[key])
combinedColors[key] = Color.dynamic(light, dark)
}
// Otherwise just return the light theme's color
else {
combinedColors[key] = light
}
}
}
// Construct the widget with dummy text
let w = new ListWidget()
w.setPadding(4, 4, 4, 4)
w.backgroundColor = combinedColors.backgroundColor
let text = w.addText("Plain Text")
text.textColor = combinedColors.textColor
let dateTime = new Date()
let dateText = w.addText(dateTime.toISOString())
dateText.textColor = combinedColors.overdueTextColor
// etc., etc.
return w
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment