Applescript osacompile fails if user input contains apostrophe / single quote 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 `&quot;'
sh: -c: line 1: syntax error: unexpected end of file

Here's the relevant code sction:
set savePath to (path to desktop as text) &amp; _appName &amp; &quot;.app&quot;

set code to &quot;osacompile -e '
tell application \&quot;Messages\&quot; 
    set imessageservice to 1st service whose service type = iMessage 
    set iMessageBuddy to buddy \&quot;&quot; &amp; _smsNumber &amp; &quot;\&quot; of imessageservice 
    send \&quot;&quot; &amp; _smsBody &amp; &quot;\&quot; to iMessageBuddy
end tell
tell application \&quot;Mail\&quot;
    set theMessage to make new outgoing message with properties {visible:true, subject:\&quot;&quot; &amp; _emailSubject &amp; &quot;\&quot;, content:\&quot;&quot; &amp; quoted form of _emailBody &amp; &quot;\&quot;} 
    tell theMessage 
        make new to recipient at end of to recipients with properties {address:\&quot;&quot; &amp; _emailAddress &amp; &quot;\&quot;} 
    end tell 
    send theMessage
end tell' -o &quot; &amp; 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.
If I was doing something such as this for myself, I would simply write the contents of &quot;code&quot; to a temporary text file then use osacompile to create the AppleScript application bundle and then delete the temporary text file. Doing it in this manner you should not have do deal with the single-quotes passed in a variable.
The following example AppleScript code is just a proof of concept. In Script Editor, just set an appropriate value for the smsNumber variable, for you, and then run the code.
It will create Foobar.app on your Desktop for you to then double-click and see that it sends you the message with the single-quotes in it.
set appName to &quot;Foobar&quot;
set smsNumber to &quot;&quot;
set smsBody to &quot;Here's my email&quot;

set tempAppleScriptCode to ¬
    &quot;/tmp/&quot; &amp; appName &amp; &quot;.applescript&quot;

set saveToPathFileName to POSIX path of ¬
    (path to desktop as text) &amp; appName &amp; &quot;.app&quot;

set myAppleScriptCode to &quot;tell application \&quot;Messages\&quot; 
    set imessageservice to 1st service whose service type = iMessage 
    set iMessageBuddy to buddy \&quot;&quot; &amp; smsNumber &amp; &quot;\&quot; of imessageservice 
    send \&quot;&quot; &amp; smsBody &amp; &quot;\&quot; to iMessageBuddy
end tell&quot;

set tempFileWasWritten to ¬
    (my writeTextToFile(myAppleScriptCode, tempAppleScriptCode, true))

tell application &quot;System Events&quot; to ¬
    set saveToPathFileNameExists to ¬
        (exists item saveToPathFileName)

set saveToPathFileName to saveToPathFileName's quoted form
set tempAppleScriptCode to tempAppleScriptCode's quoted form

if tempFileWasWritten then
    if not saveToPathFileNameExists then
        do shell script &quot;osacompile -o&quot; &amp; space &amp; saveToPathFileName &amp; ¬
            space &amp; tempAppleScriptCode &amp; &quot;;rm&quot; &amp; space &amp; tempAppleScriptCode
    else
        display dialog &quot;The file '&quot; &amp; saveToPathFileName &amp; ¬
            &quot;' already exists!&quot; buttons {&quot;OK&quot;} default button 1 ¬
            with title &quot;The target file already exists!&quot;
    end if
else
    display dialog &quot;An error occured writing to: '&quot; &amp; tempAppleScriptCode &amp; ¬
        &quot;'&quot; buttons {&quot;OK&quot;} default button 1 with title &quot;Error writing to file!&quot;
end if


on writeTextToFile(theText, theFile, overwriteExistingContent)
    try
        set theOpenedFile to open for access POSIX file theFile with write permission
        if overwriteExistingContent is true then set eof of theOpenedFile to 0
        write theText to theOpenedFile starting at eof
        close access theOpenedFile
        return true
    on error
        try
            close access file theFile
        end try
        return false
    end try
end writeTextToFile