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.");
}
})();
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.");
}