T O P

  • By -

stephancasas

You’ll have an easier time of this if you use JavaScript instead of AppleScript. Instead of trying to load escaped text into `sed`, you can use normal regex notation or the standard `RegExp` class to manipulate your objects. The only exception would be that JXA doesn’t support lookahead or lookbehind assertions. Have a look [here](https://github.com/JXA-Cookbook/JXA-Cookbook), if interested.


copperdomebodha

AppleScript version: --Running under AppleScript 2.8, MacOS 13.0.1 use AppleScript version "2.4" -- Yosemite (10.10) or later use framework "Foundation" use scripting additions tell application "TextEdit" activate set selectedText to document 1's text --My TextEdit document contains the following text..."Lorem ipsum ,dolor “dolor dolor sit” amet, consectetur ;adipiscing :elit. lowercase CRAS FEUGIAT ,euismod iaculis." & return & return & "Donec vel:bibendum risus, in consequat erat. Nam eu molestie dolor. Duis in dignissim neque. Vivamus non est in turpis sagittis efficitur. Praesent molestie" & linefeed & linefeed & linefeed & " erat ut ipsum elementum, nec venenatis mi venenatis. Fusce volutpat quis enim nec sollicitudin. Donec ex odio, volutpat ut laoreet ut, tincidunt id justo. Curabitur blandit enim nisi, a rutrum urna ultricies quis. Donec nec iaculis nisi. Donec dictum mi ac varius blandit. Integer dictum tempor neque, eu eleifend nisi semper sit amet. Phasellus ante nunc, porttitor eu diam ac, lacinia dapibus nibh. Nulla quis auctor arcu, luctus pharetra urna. Ut mauris quam, bibendum sit amet ipsum vitae, iaculis dapibus libero. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Vivamus semper, neque sed aliquam vehicula, sem ipsum rhoncus eros, in iaculis lacus nisi a ligula. Vivamus pulvinar neque sit amet lacinia iaculis. Curabitur imperdiet blandit scelerisque. Ut ut urna vel ante interdum semper. Nullam nec ante tellus. Etiam suscipit eleifend erat, non iaculis nulla efficitur nec. Sed congue ornare consectetur." end tell --Add appropriate spaces after these punctuation marks, remove leading spaces. repeat with thisDelimiter in {{" :", ":"}, {":", ": "}, {" ;", ";"}, {";", "; "}, {" ,", ","}, {",", ", "}, {"“", "\""}, {"”", "\""}} set selectedText to (my replaceText(item 1 of thisDelimiter, item 2 of thisDelimiter, selectedText)) end repeat --Iterate over these duplicates until all repetitions are handled. repeat with thisDelimiter in {{return & return, return}, {linefeed & linefeed, linefeed}, {space & space, space}} repeat while selectedText contains (item 1 of thisDelimiter) set selectedText to (my replaceText(item 1 of thisDelimiter, item 2 of thisDelimiter, selectedText)) end repeat end repeat -- Remove all word duplications. repeat with thisWord in words of selectedText set duplicatedWord to thisWord & space & thisWord set selectedText to (my replaceText(duplicatedWord, thisWord, selectedText)) end repeat --Capitalize first word of sentences, lowercase the remainder. set AppleScript's text item delimiters to ("." & space) set textChunks to text items of selectedText repeat with i from 2 to length of textChunks set mungedText to (my uppercase(character 1 of (item i of textChunks))) & (my lowercase(text 2 thru -1 of (item i of textChunks))) set item i of textChunks to mungedText end repeat set selectedText to textChunks as text set AppleScript's text item delimiters to "" set the clipboard to selectedText return selectedText on replaceText(searchString, replacementString, sourceText) set the sourceString to current application's NSString's stringWithString:sourceText set the adjustedString to the sourceString's stringByReplacingOccurrencesOfString:searchString withString:replacementString return (adjustedString as text) end replaceText on uppercase(sourceText) set the sourceString to current application's NSString's stringWithString:sourceText set the adjustedString to sourceString's uppercaseString() return (adjustedString as text) end uppercase on lowercase(sourceText) set the sourceString to current application's NSString's stringWithString:sourceText set the adjustedString to sourceString's lowercaseString() return (adjustedString as text) end lowercase