Forked from mlschmitt/Merlin_Wisdom_Widget_Scriptable.js
Created
November 17, 2021 07:14
-
-
Save thulsey/5fe690f0df61752653fad4625aee77d9 to your computer and use it in GitHub Desktop.
Random wisdom from Merlin Mann in a handy widget - https://gist.github.com/merlinmann/09af1df28d76ba028b0999f66945fd61
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Variables used by Scriptable. | |
// These must be at the very top of the file. Do not edit. | |
// icon-color: blue; icon-glyph: brain; | |
let items = await loadItems() | |
if (config.runsInWidget) { | |
let widget = await createWidget(items) | |
Script.setWidget(widget) | |
} else if (config.runsWithSiri) { | |
let firstItems = items.slice(0, 5) | |
let table = createTable(firstItems) | |
await QuickLook.present(table) | |
} else { | |
let table = createTable(items) | |
await QuickLook.present(table) | |
} | |
Script.complete() | |
async function createWidget(items) { | |
// Get random wisdom entry | |
let item = items[Math.floor(Math.random()*items.length)] | |
let gradient = new LinearGradient() | |
gradient.locations = [0, 1] | |
let gradientOptions = [ | |
[new Color("#9b59b6"), new Color("#8e44ad")], // purple | |
[new Color("#3498db"), new Color("#2980b9")], // blue | |
[new Color("#1abc9c"), new Color("#16a085")], // teal | |
[new Color("#e67e22"), new Color("#d35400")], // orange | |
[new Color("#e74c3c"), new Color("#c0392b")] // red | |
] | |
let targetGradient = gradientOptions[Math.floor(Math.random()*gradientOptions.length)] | |
gradient.colors = targetGradient | |
let widget = new ListWidget() | |
widget.backgroundColor = targetGradient[1] | |
widget.backgroundGradient = gradient | |
// Add spacer above content to center it vertically. | |
widget.addSpacer() | |
let title = cleanString(item) | |
let titleElement = widget.addText(title) | |
titleElement.font = Font.boldSystemFont(16) | |
titleElement.textColor = Color.white() | |
titleElement.minimumScaleFactor = 0.5 | |
// Add spacing below content to center it vertically. | |
widget.addSpacer() | |
return widget | |
} | |
function createTable(items) { | |
let table = new UITable() | |
for (item of items) { | |
let row = new UITableRow() | |
let title = cleanString(item) | |
let titleCell = row.addText(title) | |
titleCell.minimumScaleFactor = 0.5 | |
titleCell.widthWeight = 60 | |
row.height = 60 | |
row.cellSpacing = 10 | |
row.onSelect = (idx) => { | |
let item = items[idx] | |
let alert = new Alert() | |
alert.message = cleanString(item) | |
alert.presentAlert() | |
} | |
row.dismissOnSelect = false | |
table.addRow(row) | |
} | |
return table | |
} | |
async function loadItems() { | |
let url = "https://gist.githubusercontent.com/merlinmann/09af1df28d76ba028b0999f66945fd61/raw/" | |
let req = new Request(url) | |
let rawBody = await req.loadString() | |
let wisdomComponents = rawBody.split("# The Wisdom So Far")[1] | |
wisdomComponents = wisdomComponents.split("----\n\n[**")[0] | |
let bodyComponents = wisdomComponents.split("\n-") | |
bodyComponents.splice(0, 1) | |
return bodyComponents | |
} | |
function cleanString(str) { | |
let cleanedString = str.replace("Related: ", "") | |
cleanedString = cleanedString.split("\n\n")[0] | |
cleanedString = cleanedString.split("\n* *")[0] | |
cleanedString = cleanedString.split("\n##")[0] | |
cleanedString = cleanedString.trim() | |
let regex = /&#(\d+);/g | |
return cleanedString.replace(regex, (match, dec) => { | |
return String.fromCharCode(dec) | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment