Shell string manipulation not working in Applescript

applescriptbash

I'm trying to make a service to translate things which Chrome doesn't translate automatically. The following script works when run from my shell

#!/bin/bash

input='это тест)))' # translates to - this is a test)))
if [[ $(echo "$input") =~ ')' ]]; then 
    input="$(echo "${input//)/}")"
fi
export PATH="/usr/local/bin:$PATH"; /usr/local/bin/trans -e yandex -b :en "$input" 

the result: this is a test

However when I put this into an Applescript:

set input to "это тест)))"
set output to (do shell script "if [[ $(echo " & quoted form of input & ") =~ ')' ]]; then " & quoted form of input & "=\"$(echo \"${" & quoted form of input & "//)/}\")\"; fi; export PATH=\"/usr/local/bin:$PATH\"; /usr/local/bin/trans -e google -b :en " & quoted form of input & "")

display dialog output as string

The result is still this is a test)))

It still runs and translates the input string. But it doesn't remove the ))). I don't understand why the if statement is not working in my applescript version. Am I quoting something wrong?

This script uses a program called Translate-Shell which can be found on GitHub or at the program author's website here.

Best Answer

It doesn't work because, the shell try to change a 'string', not to set a variable.

The command as example:

do shell script "if [[ $(echo " & quoted form of input & ") =~ ')' ]]; then " & quoted form of input & "=\"$(echo \"${" & quoted form of input & "//)/}\")\"; fi; "

After the concatenation (Look at the Events in the script editor), the command is:

do shell script "if [[ $(echo 'это тест)))') =~ ')' ]]; then 'это тест)))'=\"$(echo \"${'это тест)))'//)/}\")\"; fi; "


So, put the content of the AppleScript's variable into a variable in the shell (I remove the echo commands, because it's useless), like this:

set input to "это тест)))"
set output to do shell script "input=" & (quoted form of input) & "; if [[  \"$input\" =~ ')' ]]; then input=${input//)/}; fi; export PATH=\"/usr/local/bin:$PATH\"; /usr/local/bin/trans -e google -b :en \"$input\""
display dialog output