Action

Title and Links??

Posted by Bjb, Last update almost 4 years ago - Unlisted

An attempt to search and replace through the current workspace to ensure links are updated when the title of a note is changed.

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}]]`;
    	//verify that titles and links were correctly formed. 
    	alert(oldTitle + " is Old Title. ");
    	alert(newTitle + " is New Title.");
    	alert(oldLink + " is Old Link. ");
    	alert(newLink + " is New Link.");
    	//Update the title of the draft.//
    	draft.content = draft.content.replaceAll(oldTitle, newTitle);	draft.update();
    	
    	//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++
       		 }
    		}
    		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.