What I’m trying to do
I’m trying to make a simple “smart paste” script - I have a templater script that I trigger from QuickAdd (capture macro that runs {{TEMPLATE:This Template}} that reads and parses my clipboard with some simple logic. I’m quite new to JavaScript.
A simplified version of the code is below:
<%*
clipboard = (await tp.system.clipboard());
if (clipboard.includes(Criteria 1)){
tR += clipboard.replace('some text', 'some other text');
} else if (clipboard.includes(Criteria 2)) {
tR += 'prefix' + clipboard;
} else if (clipboard.includes('**Criteria 3**')){
title = (await tp.system.prompt('Title?'));
tR += '[' + title + '](' + clipboard + ')';
} else {
tR += '';
} -%>
I’m having a ton of issues getting the third section (Criteria 3) working. I suspect this is some asynchronous race condition, but I can’t figure out how to fix it. When I run the above, one of two things happens:
- If I quickly type something in to the prompt that appears and hit enter, another prompt will appear immediately. If I then type in a title to the second prompt that appears, the code executes as expected and
[TEXT I TYPED IN THE SECOND PROMPT](clipboard)
appears in my note - If I wait a few seconds and type something in to the prompt that appears, QuickAdd throws an error saying that the file has been modified since I last read it, implying that the script ran twice (consistent with 1).
Either way, for some reason it appears my script is running twice, but this is only happening when I add the second await
, if I remove that line or replace it with title = 'some placeholder'
the code runs just fine.