0
#/bin/bash
corr="apple"
echo $corr

osascript -e 'tell application "Messages" to send  "Did you mean "'"$corr"'" "  to buddy  "A"'

Error:

51:57: syntax error: A identifier can’t go after this “"”. (-2740)

If I just pass

osascript -e 'tell application "Messages" to send  "Did you mean $corr "  to buddy  "A"'

the message comes like "Did you mean $corr"

I have tried everything mentioned at Pass in variable from shell script to applescript

Community
  • 1
  • 1
shalder
  • 1
  • 1
  • formatting was horrendous. should be more readable now – Christian Fritz May 22 '15 at 15:45
  • If you are trying to send the literal string `Did you mean "apple"`, then you aren't escaping the quotation marks around `apple`. The first one closes the message, so that `apple` is an unexpected identifier, not part of the message string. – chepner May 22 '15 at 15:58

1 Answers1

1

There is a very good explanation with decent examples at the very end of here.

Based on that, I was able to make this work (using TextEdit, not Messages):

 #!/bin/sh
rightNow=$(date +"%m_%d%B%Y")
osascript -e 'on run {rightNow}' -e 'tell application "TextEdit" to make new document with properties{name: "Date003.txt", text:rightNow}'  -e 'end run' $rightNow

I also found that I had to be VERY careful about the single and double quotes. For some reason, my keyboard kept replacing the plain quotes with curled quotes. Annoying.

Community
  • 1
  • 1
Craig Smith
  • 1,063
  • 2
  • 11
  • 21
  • One can disable that option (replacing quotes) in TextEdit's preferences –  May 22 '15 at 23:05
  • @Zero Thank you! I rarely use TextEdit for shell scripts, but I thought this would be simple. Now it is fixed. – Craig Smith May 22 '15 at 23:45
  • While not essential in this particular example, I would also strongly recommend double-quoting `$rightNow` at the end. It's a _very_ good habit to get into when writing bash scripts as it ensures correct behavior should the string being inserted contain any spaces. (Bash is such a POS it can't even do variable substitution right - instead it just inserts the raw string as-is and parses it as code. e.g. If the string is `foo bar` then `$rightNow` will be replaced by _two_ separate arguments, `foo` and `bar`, unless you explicitly wrap the variable name in quotes: `"$rightNow"` → `"foo bar"`.) – foo May 24 '15 at 13:59