Action

Title and Links

Posted by Bjb, Last update over 4 years ago

Prompts for a new title and searches the current workspace updating any links dependent upon the old title.

Steps

  • script

    //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
        var drafts = app.currentWorkspace.query("all");
    
        for (let d of drafts) {
            if (d.uuid != draft.uuid) {
                d.content = d.content.replaceAll(oldLink, newLink);
                d.update();
                count++
            }
        }
    
        editor.setText(editor.getText().replace(oldTitle, newTitle))
        alert(count + " drafts evaluated.");
    }
    
    
    

Options

  • After Success Nothing
    Notification Error
    Log Level Error
Items available in the Drafts Directory are uploaded by community members. Use appropriate caution reviewing downloaded items before use.