Removing line return from last paragraph using AppleScript

Hi,

I have a large amount of text broken into sections (using AppleScript delimiters) which mostly consist of several paragraphs. Each paragraph has a line return at the end of it. I would like to be able to remove the last line return after the last paragraph for each section, and I’m wondering how I can do this with AppleScript. In other words I would like the last character of each section to be the full stop at the end of the last paragraph.

Thanks

Nick

repeat while character -1 of x is in {return, linefeed}
	set x to text 1 thru -2 of x
end repeat

Hey Nick,

I’m surprised Shane didn’t throw some AppleScriptObjC at you. :cool:

But his old-school method is actually quite fast.

Personally I like to use the Satimage.osax for this sort of thing.


set myList to {"one
", "two
", "three

"}

set myList to change "\\s+\\Z" into "" in myList with regexp

--> {"one", "two", "three"}

One of its cool features is the ability to operate on lists of strings (as well as text of course).


Chris

My understanding is that the problem isn’t completely defined.
How are the sections supposed to be delimited ?

Ignoring the answer, Shane’s proposal remove the line breaks at the very end of the complete text, not at the end of every section.

Yvan KOENIG running Sierra 10.12.3 in French (VALLAURIS, France) mardi 14 février 2017 11:24:26

True.

But wouldn’t making the first character of the delimiter a return or linefeed deal with that?

This goes back to your first point.

Thanks Shane. Your method worked fine for my purposes.

Nick

I asked because I am accustomed to Pages in which there is an official definition of sections.
They are blocks of text separated by section breaks which are character id 4.

Starting from a document containing two section breaks, running the Shane’s code removed only the characters at the end of the document. The linefeeds preceding the section breaks weren’t removed.

So I tried to enhance the code and built :

set sectionBreak to character id 4

tell application "Pages" to tell document 1 to tell body text
	# Do what Shane's script do
	repeat while character -1 is in {return, linefeed}
		delete character -1
	end repeat
	
	# Now treats the possible sections
	set theChars to its characters
	set i to count theChars
	# loops while it remains a section break in the list theChars, aka while there is a section break before the current character
	repeat while theChars contains sectionBreak
		if theChars's item i ≠ sectionBreak then
			set i to i - 1
		else
			set theChars to items 1 thru i of theChars
			repeat
				if theChars's item -2 is in {return, linefeed} then
					delete its character (i - 1) # delete the linebreak preceding sectionBreak
					-- set item -2 of theChars to sectionBreak # we may get rid of that
					set theChars to items 1 thru -2 of theChars
					set i to i - 1
				else
					exit repeat
				end if
			end repeat
		end if
	end repeat
end tell

It’s probably not the most efficient code but at least it does the complete cleaning job.

Yvan KOENIG running Sierra 10.12.3 in French (VALLAURIS, France) mardi 14 février 2017 14:38:52