
I have created a script which takes some user input from me (prompts with basic dialogs), then generates a standalone script that sends me a text message and an email, which I can then schedule as a calendar alert to trigger at a desired date/time.
Everything works fine, until there is an apostrophe/single quote in the user input. For example if my email body is:
Here's my email
the script will fail to run, with:
sh: -c: line 0: unexpected EOF while looking for matching `"'
sh: -c: line 1: syntax error: unexpected end of file
Here's the relevant code sction:
set savePath to (path to desktop as text) & _appName & ".app"
set code to "osacompile -e '
tell application "Messages"
set imessageservice to 1st service whose service type = iMessage
set iMessageBuddy to buddy "" & _smsNumber & "" of imessageservice
send "" & _smsBody & "" to iMessageBuddy
end tell
tell application "Mail"
set theMessage to make new outgoing message with properties {visible:true, subject:"" & _emailSubject & "", content:"" & quoted form of _emailBody & ""}
tell theMessage
make new to recipient at end of to recipients with properties {address:"" & _emailAddress & ""}
end tell
send theMessage
end tell' -o " & quoted form of POSIX path of savePath
do shell script code
Again it works great as long as I don't pass in an apostrophe. In my searching, it appears that escaping apostrophes in script is really, really tricky to get right. I've tried numerous approaches (heredoc, multiple -e lines, quoted form of, etc.) but so far I haven't been able to get any to work. If anyone knows how to get around this I'd love to hear it.