APPLESCRIPT: count lines and replace text

Hi All

I have the following list where in need to the first new line to “linebreak” and then put X’s for the in-between ones.

I have.

Shane
Shane
Shane
Shane
Shane
Shane
Shane
Shane
Shane
Shane
Alan
Alan
Alan
Alan
Alan
Alan
Alan
Alan
Alan
Alan
Alan
Jenny
Jenny
Jenny
Jenny
Jenny
Jenny
Jenny
Jenny
Jenny
Jenny

but need to change it to.
LineBreak
X
X
X
X
X
X
X
X
X
LineBreak
X
X
X
X
X
X
X
X
X
X
LineBreak
X
X
X
X
X
X
X
X
X

thanks for any answers

Shane

Hello.

I think you are looking for something like this, you are not telling much about the context. :slight_smile:

I solved your problem by getting the text as paragraphs, effectively turning the chunk of text into a list with each item consisiting of one paragraph. I then created the start of the new text, (you may want to exchange linefeeds for returns if that suits you better.) Having counted the number of lines, and made the preamble, I just assigned the first list as to what to compare to, and appended an X to the end of the new text. Whenever a change occured, I appended “Linebreak”, and changed the comparator to be the new item. But only on lines that contains something, effectively discarding blank lines at the the end. (I also discard any blank lines from the front of the text. )

set oldText to paragraphs of "Shane
Shane
Shane
Shane
Shane
Shane
Shane
Shane
Shane
Shane
Alan
Alan
Alan
Alan
Alan
Alan
Alan
Alan
Alan
Alan
Alan
Jenny
Jenny
Jenny
Jenny
Jenny
Jenny
Jenny
Jenny
Jenny
Jenny
"
set lc to (count items of oldText)
set j to 1
repeat while lc ≥ j
	if item j of oldText ≠ "" then
		set comparator to item j of oldText
		exit repeat
	else
		set lc to lc - 1
	end if
	set j to j + 1
end repeat

if lc > 0 then
	set newText to "Linebreak" & linefeed
else
	set newText to ""
end if

set subtract to 0
repeat with i from (j + 1) to lc
	if item i of oldText = comparator then
		set newText to newText & "X" & linefeed
	else if item i of oldText ≠ "" then
		set comparator to item i of oldText
		set newText to newText & "Linebreak" & linefeed
	else
		set subtract to subtract + 1
	end if
end repeat
log "new text"
log newText
set lc to (lc - subtract)
log "linecount: " & lc

HI McUsrII

thanks for your reply “ it works a treat!

The context. I’m auto flowing a els to indesign and these are the product sections where i need a page break.

thanks

LJ