1

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.

2
  • Where did trans come from as it's not a native OS X app included in the OS X installation, so I'm unable to reproduce your command line for testing. Is there a native OS X binary available from somewhere or the source code so I can compile it directly as I don't use the typical package managers, e.g Homebrew? Commented Sep 7, 2016 at 15:21
  • My apologies. I completely overlooked that. trans is translate-shell which can be found on GitHub or at the authors own website here.
    – I0_ol
    Commented Sep 7, 2016 at 19:31

1 Answer 1

2

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .