I have a python 2.7.10 script that takes a user's input and inserts it into the name
and body
parameter of the make new note
AppleScript.
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//
or c:\test\test.txt
without giving me a Expected “"” but found unknown token.
error or ignoring the t
character after the \
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?
NOTE:
This is NOT a duplicate of this question because I've tried replacing the string variable header
and body
with eval(header)
and eval(body)
but that didn't work. I also tried .decode('string_escape')
to no avail. Finally, I tried this with no luck either:
d = header.escape('\w[0-9]')
header = d.decode('string_escape')
I think this has to do with AppleScript's ability to accept this string and not just Python's ability to sanitize it using the above functions.
UPDATE
I am capturing user input using a dialog box with code like this:
cmd = "osascript -e \'set theString to text returned of (display dialog \"Please Enter The Note To Add To Your iCloud Notes \" with icon file \"%s\" default answer \"\n\n\n\" buttons {\"OK\",\"Cancel\"} default button 1) \'" % (appicon_abs_path_apple)
note = run_script(cmd)