copy text WHILE?

thanks for your help, again. i think from here I’ll be able to make the adjustments I want myself and write some other programs. it’s helpful to have this working model to go by.

Hi bridgerbell,

You’re welcome. Glad you’re trying to learn AppleScript. Be careful when experimenting with the read/write commands.

gl,

Late to the party, but I thought I’d throw in a slightly different approach, which you might want to take a look at. It should be fairly fast (which could be relevant, given the apparent size of the document), and attempts to preserve RTF encoding.

What’s not clear to me is whether the document section headings are actually formatted as “Sec. n.” (ending with a point) or “Sec. n” (ending in just the number). Your discussions appear to have mentioned both forms - and it obviously makes a difference. Here are a couple of variations that should cover either eventuality:

Section headings with end point:

repeat
	set sourceFile to choose file with prompt "Please choose the source RTF file:"
	if (sourceFile as Unicode text) ends with ".rtf" then exit repeat
	display dialog "Please choose an RTF file." buttons {"Cancel", "Retry..."} default button 2 with icon 2
end repeat

repeat
	set targetFolder to (choose folder with prompt "Please choose a target folder:") as Unicode text
	if (count (list folder targetFolder without invisibles)) is 0 then exit repeat
	display dialog "Please choose an empty folder." buttons {"Cancel", "Retry..."} default button 2 with icon 2
end repeat

set t to read sourceFile
set d to text item delimiters
considering case
	set text item delimiters to "\\cf0 "
	set h to t's text item 1 & text item delimiters
	set t to t's text ((count h) + 1) thru -2
	set n to 1
	set text item delimiters to "Sec. 1."
	repeat while (count t's text items) > 1
		set t to t's text item 2
		set f to open for access (targetFolder & text item delimiters & "rtf") with write permission
		set n to n + 1
		set text item delimiters to "Sec. " & n & "."
		write h & t's text item 1 & "}" to f
		close access f
	end repeat
end considering
set text item delimiters to d

Section headings without end point:

-- include the 2 initial repeat loops from above script and then continue with:

set t to read sourceFile
set d to text item delimiters
considering case
	set text item delimiters to "\\cf0 "
	set h to t's text item 1 & text item delimiters
	set t to t's text ((count h) + 1) thru -2
	set n to 1
	set text item delimiters to "Sec. 1"
	repeat while (count t's text items) > 1
		set t to t's text item 2
		set f to open for access (targetFolder & text item delimiters & ".rtf") with write permission
		set n to n + 1
		set text item delimiters to "Sec. " & n
		write h & t's text item 1 & "}" to f
		close access f
	end repeat
end considering
set text item delimiters to d