Change order of paragraph - Why the duplication?

Hi

The code below reads a txt-file that is not saved. The text of this test file is “aa, bb” (paragraph 1… nothing else) and the result when the script is finished is “bb aa”. OK so far.

If I then delete the text from the text-file and paste “cc, dd” the result should be “dd cc”, but I get “bb aa” (paragraph 1) followed by “dd cc” (paragraph 2). Why??? I’m doing something wrong, but can you tell me what it is? The code seems to remember the text from the last run.

property new_source_txt : {}

tell application "TextEdit"
	set the source_txt to text of document 1
	
	set AppleScript's text item delimiters to {"," & space}
	
	repeat with i from 1 to (count paragraphs of source_txt)
		set the_paragraph to paragraph i of source_txt
		try
			set {item_1, item_2} to {text item 1 of the_paragraph, text item 2 of the_paragraph}
			copy item_2 & space & item_1 & return to the end of new_source_txt
		end try
	end repeat
	
	set AppleScript's text item delimiters to {""}
	
	set text of document 1 to new_source_txt as string
end tell

Thank You
Pirri

Hello

You forgot that a property is remembered by the script so it would be useful to code:

property new_source_txt : {}

tell application "TextEdit"
	set new_source_txt to {} -- ADDED
	set the source_txt to text of document 1

or even, if it is not necessary to make new_source_txt global,

-- property new_source_txt : {}

tell application "TextEdit"
	set new_source_txt to {} -- ADDED
	set the source_txt to text of document 1

Yvan KOENIG

Thanks for the answer.

… and yes, I forgot about properties remembering

Thank you
Pirri