0

I have this bash function that runs some Applescript. If I run the Applescript part in the Applescript Editor or in Textmate, it works fine, but on the command line, the function fails...

wtf() {
  osascript - <<EOF
  tell application "iTerm"
      tell current terminal
          launch session "Railscasts"
          tell the last session
              write text 'echo -ne "\\e]1;$account\\a"'
          end tell
      end tell
  end tell
EOF
}

And the error is:

190:191: syntax error: Expected expression but found unknown token. (-2741)

I know (think) that the problem is with the first bash escape sequence on this line:

write text 'echo -ne "\\e]1;$account\\a"'
                      ^

But I don't know why it fails... Any ideas on why this doesn't work please?

EDIT 1: I also tried this and it failed:

wtf() {
  osascript - <<EOF
  tell application "iTerm"
      tell current terminal
          launch session "Railscasts"
          tell the last session
              write text "echo -ne \\\"\\e]1;$account\\a\\\""
          end tell
      end tell
  end tell
EOF
}

Error message:

163:164: syntax error: Expected end of line but found unknown token. (-2741)

2 Answers 2

3

AppleScript doesn't support '…' for a string literal. You must use "…", and escape the inner pair of "…" with \.

Also, it looks like shells reduce backslash sequences in heredocs (!!!), so you need to add more backslashes on the existing backslashes:

"echo -ne \"\\\\e]1;$account\\\\a\""
Sign up to request clarification or add additional context in comments.

4 Comments

I tried your solution, it failed with a different error message...Please see my edit.
I think "echo -ne \"\\e]1;$account\\a\"" is what you want.
@chepner I also tried that, but I get yet again a different error: 163:164: syntax error: Expected “"” but found unknown token. (-2741)
@AzizLight: chepner's version is what I'd intended. There's some non-obvious behavior at work here (thanks, shells!), so I've posted the finished string literal in my answer.
1

You could also replace <<EOF with <<'EOF':

<<'END'
\$` are not interpreted
END

<<END
\$` are interpreted
END

Or just use single quotes:

osascript -e 'tell application "iTerm"
    --
end tell'

X=999 osascript -e 'on run argv
    item 1 of argv + system attribute "X"
end run' 888

3 Comments

What's the significance of 888 at the end of the second example?
I just used 888 and 999 as examples on how to pass values to the script by adding an explicit run handler or by using environment variables.
Oh, I see—so when that script runs, argv will be {888} (or maybe {"888"}).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.