Skip to content

Instantly share code, notes, and snippets.

@solace
Last active February 26, 2025 05:35
Show Gist options
  • Save solace/a292a6ce3731651d1bd13a4809d2dbef to your computer and use it in GitHub Desktop.
Save solace/a292a6ce3731651d1bd13a4809d2dbef to your computer and use it in GitHub Desktop.
Create an "I'm feeling lucky" command to surface old notes in Obsidian

%% NOTE: Remove code fences. They were added for gist rendering purposes. %%

<%*
dv = app.plugins.plugins.dataview.api;
function lucky() {
        const threshold = dv.date('now') - dv.duration('14 days');
        const list = dv.pages().where(p => {
                if (p["se-last-reviewed"]) {
                        const lastReviewed = p["se-last-reviewed"].toString().slice(0, 10);
                        return dv.date(lastReviewed) < threshold;
                } else {
                        return false;
                }
        });
        const file = list[Math.floor(Math.random() * list.length)].file;
        app.workspace.getLeaf(true).openFile(app.vault.getAbstractFileByPath(file.path));
}
  
app.commands.addCommand({  
        id: 'app:feelingLucky',  
        name: "I'm feeling lucky",  
        callback: lucky,  
        hotkeys: []  
});  
console.log("[I'm feeling lucky] added to commands");  
%>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment