How to get osascript to parse backslashes and run this command in a new Terminal window

applescriptbashterminal

Here's a command that works fine when I paste it into the Mac Terminal window:

echo -n -e \\033]0\;My Custom Window Title Here\\007 ; ping www.google.com

In particular, it sets the Terminal's title to include the string "My Custom Window Title Here", then starts pinging www.google.com.

Now what I want to do is have the above command run in a separate Terminal window instead, so I enter this:

osascript -e 'tell app "Terminal" to do shell script "echo -n -e \\033]0\;My Custom Window Title Here\\007 ; ping www.google.com"'

… but all I get is this error:

dyld: DYLD_ environment variables being ignored because main executable
(/usr/bin/osascript) is code signed with entitlements 59:60: syntax error: Expected “"” but found unknown token. (-2741)

If I get rid of the backslashes, osascript will run the command, but then of course the custom window title doesn't get set.

Is there some way I can get my bash command through osascript's parser intact, so that I can have my ping command execute in its own window AND with a customized window title?

Best Answer

Because it's going through another layer of interpretation, you need to escape the escapes -- essentially, you need to double the backslashes:

osascript -e 'tell app "Terminal" to do script "echo -n -e \\\\033]0\\;My Custom Window Title Here\\\\007 ; ping www.google.com"'