I am writing applescripts using ‘sed’ to perform batch text manipulations on the note fields of my contacts.
For instance, :
tell application "Contacts" repeat with the_person in people tell the_person set cmd to "echo \"" & note & "\" | sed 's_abc_ABC_g'" set new_note to do shell script cmd set note to new_note save end tell end repeat end tell
While this works well enough for most contacts, it does not for contacts with double quotes in their note field. At best, the new note will be stripped of its double quotes (which I do not want) and at worst the messed up cmd causes an error in the shell script.
My solution is to replace all double quotes with simple quotes:
tell application "Contacts" repeat with the_person in people tell the_person repeat 1 times if note is missing value then exit repeat end if tell me to set text item delimiters to ASCII character 34 set the_note to note set the_items to text items of the_note tell me to set text item delimiters to ASCII character 39 set the_note to the_items as string set note to the_note save end repeat end tell end repeat end tell
However, this approach is very slow (35’ for <1500 contacts). I’d rather use a terminal command, but I don’t know how to do it. My question is: when using commands such as:
do shell script “echo "” & SomeText & “" | SomeTerminalCommand”
how to perform stream text edits when the input text might or might not contain any number (including odd) of double quotes. My goal is to improve my general skill and understanding, as my initial problem is solved.
Thank you for your attention, and thank you in advance for your replies.