Simple Match and Replace Script

I need to match a date and then replace all instances of dated, with the matched date. I wrote a simple script and I just can’t figure out why it is not replacing anything.

Sample text:

C-CAT (FO), dated March 5, 2024.

Testing dated, was found.

dated, 

Here is the dated, 

Javascript

    // Regular expression to match the pattern "C-CAT (FO), dated March X, YYYY"
    // It captures the entire date string after "dated"
    const pattern = /C-CAT \(FO\), dated (\w+ \d+, \d+)/;
    
    // Try to match the pattern in the draft content
    const match = draft.content.match(pattern);
    
    if (match && match[1]) {
        // Extract the date from the match
        const date = match[1];
        
        // Replace all instances of "dated," with "dated [extracted date],"
        
        draft.content = draft.content.replace(/dated,/g, `dated ${date},`);
        draft.update();
        
        // Provide feedback
        alert("All instances of 'dated,' have been updated with the dynamic date: " + date);
    } else {
        // If the pattern is not found, provide feedback
        alert("The specific pattern 'C-CAT (FO), dated ...' was not found.");
    }
})();

What are the extra brackets for on the last line?
})();

Unless this is an extract from a larger script, removing that seems to work.

I cleaned it up. Pleaese take a look. What am I doing wrong.

Screen recording.

It’s usually easier if you share the action itself so we can install and test comparing apples to apples.

This example is your exact script as above, with just the line @jsamlarose suggested removing taken off the end, and it seems to work fine:

I tried that. It does not work. I created a new action and pasted your code. Same outcome. I restarred Drafts and Mac. Same problem.

Please take a look at this recording.

Using the action shared above worked perfectly for me against your original example.

What happens if you try that action rather than your own modification?

If it works, do a text comparison. I couldn’t see anything off in the video, but I was scanning it on a small screen.

This is so strange. It works on iOS but not on Mac. I reinstalled Drafts on Mac and still it does nothing. The same text and action work as expected on iOS.

Edit: I installed Drafts on a new MacBook Air and get the same result. Text is not being replaced.

@sylumer Can you please confirm that this script works for you on MacOS?

I am seeing an issue on macOS (using the latest beta). I believe the issue can be demonstrated using the code/action below. In my testing, the updated text (to “foo”) is retained on iOS and iPadOS, but on macOS, the text is not retained.

alert("# Current draft content #\n" + draft.content);
// Original content of draft is shown

draft.content = "foo";
alert("# Modified draft content pre-update #\n" + draft.content);
// New content of draft is shown

draft.update();
alert("# Modified draft content post-update #\n" + draft.content);
// Draft loses new content and original content of draft is shown on macOS
// Draft show new content of draft when run on iOS

Dropping the explicit update instruction and relying on an implicit update at the end of the action also does not seem to make any difference for me.

I am running TestFlight builds on my devices.

Drafts Version: 426
OS Version: macOS Version 14.1.1 (Build 23B81)

@agiletortoise, are you able to reproduce this on a macOS build?

Interesting. Something about the alert seems to be interfering with the update. If you comment out the confirmation alert line it works fine.

I’ll have to look into that in more detail, but in the meantime, just comment out the alert after the replace…or, I would suggest it is better to write a script like this meant to manipulate the text in the editor using editor functions, which directly work with the editor and will also allow you to undo your operations if needed.

In this case that would look like:

// Regular expression to match the pattern "C-CAT (FO), dated March X, YYYY"
// It captures the entire date string after "dated"
const pattern = /C-CAT \(FO\), dated (\w+ \d+, \d+)/;
    
// Try to match the pattern in the draft content
let text = editor.text()
const match = text.match(pattern);

if (match && match[1]) {
    // Extract the date from the match
    const date = match[1];
    
    // Replace all instances of "dated," with "dated [extracted date],"
    
    editor.setText(text.replace(/dated,/g, `dated ${date},`))        
} else {
    // If the pattern is not found, provide feedback
    alert("The specific pattern 'C-CAT (FO), dated ...' was not found.");
} 

Commenting out the alert fixes the problem. Thank you.

I hope you can find the bug for the next update.

I tried your script with the editor but I get an error:

Script Error: TypeError: editor.text is not a function. (In ‘editor.text()’, ‘editor.text’ is undefined)
Line number: 6, Column 23

Edit: Found the fix. I had to change let text = editor.text() to let text = editor.getText()