How to Automatically Set Display Brightness to Specific % When An App Comes to Focus?

I would like to have the display brightness automatically change to 80% when an app comes to focus, and return the display brightness back to the previous value when that app loses focus.

Is this doable with BTT?

You can use two simple scripts for this and assign them to the "specific app did activate / deactivate" triggers in "Automations, Named & Other Triggers:

For Specific App Did Activate:

async function updateBrightness() {
	let currentBrightness = await get_number_variable("CurrentDisplayBrightness");		
	await set_number_variable({variable_name:'LastBrightness', to: currentBrightness});
	await set_number_variable({variable_name:'CurrentDisplayBrightness', to: 0.8})
	return currentBrightness;
}

For Specific App did Deactivate:

async function restoreSavedBrightness() {
	let previousBrightness = await get_number_variable("LastBrightness");
	await set_number_variable({variable_name:'CurrentDisplayBrightness', to: previousBrightness})
	return previousBrightness;
}

Here is an example preset doing this for whatsapp:
brightness.bttpreset (4.1 KB)

Thanks for this, but it doesn't seem to work?

I downloaded your preset file and changed the app to Messages.app, but when I brought the messages app to focus, the brightness didn't change. Is there something I need to change in the script on my end for it to work? Sorry, I don't know anything about programming!

Maybe add a "Show HUD OVerlay" action to see whether it actually triggers the action sequence. Possibly try to restart BTT once.

BTT does only support changing brightness for displays that support it through the standard macOS functions though.

BTT does only support changing brightness for displays that support it through the standard macOS functions though.

OK this must be the culprit, then. The HUD shows that the automation does get triggered. I'm using an external display that doesn't have a brightness level slider in the Display settings in System Settings. I guess it's a dead end; darn!

Still, thank you for the code! I hope it helps others.

1 Like

This works great, except when I quit the specified app. After I quit the app, the brightness isn't restored. Is there a way to handle this in BTT?

EDIT: I added another trigger for "Specific App Did Terminate" that runs the same action and it works great. Thanks!

1 Like