need help with stack overflow error

Hi, I hope that someone can help me solving a stack overflow error that I get when running a script I created. I’ve pasted part of the script below. What the script does is read a text file and then split it into a number of smaller text files. The names of the smaller text files are already in the big text file. These names are between “&&&—” and “—&&&”. All the text after “—&&&” up until the next occurence of “&&&—” is text that needs to be put into a separate text file. I hope this is clear. Anyway, the script runs fine for smaller text files, but with larger text files (>25K) I get a “stack overflow” error. I have no idea why. Any suggestions are very welcome! I’ve given the script loads of memory but that doesn’t help at all… (I’m not a progammer at all, far from it, as you can probably tell from the script I pasted below.) Thanks - Roja

repeat
	try
		--i'm using the osax DTFindString even though I guess I don't really need it
		DTFindString "&&&---" in fileContents starting at searchFrom
	on error errMsg number errNum
		--DTFindString returns errNum -1 when it can't find the search string
		if errNum = -1 then exit repeat
	end try
	set firstCharOfFileName to result + 6
	DTFindString "---&&&" in fileContents starting at searchFrom
	set lastCharOfFileName to result - 1
	set shortFileName to (characters firstCharOfFileName thru lastCharOfFileName of fileContents) as string
	set firstCharOfMainText to lastCharOfFileName + 8
	DTFindString "&&&---" in fileContents starting at firstCharOfMainText
	set lastCharOfMainText to result - 2
	-- the next line is where the "stack overflow" error occurs after a number of text files have been created just fine
	set mainText to (characters firstCharOfMainText thru lastCharOfMainText of fileContents) as string
	tell application "Finder"
		set outFile to ((path to preferences folder as string) & "QXP:" & shortFileName) as string
		set fileRef to open for access outFile with write permission
		write mainText to fileRef
		close access fileRef
	end tell
	set searchFrom to lastCharOfMainText
end repeat

: Hi, I hope that someone can help me solving a stack overflow
: error that I get when running a script I created. I’ve pasted
: part of the script below. What the script does is read a text
: file and then split it into a number of smaller text files.
: The names of the smaller text files are already in the big
: text file. These names are between “&&&—” and
: “—&&&”. All the text after “—&&&” up
: until the next occurence of “&&&—” is text that
: needs to be put into a separate text file. I hope this is
: clear. Anyway, the script runs fine for smaller text files,
: but with larger text files (>25K) I get a “stack
: overflow” error. I have no idea why. Any suggestions are
: very welcome! I’ve given the script loads of memory but that
: doesn’t help at all… (I’m not a progammer at all, far from
: it, as you can probably tell from the script I pasted below.)
: Thanks - Roja

: repeat

: try

: --i’m using the osax DTFindString even though I guess I don’t
: really need it

: DTFindString “&&&—” in fileContents starting at
: searchFrom

: on error errMsg number errNum

: --DTFindString returns errNum -1 when it can’t find the search
: string

: if errNum = -1 then exit repeat

: end try

: set firstCharOfFileName to result + 6

: DTFindString “—&&&” in fileContents starting at
: searchFrom

: set lastCharOfFileName to result - 1

: set shortFileName to (characters firstCharOfFileName thru lastCharOfFileName of fileContents) as string

This is the most likely culprit. It makes a list of the individual characters you want and then makes a string consisting of them as well. Do that enough times and you’ll run out of space. Try:

[code:1:000] set shortFileName to text firstCharOfFileName thru lastCharOfFileName of fileContents[/code:1:000]

… which extracts the shorter string directly from the longer one without creating the intervening list.

: set firstCharOfMainText to lastCharOfFileName + 8

: DTFindString “&&&—” in fileContents starting at
: firstCharOfMainText

: set lastCharOfMainText to result - 2

: – the next line is where the “stack overflow” error
: occurs after a number of text files have been created just
: fine

: set mainText to (characters firstCharOfMainText thru lastCharOfMainText of fileContents) as string

Similarly here:

[code:1:000] set mainText to text firstCharOfMainText thru lastCharOfMainText of fileContents[/code:1:000]

: tell application “Finder”

: set outFile to ((path to preferences folder as string) & “QXP:” & shortFileName) as string

You don’t need ‘as string’ at the end of this line as what you’re creating is already a string - which is why (unless you have Jon’s Commands or Akua Sweets installed) the following line:

: set fileRef to open for access outFile with write permission

… should contain the word ‘file’:

[code:1:000] set fileRef to open for access file outFile with write permission[/code:1:000]

: write mainText to fileRef

: close access fileRef

: end tell

: set searchFrom to lastCharOfMainText end repeat

NG

Hi Nigel,
Thanks a lot! Your suggestions worked like a dream. Everything now works just fine. Thanks again.
Roja.