Skip to content

Instantly share code, notes, and snippets.

@kmf
Forked from rberenguel/obsidianTodayWidget.js
Created December 27, 2021 06:54
Show Gist options
  • Save kmf/aacc7f606ac6e1df98c31f6a3a4ed4e8 to your computer and use it in GitHub Desktop.
Save kmf/aacc7f606ac6e1df98c31f6a3a4ed4e8 to your computer and use it in GitHub Desktop.
Quick-and-dirty Scriptable (https://scriptable.app/) script to show a medium widget with today's tasks from Obsidian, for my set up (today is in journal/YYYYMMDD.md, tasks are under the `### Plan` heading). Optionally pass "tomorrow" as widget arg to render tomorrow (if it doesn't exist it will fail of course)
let when = args.widgetParameter || 'today'
let now = new Date()
if (when == 'tomorrow') {
now.setDate(now.getDate() + 1);
}
let formatter = new DateFormatter()
formatter.dateFormat = 'yyyyMMdd'
let today = formatter.string(now)
const journalBookmark = "journal"
const journalPath = "journal"
const obsidianURL = `obsidian://open?file=${journalPath}/"${today}.md`
let items = await loadList(today, journalBookmark)
let widget = createWidget(items, today, obsidianURL)
if (config.runsInWidget) {
Script.setWidget(widget)
Script.complete()
} else {
widget.presentMedium()
}
async function loadList(today, journalBookmark) {
const fm = FileManager.iCloud()
const file = fm.bookmarkedPath(journalBookmark) + "/" + today + ".md"
const download = fm.downloadFileFromiCloud(file)
return download.then(() => {
let lines = fm.readString(file)
let found = false
let items = []
for (let line of lines.split('\n')) {
if (found && line.startsWith("- [")) {
let newline = line.replace("- [x]", "✓")
newline = newline.replace("- [ ]", "☓")
items.push(newline)
}
if (line.includes("### Plan")) found = true
}
return items
})
}
function createWidget(items, today, obsidianURL) {
let clippedItems = items
if (clippedItems.length > 5) {
clippedItems = clippedItems.slice(0, 5)
clippedItems.push("…")
}
console.log(clippedItems)
let w = new ListWidget()
w.url = obsidianURL
let titleStack = w.addStack()
let title = titleStack.addText(today)
title.font = Font.boldSystemFont(17)
title.textColor = Color.white()
w.addSpacer(4)
let stack = w.addStack()
stack.layoutVertically()
for (let item of clippedItems) {
let line = stack.addText(item)
line.textColor = Color.white()
}
return w
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment