Widget: Temporarily Show Selected Text in Touch Bar

I made a simple widget that, if pressed, shows the currently selected text in the touch bar. If pressed again, it disappears.

Screenshot

How to use
You can either use the all-in-one version, which only consists of one widget, or the button-and-widget-version seen in the screenshot, which i find a little more useful (one button with only an icon and a widget for showing the text, which is activated by the button). Both are included in the preset download. Note that in order for the widget(s) to work I think you have to copy the UUID of the widget by right clicking on it and replace the UUID in the script.

Download
https://share.folivora.ai/sharedPreset/79f10f19-3394-4384-b8e1-da6d327993ff

Limitation
The widget uses the clipboard to get the selected text. It saves the current clipboard before that and restores it right afterwards. But afaik, this saving/restoring may not always work correctly, especially when you have a complex data type in your clipboard i guess. Please have that in mind and consider not using the widget when there is something important in your clipboard! Apparently there are more complicated ways to improve this, which i did not want to get into. If you want to read more about this problem, or want to improve this, you could start here: https://apple.stackexchange.com/questions/271161/how-to-get-the-selected-text-into-an-applescript-without-copying-the-text-to-th

Scripts (of the button-and-widget version)
Button: Get currently selected text and pass to widget:

tell application "BetterTouchTool"
	
	-- temporarily save the content of the clipboard because we are about to use the clipboard to get the selected text
	set savedClipboard to the clipboard -- the content of your clipboard may not always be saved correctly if it is e.g. a complex data type. be aware of that
	
	-- get selected text
	tell application "System Events" to keystroke "c" using {command down}
	delay 0.5 -- i read that this is necessary, you may try without
	set selectedText to the clipboard
	
	-- restore clipboard
	set the clipboard to savedClipboard
	
	-- remove line breaks etc from the text
	set AppleScript's text item delimiters to {return & linefeed, return, linefeed, character id 8233, character id 8232}
	set selectedText to text items of selectedText
	set AppleScript's text item delimiters to {" "}
	set selectedText to selectedText as text
	
	-- pass the text to the touch bar via BTT variable
	set_string_variable "selectedText" to selectedText
	refresh_widget "648FB9FD-18B0-4DF0-A79A-1F24773A9BD1"
	
end tell

Widget: Create return value:

tell application "BetterTouchTool"
	
	-- get text from BTT variable
	set selectedText to get_string_variable "selectedText"
	
	-- a "remove text from touch bar" means this widget was pressed, so the text should be removed. a missing value means BTT was just (re)started, so there is nothing to show
	if (selectedText is equal to "remove text from touch bar") or (selectedText is equal to missing value) then
		return "" -- return empty string so widget wont be visible
	else -- in this case there is text available that is supposed to show up
		
		-- optional: set a max character length for the text shown on the touch bar
		set maxSize to 200
		if length of selectedText is greater than maxSize then
			set selectedText to " " & text 1 thru (maxSize - 1) of selectedText & "…"
		end if
		
		-- return text
		return selectedText
		
	end if
end tell

Widget: When pressed:

tell application "BetterTouchTool"
	set_string_variable "selectedText" to "remove text from touch bar"
	refresh_widget "648FB9FD-18B0-4DF0-A79A-1F24773A9BD1"
end tell

Feel free to leave some feedback and let me know if something doesn't work!

Cool idea, but out of curiosity, what's your most common use-case of that widget for you if I may ask?

I use when i find some info I just want to keep in mind for a few minutes or so, or when I have to remember a number or something