0
#!/bin/bash

printf 'Starting Text Script... \nWhat number would you like to text?\n'
read number

msg="When..."
/usr/bin/osascript - "$@" << END
tell application "Messages" to send "$msg" to buddy "$number" 
END
sleep .1

msg="Cause..."
/usr/bin/osascript - "$@" << END
tell application "Messages" to send "$msg" to buddy "$number" 
END                         
sleep .1

msg="Laughing..."
/usr/bin/osascript - "$@" << END
tell application "Messages" to send "$msg" to buddy "$number" 
END
sleep .1

msg="Like..."
/usr/bin/osascript - "$@" << END
tell application "Messages" to send "$msg" to buddy "$number" 
END
sleep .1

this is the beginning of a script that sends a series of messages to an inputted number (just a script for fun to mess with a friend a bit). For some reason, the 1st message and every message at and after the 4th message; but the 2nd and 3rd message do not send and I get the listed error. I am confused since they are all written the same and only those two fail. I don't have much experience with apple scripts, so I am sure my error is somewhere in there.

I initially had the second value of msg set to "'Cause...", but I thought that's what was causing my error so I removed it. I am also thinking about having the script take input of a number and filepath instead and then reading each line of the file as a separate msg to send, but I am not positive how to achieve that. Any and all help is greatly appreciated!

Cyrus
  • 84,225
  • 14
  • 89
  • 153
  • towards the end of this [discussion of the here document](https://tldp.org/LDP/abs/html/here-docs.html) it mentions - *`Trailing whitespace after the [closing] limit string likewise causes unexpected behavior.`*; the sample code you've provided has a series of spaces after the closing limit string (`END` in this case) of the 2nd block; consider reviewing your code and removing all trailing white space after the closing `END` – markp-fuso Nov 12 '22 at 20:51
  • Aside from the whitespace typo found by markp, that is the [wrong, unsafe way](https://bobby-tables.com/) to parameterize an AppleScript. Doing it the wrong way leaves your code open to syntax errors, incorrect behaviors, malicious hacks, etc. The correct, safe way is to pass `$msg` and `$number` [as arguments to its `run` handler](https://stackoverflow.com/questions/73365822/run-2-commands-in-applescript-osascript-with-variable/73369467#73369467). – has Nov 14 '22 at 08:38

1 Answers1

0

There was trailing white space on the closing on line 15 of the script. Not sure how it got there haha.