I have a python script that takes a user’s input and inserts it into the name
and body
parameter of the make new note
apple script. The problem is that any escape or special characters get interpreted by applescript. I want all strings to be treated as raw and to ignore all escape characters and things like :\//tft//
without giving me a Expected “"” but found unknown token.
error
The line of python looks like this:
cmd = "osascript -e 'tell application \"Notes\" \n tell account \"iCloud\" \n make new note at folder \"Notes\" with properties {name:\"%s\", body:\"%s\"} \n end tell \n end tell'" % (header, body)
… where header
and body
are the user supplied strings.
But for manual testing I use the Script Editor to quickly reproduce the error.
tell application "Notes" tell account "iCloud" make new note at folder "Notes" with properties {name:"myname", body:"c:\test\test.txt"} end tell end tell
This particular note ends up looking like this:
I know that I can use \
instead of \
but I don’t want users to have to sanitize all their input as they could be copy pasting from a large body of text. Think of a log file or paste-bin style body of text.
Is there a way to programmatically sanitize the user’s input?