Text to List Delimiter Problem

I pirated a function posted here that coverts text to a list using an arbitrary separator. The function works great except when the separator is a space. My attempts to fix it keeps breaking the function. Can someone tell me how to fix this fuction so that it works with space as the separator without breaking? TIA.


on textToList(theText, theSep)
	set soFar to {}
	set textSoFar to theText
	repeat until theSep is not in textSoFar
		set thePos to the offset of theSep in textSoFar
		set thenewPos to thePos
		if thenewPos is 1 then set thenewPos to 2
		set nextBit to text 1 through (thenewPos - 1) of textSoFar
		if textSoFar is not theSep then
			set textSoFar to text (thePos + (count of text items in theSep)) through -1 of textSoFar
			display dialog textSoFar
			copy nextBit to the end of soFar
		else
			set textSoFar to ""
		end if
	end repeat
	copy textSoFar to the end of soFar
	return soFar
end textToList


Hi,

Your handler works for me. Try:

set myText to "some words to test."
textToList(myText, " ")

on textToList(theText, theSep)
	set soFar to {}
	set textSoFar to theText
	repeat until theSep is not in textSoFar
		set thePos to the offset of theSep in textSoFar
		set thenewPos to thePos
		if thenewPos is 1 then set thenewPos to 2
		set nextBit to text 1 through (thenewPos - 1) of textSoFar
		if textSoFar is not theSep then
			set textSoFar to text (thePos + (count of text items in theSep)) through -1 of textSoFar
			display dialog textSoFar
			copy nextBit to the end of soFar
		else
			set textSoFar to ""
		end if
	end repeat
	copy textSoFar to the end of soFar
	return soFar
end textToList

Are you sure all all the spaces in the text you are converting are non-breaking spaces (ie ASCII character 32) and not other types of spaces (tabs, carriage returns etc.)?

On another point. Is there a reason not to use text item delimiters to do this?

set myText to "some words to test."
textToList(myText, " ")

on textToList(theText, theSep)
	set {myTID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, {theSep}}
	set soFar to text items of theText
	set AppleScript's text item delimiters to myTID
	return soFar
end textToList

Best wishes

John M

Thanks for the reply. The white space is only ASCII 32. As to your second question, I don’t know. As I said I pirated the code from this list and it works mostly. I am not an expert so I don’t know if your code will solve the problem or not. Do I take it from your reply that what I have should work even for spaces? The result I am getting is that the list item is missing the first character when they are separated by spaces.

Yes, your handler works for me with spaces. As to the handler I suggested, why not try replacing your handler with it and see if it works.

Best wishes

John M

Your script gives the right answer - what is confusing you is the display dialog textSoFar in the middle of the repeat. I agree with John, however, text item delimiters are the only way to go here. Worth learning how to do that.

I think the original problem can be explained by the fact that the handler counts ‘text items’ without setting any text item delimiters. The default value of the delimiters is {“”}, so that a ‘text item’ is normally equivalent to a ‘character’. But the “missing first character” effect can be reproduced if the delimiters are set to the same as ‘theSep’. My guess is that the’ve been set to a space either elsewhere in gw1500se’s script or in another script previously run by the same application. John M’s suggestion isn’t vulnerable to this and is in any case far more efficient.

Thanks for all the help. First the reason for the display dialog was for debugging purposed. I forgot to edit it out when I posted the code.

I tried John M’s suggestion. Unfortuately, it doesn’t work because it includes the seperator as part of the item. That may be why the function I posted was more complex. However, if the original function works for spaces for you then I can’t imagine why it doesn’t work for me unless Nigel’s explaination is what is happening. I don’t know where the delimiter would have been set but is there any reason I can’t set it to “” at the beginning of my script? Would it be more reliable to use John M’s suggestion and simply strip off the last character every time? Would that still work when a space is the separator?

The Text Item Delimiter example I gave above should not include the seperator in the resulting list elements. When you run either of the scripts in my post above with Script Editor’s results pane open does the resulting list have spaces in it?

It’s had to say why there is a difference between my results and yours, unless you can explain where your text is coming from.

Best wishes

John M

Hmm. Well, in this particular case ‘theSep’ is a colon (‘:’) and there is a ‘:’ at the end of each item upon return. Is there something special about a colon?

I confess I haven’t run your code. I just dumped it into my script. I suspect the problem is somewhere in my script or my data so running your code would not help much. There is no reason to believe it will work any different on my machine then yours.

The data is coming from a shell script, specifically from the massaged output of ‘ldapsearch’. I get it into the script thus:

set list_items to (do shell script <path>)'s paragraphs
   .
   .
   .
repeat with this_list_item in list_items
   .
   .
   .
set keyword_list to textToList(this_list_item,":")
   .
   .
   .

Nigel’s evaluation was apparently correct. By forcing the delimiters to {“”} at the beginning of my script, the problem is solved. There is probably a better way to do this but since I have this part working now, I don’t think I want to mess with it. Thanks again for all the help.