The following text item delimiter function replaces linefeeds , carriage returns, or similar combinations in a SourceString with a space.
set SourceString to "Around " & linefeed & "the world" & return & "from East" & linefeed & return & "to West" & character id 8233 & "from South" & character id 8232 & "to North"
my ReplaceLineFeedReturnsInString:SourceString
on ReplaceLineFeedReturnsInString:SourceString
set AppleScript's text item delimiters to {return & linefeed, return, linefeed, character id 8233, character id 8232}
set SourceList to text items of SourceString
set AppleScript's text item delimiters to {space}
set RefinedSourceString to SourceList as text
set AppleScript's text item delimiters to ""
RefinedSourceString
end ReplaceLineFeedReturnsInString:
I would like to abbreviate the function, using the Foundation Framework in Objective C, but cannot find a method that will accept a list of strings such as:
and replace those occurrences with:
The Objective C method stringByReplacingOccurrencesOfString will replace a single string, in this case, return, but not a list of strings.
use AppleScript version "2.5" -- macOS 10.11 or later
use framework "Foundation"
use scripting additions
set SourceString to "Around " & linefeed & "the world" & return & "from East" & linefeed & return & "to West" & character id 8233 & "from South" & character id 8232 & "to North"
my ReplaceLineFeedReturnsInString:SourceString
on ReplaceLineFeedReturnsInString:SourceString
set theNSString to current application's NSString's stringWithString:SourceString
set RefinedSourceString to (theNSString's stringByReplacingOccurrencesOfString:return withString:space) as text
end ReplaceLineFeedReturnsInString:
What Objective C method might replace a list of strings with a single string?
What shell script might accomplish this replacement, should no Objective C method exist?
use AppleScript version "2.5" -- macOS 10.11 or later
use framework "Foundation"
use scripting additions
set SourceString to "Around " & linefeed & "the world" & return & "from East" & linefeed & return & "to West" & character id 8233 & "from South" & character id 8232 & "to North"
my ReplaceLineFeedReturnsInString:SourceString
on ReplaceLineFeedReturnsInString:SourceString
set theNSString to current application's NSString's stringWithString:SourceString
set RefinedSourceString to (theNSString's stringByReplacingOccurrencesOfString:"\\R" withString:space options:(current application's NSRegularExpressionSearch) range:{0, theNSString's |length|()}) as text
end ReplaceLineFeedReturnsInString:
You could also use the shell command tr along these lines:
set ss to "Around " & linefeed & "the world" & return & "from East" & linefeed & return & "to West" & character id 8233 & "from South" & character id 8232 & "to North"
do shell script "echo " & quoted form of ss & " | tr -s '" & linefeed & return & character id 8232 & character id 8233 & "' ' '"
In the terminal, the command would look something like this, basically replacing any of the four listed characters with a space:
[format]echo "Around
the world
from East
to West<2029>from South<2028>to North" | tr -s ‘\n\r<2028><2029>’ ’ '[/format]
NB ‘<2028>’ is how the terminal represents the separator character. Also, I use echo here for simplicity but know that it appends a newline.
The shell also has access to groupings such as [:cntrl:], which includes linefeed and return, but it doesn’t include the two oddball separators, so unfortunately, you can’t simply go with ‘[:cntrl:]’ as your ‘string1’. You could do this but I’m not sure whether it’s an improvement over the above unless there are additional characters you would like to replace.
do shell script "echo " & quoted form of ss & " | tr -s '[:cntrl:]" & character id 8232 & character id 8233 & "' ' '"
A list of the character groupings can be found with ‘man tr’, and the characters within the groupings can be found (depending upon the grouping) at or around Apple’s ctype(3) page, but I was unable to locate any which include either separator. Look for ‘iscntrl’ there.