Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
}
@haemi
Copy link

haemi commented Dec 26, 2021

wouldn't a fm.downloadFileFromiCloud(file) help before line 28 when you (also) use Obsidian on e.g. your Mac?

@rberenguel
Copy link
Author

rberenguel commented Dec 26, 2021

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