When establishing a group of drafts that are linked to each other with titles, it is important that you be able to change a title (e.g., to clarify it) without breaking all the links. I’m therefore trying to create an action that (1) changes a title, and (2) looks through the current workspace to find and update any links associated with the old title.
Being a rookie at JavaScript, I’ve borrowed heavily from scripts written by others. The logic is:
a. Prompt for the new title.
b. Create variables for the old and new titles and the old and new links.
c. Change the title.
d. Search through the current workspace and update the links.
The action works reliably when executed from my iPhone. When executed from my iMac, the links are changed, but the title is not. I would appreciate help in figuring out what I am doing wrong.
//This script prompts for a new title and then replaces the old title with the new title.//
//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}]]
;
//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) {
d.content = d.content.replaceAll(oldLink, newLink);
d.update();
count++
}
alert(count + "drafts evaluated.");
}