Skip to content

Instantly share code, notes, and snippets.

@rberenguel
Last active August 20, 2023 20:41
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save rberenguel/f6662c82d94a145765c9b4901f8f8cf8 to your computer and use it in GitHub Desktop.
Save rberenguel/f6662c82d94a145765c9b4901f8f8cf8 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()
}
function replaceURLs(task) {
return task.replace(/\[([^\]]+)\]\([^\)]+\)/, "🔗$1")
}
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(replaceURLs(newline))
}
if (line.includes("### Plan")) found = true
}
return items
}).catch(err => {console.log(err); return []})
}
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()
if(clippedItems.length == 0){
let line = stack.addText("File does not exist")
line.textColor = new Color("#772222")
}
for (let item of clippedItems) {
let line = stack.addText(item)
line.textColor = Color.white()
}
return w
}
@rberenguel
Copy link
Author

I also use Obsidian on my Mac indeed, and probably that’s needed if Obsidian is unsynced, I’ll confirm tomorrow (my iPhone should have purged it by then) and probably add it (I don’t think it should hurt at all anyway).

So far I always had everything in sync so saw no issues. Thanks a lot! Otherwise I may have been extremely puzzled if it started failing 🤣

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment