Is trying to use sed to execute Regex the best way of going about doing text cleanup?
What is the best way to get the script to use the selected text in the frontmost window? I’m just not able to get a clear picture on how to achieve this.
tell application "System Events"
set frontApp to name of first application process whose frontmost is true
tell application frontApp
set selectedText to value of text area 1 of scroll area 1 of front window as string
-- Replace ". " with ". "
set selectedText to do shell script "echo " & quoted form of selectedText & " | sed 's/\\. /\\. /g'"
-- Replace " " with " "
set selectedText to do shell script "echo " & quoted form of selectedText & " | sed 's/ / /g'"
-- Replace " " with " "
set selectedText to do shell script "echo " & quoted form of selectedText & " | sed 's/ / /g'"
-- Replace ", " with ", "
set selectedText to do shell script "echo " & quoted form of selectedText & " | sed 's/, /, /g'"
-- Replace ":" without a space after with ": "
set selectedText to do shell script "echo " & quoted form of selectedText & " | sed 's/:\([^[:space:]]\)/: \\1/g'"
-- Replace ";" without a space after with "; "
set selectedText to do shell script "echo " & quoted form of selectedText & " | sed 's/;\([^[:space:]]\)/; \\1/g'"
-- Replace curly quotes with straight quotes
set selectedText to do shell script "echo " & quoted form of selectedText & " | sed 's/[“”‘’]/\"/g'"
-- Replace multiple carriage returns with one carriage return
set selectedText to do shell script "echo " & quoted form of selectedText & " | awk 'BEGIN{RS=\"\";FS=\"\\n\"}{for(i=1;i<=NF;i++)if($i)printf(\"%s%s\",$i,i==NF?\"\":\"\\n\")}'"
-- Replace repeated words with one occurrence of the word
set selectedText to do shell script "echo " & quoted form of selectedText & " | sed -E 's/(\\b\\w+\\b)(\\s+\\1)+/\\1/g'"
-- Capitalize the first letter after a period and lowercase the rest
set selectedText to do shell script "echo " & quoted form of selectedText & " | sed -E 's/\\b([a-z])|\\.\\s+(.)/\\U\\1\\L\\2/g'"
-- Update the text in the frontmost window with the modified text
set value of text area 1 of scroll area 1 of front window to selectedText
end tell
end tell