Selected Text Variable

Hi,
I am trying to create a touchbar button that when long pressed copies the text that I have selected to be the contents of a variable that I create and then on a regular press, I want to paste the collected value. This variable should continue to be that same value until I long press on selected text again.

I tried following the instructions for variables on the documentation and looking at the JavaScript/Apple Script options, but was not able to get it to work. I also tried doing the {} around a varaible in the hud display but it just shows literally {variable name} and not a value even if I think I correctly set a value.

I want it to set it to a variable I create because I would ideally create a few of these that have different values for each case I work and I just update the value for each one when I start a new case.

Please let me know if this is possible and how. Thanks!

Any idea on this @Andreas_Hegenberg ? Thanks

Or explain the dynamic variables if that would work?

This would need some scripting.

  1. A script that gets the selected text and concatenates it to some variable, in this example it is collected in the variable named selectedTextCollector1:
(async () => {

const variableName = 'selectedTextCollector1';
let selected = await callBTT('get_string_variable', {variableName: 'selected_text'});

let collectorVar = await callBTT('get_string_variable', {variableName: variableName});


await callBTT('set_string_variable', {variableName:  variableName, to: (collectorVar ? collectorVar : '') + selected});

returnToBTT(selectedTextCollector1);
})()

2.) An action that pastes the content of the variable. You can use the "Insert/Type/Paste Custom Text" action:

3.) A script to clear the contents of the variable:

(async () => {

const variableName = 'selectedTextCollector1';
await callBTT('set_string_variable', {variableName:  variableName, to: ''});
returnToBTT('cleared');
})()
1 Like

Thank you so much, that worked perfect. The clear was the part I was missing. I could get the variable to be made but it always did the first value I gave it. I put the clear code as an action followed by the get the text and set variable value, so that way it always clears it before I set the new value and don't need to do two triggers for it and I can just use the paste one until I need to change the value!

Thanks again!