I don’t know Automator.app very well, but I think it should be possible to use an AppleScript in an Automator service.
The script below replaces one linefeed with two. It doesn’t check if there are already two linefeeds or if there are characters in front of a linefeed - if you want that you’ll need a regex, e.g. from Satimage scripting addition.
In any case I would first try to create an Automator service with the script below, if that works writing a regex script would be the next step.
-- Replace one linefeed with two linefeeds
set theText to "Hello World
Test
"
set newText to my replace_String(theText, linefeed, linefeed & linefeed)
on replace_String(theText, oldString, newString)
local ASTID, theText, oldString, newString, lst
set ASTID to AppleScript's text item delimiters
try
considering case
set AppleScript's text item delimiters to oldString
set lst to every text item of theText
set AppleScript's text item delimiters to newString
set theText to lst as string
end considering
set AppleScript's text item delimiters to ASTID
return theText
on error eMsg number eNum
set AppleScript's text item delimiters to ASTID
error "Can't replaceString: " & eMsg number eNum
end try
end replace_String
Here’s a version that only replaces a linefeed if there’s none before or after it.
-- Replace one linefeed with two linefeeds (via AppleScriptObjC)
use AppleScript version "2.4"
use framework "Foundation"
set theText to "Praesent vel sapien lobortis dolor rhoncus congue eget quis ligula. Nulla nunc dolor, malesuada sit amet pharetra sit amet, dapibus eget felis.
Mauris eget risus pellentesque, mattis arcu scelerisque, pellentesque metus. Cras maximus convallis tellus. Vivamus mollis nisi quis tincidunt semper.
Fusce facilisis, diam vitae bibendum blandit, leo risus iaculis purus, vitae porttitor lorem urna at odio.
Maecenas varius consequat sem ut convallis. Donec odio sem, maximus ut nulla in, pretium condimentum mi. Praesent dictum nunc non sem elementum, sed cursus turpis lacinia.
Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Nullam sed nisl sapien. Aliquam consectetur tortor nisl, id interdum tortor consequat a.
Morbi ullamcorper, tellus eget cursus pulvinar, mauris libero sollicitudin tortor, sit amet auctor nisl ex non nisi. Nunc vestibulum sagittis consectetur.
Aliquam sit amet nulla non nisi dictum mollis at at dui."
set theString to current application's NSString's stringWithString:theText
set newString to theString's stringByReplacingOccurrencesOfString:("(?<!" & linefeed & ")" & linefeed & "(?!" & linefeed & ")") withString:(linefeed & linefeed) options:(current application's NSRegularExpressionSearch) range:{location:0, |length|:length of theText}
set newText to newString as string
I tested this script in an Automator service, here’s what I found:
it seems it’s not possible to use plain text and rich text together as possible input
it seems it’s not possible to transform rich text without loosing formatting, e.g. bold
Here’s a service that accepts plain text as input.