Drafts Used for a Zettelkasten

Hey, that’s a great script. I made a small change, so that the script counts the drafts that were changed as well, giving the total of drafts changed AND the total of drafts evaluated. Just a small sanity check for my use!

Here’s the change:

//This scripts prompts for a new title and then replaces the old title with it.// 
//The script also searches the current workspace to locate links based on the old title, and updates them.//


// prompt for new title.// 
let p = Prompt.create();
p.title = "New Title";
p.message = "Enter the new title. The old title and all links using that title will be replaced by the new title.";

p.addTextField("newTitle", "New Title", "", {
    "placeholder": " ",
    "autocorrect": false,
    "autocapitalization": "none",
    "wantsFocus": true
});

p.addButton("Replace");

if (p.show()) {
    //Establish variables for the old and new titles and the old and new links.//
    let oldTitle = draft.displayTitle;
    let oldLink = `[[${oldTitle}]]`;
    let newTitle = p.fieldValues["newTitle"];
    let newLink = `[[${newTitle}]]`;

    //Search the current workspace looking for links based on the old title and update them.
    let count = 0
    let cCount = 0
    var drafts = app.currentWorkspace.query("all");

    for (let d of drafts) {
        if (d.uuid != draft.uuid) {
        		if (d.content.includes(oldLink)){
            	d.content = d.content.replaceAll(oldLink, newLink);
            	d.update();
            	cCount++
            }
           count++
           
        }
    }

    editor.setText(editor.getText().replace(oldTitle, newTitle))
    alert(cCount + " changed in " + count + " drafts evaluated.");
}
6 Likes