Script Works on iPhone but not on iMac

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


if ( {
//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);
	alert(count + "drafts evaluated.");

The easiest way to share action content when asking for help is to share the action to the action directory (in action edit screen) as “unlisted” and post the link here. Then people can install the action to look at it. I tried pasting this script in, but am getting compile errors.

My guess, looking at this is that you are creating a race condition updating the draft currently in the editor, however, which might come out a little different on different platforms. Presumably the current draft is also in the workspace you are looping over, so you are updating it twice.

If you move the update of the current draft after the loop, you could avoid this, but also you could prevent updating the current draft in the loop, since it should not be needed, like:

for (let d of drafts) {
    if (d.uuid != draft.uuid) {
        d.content = d.content.replaceAll(oldLink, newLink);

Thanks for the information on how to share a link to an Action.

I tried both of your suggestions and couldn’t get either to work. The current draft is in the workspace I am looping over.

Definitely some tricky timing issues that explain the differences in behavior. I will look into those, but for the time being, it’s easy to workaround by modifying the text directly in the editor for the current draft.

Try this modified version.

The version you sent changes the title, but not the links in other drafts that need to be updated. That problem exists on both macOS and iOS.

The //d.update() in the loop is commented out, sorry. Did not want to actually make those changes in my test data. You should be able to make that change and it’ll be fine.

Thanks, didn’t notice that. Works great and I appreciate the help.