I've created a Text Selection Did Change trigger in the Automation section and I set it to Run Real JavaScript. The code is:
//if you rename this, make sure to also rename it in the "function to call" field below.
async function copyCaseNumber() {
let caseNumber = await get_string_variable({variable_name:'selected_text'})
returnToBTT(caseNumber);
}
I then created a HUD display as the second action that is supposed to display the contents of the copyCaseNumber variable, but it is not showing. I tried all the following items in the HUD Display text area:
(BTT)@variable:caseNumber(BTT)
{caseNumber}
(BTT){caseNumber}(BTT)
(BTT)@caseNumber(BTT)
(BTT)caseNumber(BTT)
They all just show the actual text I put in the HUD display area, not the value I wanted to set.
Any help would be greatly appreciated!
P.S. I also tried to do the set/Assign Value to Variable action, but I don't think you can set the variable to have a value of "selected text". If you can, please let me know how as I was unable to get that to work.
you JavaScript is just reading the selected_text variable, but it's not setting the case number variable.
You'd need to add
await set_string_variable({variableName: "caseNumber", to: caseNumber})
You could also directly use {selected_text} or better {BTTTextSelection} in your HUD
So I want to be able to use the variable's value later which is why I have it setting it to caseNumber. I am going to set an touchbar button to paste that value, but it's only going to get the value if the selected text starts with 00 which are all certain ids at my work. But it doesn't seem to be working. I have it set like this for the javascript
//if you rename this, make sure to also rename it in the "function to call" field below.
async function copyCaseNumber() {
let caseNumber = await get_string_variable({variable_name:'selected_text'})
await set_string_variable({variableName: "caseNumber", to: caseNumber})
returnToBTT(caseNumber);
}
Is that correct?
better use BTTTextSelection instead of selected_text with the did change trigger. selected_text is old and has been replaced by this. I'll soon map it to be the same but this is not yet done.
casenumber.bttpreset (3.4 KB)
So if I use that Preset and have the trigger conditions for it set to:
Shouldn't it be copying this selected text I have in a note?
Thank you for all your help!
I would do the testing for the 0039 directly in your java script - without an advanced trigger condition.
You mean using javascript with a regex code instead of BTTTextSelection?
Have a look at this:
casenumber_exported_triggers.bttpreset (7.0 KB)
function getFirst0039Number(inputString) {
// This regular expression looks for "0039" followed by one or more digits.
// \b is a word boundary, ensuring we capture a discrete number and not part of another string of characters.
// test number: 003923241
const regex = /\b0039\d+\b/;
const match = inputString.match(regex);
return match ? match[0] : null; // If a match is found, return it; otherwise, return null.
}
async function copyCaseNumber() {
let selectedText = await get_string_variable({variable_name:'BTTTextSelection'});
let caseNumber = getFirst0039Number(selectedText);
if(caseNumber != null) {
await set_number_variable({variableName: "caseNumber", to: parseInt(caseNumber)})
} else {
await set_number_variable({variableName: "caseNumber", to: -1})
}
return caseNumber;
}
It will show the first matching number it finds
IGNORE:
So that allows me to see the value in the hud, but when I then try to access the caseNumber value using past text, i am getting -1 and it puts the cursor between the - and the 1. Any idea why?
The -1 only appears sometimes, it works most of the time I found!
Thanks so much!!
it sets the variable to -1 if the last selected text didn’t contain the number, if you want to keep the last recognized number instead you can remove the line where it sets the -1 value
1 Like
So I used the same code to make another trigger for a different regex, but the variable that I named it to is just returning btt-undefined as the value no matter what I copy. It shows that it is finding the correct value when I highlight it because the hud is getting returned but the value is just "btt-undefined". Any idea why?
I found that using BTTTextSelection works for advanced conditions instead of selected_text though, which is good to differentiate the two highlights.
for strings you need to use "set_string_variable" instead of "set_number_variable"
Got it, duh, thank you! sorry about that!
So now it's just showing {caseEmail}. Sorry! I updated to:
that would mean it wasnt able to find a matching email in your text. could you attach the trigger here? (right click, export to file)
your regex is missing the @. This one should work:
const regex = /^[a-zA-Z0-9._%+-]+@walkme\.com$/
Dang it, and here I thought i had set it up so well. Thank you! That fixed it, it's working now! Appreciate the help!
1 Like
Just FYI: chatgpt is great at telling you why a regex won't work
(I also always get them wrong)
Oh that's a great idea, thank you!