Counter to change behavior for btt script

I want to create a script to change the color of text in a selected cell range in google sheets. Ideally I would press the same shortcut (cmd shift c) and the text color would change based on a global variable I increment when running the script.

I have figured out how to change the text color, but not how to create a variable and increment it. After some reading it seems like using AppleScript API might be the way to go, but help would be appreciated.

In case helpful, here’s what I think I need to do:

  • when keys pressed, check if variable (let’s say ‘text_color’ exists. If not, create it.
  • if it exists:
    *reset the value eg +=1
    *return a text string that corresponds to the value (eg 1 = blue, 2 = red, etc)
    *btt uses that text string to input/paste text.

Thanks so much!

I'd recommend to use the new "Real Java Script" option instead of raw apple script. It has easier syntax and you can add apple script when needed. This example increases a variable and shows an alert via apple script every 3. run:

async function variableStuff() {

// can be anything
let variableName = 'someVar';

// try to get the variable with the name specified above
let variable = parseInt(await callBTT('get_number_variable', {variable_name: variableName}));

// if it hasn't been set before it will be undefined
// in that case set it to 1
	if(variable === undefined) {
		await callBTT('set_number_variable', {variable_name: variableName, to: 1});
	}
	
	// do something based on the variable state
	if(variable == 1) {
		let appleScript = `
			set theDialogText to "The curent date and time is " & 	(current date) & "."
			set result to display dialog theDialogText
			return result
		`;

		let appleScriptResult = await runAppleScript(appleScript);
	} else if(variable == 2) {
	
	}  else if(variable % 3 == 0) { 
		// this is just for demo purposes to reset the variable to 0 after 3 runs
		await callBTT('set_number_variable', {variable_name: variableName, to: 0});
		variable = 0;
	}
	

	// increase the variable
	await callBTT('set_number_variable', {variable_name: variableName, to: variable+1});

    // return to BTT
	returnToBTT(variable);

}


variableStuff();

1 Like

Thanks so much! Sorry if I'm being dense, but how do I then use the variable in a BTT context? E.g., pass it to HUD, or insert as text? When I copy your script exactly and assign to a shortcut I'm not seeing the applescript alert, which makes me think I'm missing something basic in the set up.

Which version are you currently on? This java script stuff was just added earlier today

In the HUD you can use variables by wrapping them in curly braces {someVar}

version 3.332. I can now capture the variable, but it always returns NaN...

The 3.332 versions are good but you need to be even on the latest build of that ( 1530). Does BTT show an update if you check for updates?

Sorry I just noticed there was a little error in the script above. This one should work:

async function variableStuff() {

// can be anything
let variableName = 'anotherVariable';

// try to get the variable with the name specified above
let variable = await callBTT('get_number_variable', {variable_name: variableName});
// if it hasn't been set before it will be undefined
// in that case set it to 1
	if(variable === undefined) {
		await callBTT('set_number_variable', {variable_name: variableName, to: 1});
		variable = 1;
	}
	
	// do something based on the variable state
	if(variable == 1) {
		let appleScript = `
			set theDialogText to "The curent date and time is " & 	(current date) & "."
			set result to display dialog theDialogText
			return result
		`;

		let appleScriptResult = await runAppleScript(appleScript);
	} else if(variable == 2) {
	
	}  else if(variable % 3 == 0) { 
		// this is just for demo purposes to reset the variable to 0 after 3 runs
		await callBTT('set_number_variable', {variable_name: variableName, to: 0});
		variable = 0;
	}
	

	// increase the variable
	await callBTT('set_number_variable', {variable_name: variableName, to: variable+1});

    // return to BTT
	returnToBTT(variable);

}


variableStuff();