How can I access the "selected_text" variable from Shell script?

There shouldn't be, does the slected text contain quotes? If so they would need to be escaped first, maybe something like this would work:

(async ()=> {

let selectedText = await callBTT('get_string_variable', {variable_name:'selected_text'})
let escapedSelectedText = JSON.stringify(selectedText).slice(1, -1);

// put the shell script into a string (single backticks are great for multiline strings)
let shellScript = `random_file="/Users/abc/throwaway/$(openssl rand -hex 8).json"; echo "${escapedSelectedText}" > "$random_file"; code "$random_file"; sleep 1; rm "$random_file"`;

let shellScriptWrapper = {
    script: shellScript, 
    environmentVariables: 'PATH=MY PATH (Cant share)' //optional e.g. PATH=/test/;VAR2=/test2/;

let result = await runShellScript(shellScriptWrapper);


It does contain quotes, Its a proper JSON request that I will be making to a service. I am not sure if I can share the request or the text , but let me try to anonymize it and share

Ah, then with the initial version it would cut off at the first quote because it would insert the whole selected text with the quote in the command (and the command itself expects a quote to end the "echo"

Did not get what you meant.
Its still not working, I suspect it has something to do with string itself, but I am not able to figure it out
Here is the request I am trying to select

The Problem is there is a user agent string in the request and that is causing some problems. I can take it from here. Thanks for your help :slight_smile:

"Dalvik/2.1.0 (Linux; U; Android 7.1.2; AFTMM Build/NS6287)"

This string is causing problems

Weird, I don't see anything that would need further escaping in this :-/

It looks like I need to escape the " in this string with \ , so basically convert it into

\"Dalvik/2.1.0 (Linux; U; Android 7.1.2; AFTMM Build/NS6287)\"

But I am not sure why only this particular field, since the request have various other strings enclosed within " "

The weird thing is, the let escapedSelectedText = JSON.stringify(selectedText).slice(1, -1);
should already do this

My bad , I tried the stringify approach again and it worked, looks like the first time I did something wrong when trying this :frowning: Soo sorry for your time.

Ah great! I'll add an example like this to the docs :slight_smile:

1 Like

I did it in a simpler way....

In AppleScript

	tell application "BetterTouchTool"
		set_string_variable "SavedTextAlert" to MyTitle
	end tell

In Shell ( zsh by macos )

osascript <<EOD

	tell application "BetterTouchTool"
        		Set MyTitle to get_string_variable "SavedTextAlert"
	end tell
	tell application "reminders"
		display notification MyTitle with title "Lembrete Criado" sound name "Frog"
	end Tell