It likely is possible but we are discussing some date stuff internally. If @chrillek’s works, I’d suggest using it.
Thank you Bluefrog. Please do update me with your findings once you have them. I’ll try test @chrillek 's suggestion later this week and report back
No problem
Hi Chrillek
I have finally found some time to sit down and try out your Javascript, but I have hit a hurdle right out the gate. Please see attached image:
I’d be grateful for your help
There were some parenthesis missing, unfortunately (sorry, @Blanc). I’ve amended the script in the original post.
Thank you, that worked a treat!
Is there a way I can automate it to run on a selection rather than individually?
@chrillek My bad! I’ve just figured it out! Instead, could I ask for some help now turning this into a smart rule?
This has been described in other posts already. Search for “performsmartrule”, for example. And check the documentation’s section on Automation – it’s all explained there.
Thanks Chrillek. Okay, so I found your previous post (JavaScript: Using a script stand-alone and in a smart rule](JavaScript: Using a script stand-alone and in a smart rule) and I attempted to replicate, but I’m getting an error.
I’d be grateful if you could help steer me toward a solution.
Thank you for your time and effort, I really do appreciate your help as not very versed in scripting and automation.
I’d wager you’re missing two } -
So I think the line with the single } needs to be followed by another two lines each with a }
Also I suspect you don’t want the first (() => { nor the second and third lines in your script (const app and records.) as you are using those commands twice within the script.
Then again I haven’t got a clue But I’m pretty sure each open parenthesis needs to be closed somewhere along the way
Hi Blanc, thank you for jumping in
I’ve attempted to follow your suggestions (I probably haven’t interpreted correctly) and this is what I get with an error on line 23.
function performsmartrule(records) {
(() => {
const app = Application("DEVONthink 3");
const records = app.selectedRecords();
const dateRE = /(\d{4}-\d\d-\d\d)/; /* Regular expression for the date */
records.forEach(r => {
const match = r.plainText().match(dateRE);
if (match) {
/* There was a date found (possibly) */
const oldDate = r.creationDate();
let newDate = new Date(match[1]);
/* Set the new date's time to the old ones */
newDate.setHours(oldDate.getHours());
newDate.setMinutes(oldDate.getMinutes());
newDate.setSeconds(oldDate.getSeconds());
/* set DT creationDate to the new date + old time */
r.creationDate = newDate;
}
performsmartrule(Application("DEVONthink 3").selectedRecords())
}
I’m hopelessly lost
Sorry, I haven’t explained very well - I’ve now moved from iPad to iMac, so here goes. What I meant was:
function performsmartrule(records) {
const app = Application("DEVONthink 3");
const dateRE = /(\d{4}-\d\d-\d\d)/; /* Regular expression for the date */
records.forEach(r => {
const match = r.plainText().match(dateRE);
if (match) {
/* There was a date found (possibly) */
const oldDate = r.creationDate();
let newDate = new Date(match[1]);
/* Set the new date's time to the old ones */
newDate.setHours(oldDate.getHours());
newDate.setMinutes(oldDate.getMinutes());
newDate.setSeconds(oldDate.getSeconds());
/* set DT creationDate to the new date + old time */
r.creationDate = newDate;
} /* this one closes the if condition */
}) /* this one closes the repeat cycle */
} /* this one closes the function */
(() => {
performsmartrule(Application("DEVONthink 3").selectedRecords())
})()
(give me two minutes after posting, I’m just going to read through it again)
EDIT: missing a ) on the } closing the repeat cycle
EDIT 2: removed dual selection of records
I see, you’re getting the hang of it. Be careful not to develop an addiction to parenthesis!
I’m still hitting an error?
correct; delete that line, please - the records are selected by the smart rule (line one).
Please excuse that we are doing this by trial and error; I have never written let alone debugged a JS to date (which is why @chrillek & I have this running gag on parenthesis on this forum - I’m reasonable at AppleScript and find JS pretty inscrutable - by I’ve endeavoured to try and get a grasp; chrillek, in contrast, is the resident JS genius).
Bingo! Thank you Blanc
I’ll try running the smart rule now
Do it on copies of your data, please!
Will do thanks again for your help