AppleScript: remove blank lines at end of markdown record

Some of my “scriptulations” (= manipulation of text by script) leave blank lines wafting around at the end of a markdown record. I like things tidy so in order to manage that untidy horror I developed this little handler (which is probably of no use to anyone but me and is, of course, pretty simple…but there you go). It’s called by something like set theText to my stripEndBlankLines(theText).

on stripEndBlankLines(someText)
	-- split the text into a list of paragraphs
	set theParas to paragraphs of someText
	-- keep checking the list until the last item is not a blank line
	repeat until last item of theParas is not ""
		-- check whether we have a trailing blank line
		if last item of theParas is "" then
			-- we do, so strip the last item
			set theParas to items 1 thru -2 of theParas
		end if
	end repeat
	set astid to AppleScript's text item delimiters
	set AppleScript's text item delimiters to linefeed
	set someText to theParas as text
	set AppleScript's text item delimiters to astid
	return someText
end stripEndBlankLines

Stephen

3 Likes