tab-delimited entries

Hi,
what i’m trying to do, is to parse a string in a determinate position in a tab-delimited text file, (i got it, don’t worry) and successively to replace the given string into another one. (seems easy, but i get a bug when doing it)
After several runs with longer and shorter strings to replace, the text file becomes corrupt and adds more paragraphs and truncated strings from the last entry.
The number of entries should be always 4 entries only. Suggestions? :slight_smile:


property module_id : 2

set my_text to "ab	cd	ef	gh"
set rep_text to "kl"

my get_fav(my_text, rep_text, 2)
on get_fav(my_text, rep_text, ky)
	set tid to text item delimiters
	set text item delimiters to tab
	set save_fav to (text item module_id of my_text)
	set text item delimiters to tid
	#log my_text
	
	if ky is 2 then #write to fav
		set text item delimiters to save_fav
		set my_text to text items of my_text
		set text item delimiters to rep_text
		set my_text to text 1 thru -2 of (paragraph 1 of (my_text as text) as text)
		set text item delimiters to tid
	end if
	
	#log {my_text, rep_text}
	return {my_text, rep_text}
end get_fav

another question:
-how to fetch effectively all alphanumeric strings in a line ? (line, not paragraph)

Hello.

In all fairness, I copied your script without logging in, but then there were spaces and not tabs in your string “my_text”, I changed that, and now I think it works as it should. :slight_smile:

set AppleScript's text item delimiters to ""
property module_id : 2

set my_text to "ab	cd	ef	gh"
set rep_text to "kl"

my get_fav(my_text, rep_text, 2)
on get_fav(my_text, rep_text, ky)
	set tid to text item delimiters
	set text item delimiters to tab
	set save_fav to (text item module_id of my_text)
	set text item delimiters to tid
	#log my_text
	
	if ky is 2 then #write to fav
		set text item delimiters to save_fav
		set my_text to text items of my_text
		set text item delimiters to rep_text
		set my_text to text 1 thru -2 of (paragraph 1 of (my_text as text) as text)
		set text item delimiters to tid
	end if
	
	#log {my_text, rep_text}
	return {my_text, rep_text}
end get_fav

I don’t care much for who wins soccer-games, as long as Italia and Juventus loose. :smiley:

another approach


property module_id : 2

set my_text to "ab	cd	ef	gh"
set rep_text to "kl"


get_fav(my_text, rep_text, module_id)

on get_fav(textToParse, textToReplace, listIndex)
	set {tid, text item delimiters} to {text item delimiters, tab}
	set tabDelimitedList to text items of textToParse
	set item listIndex of tabDelimitedList to textToReplace
	set returnValue to tabDelimitedList as text
	set text item delimiters to tid
	return returnValue
end get_fav


Hello Stefan

It seems that you failed to take care of the fact that the original code replace only if listindex is equal to 2.
If I understand well, if the value of the property module_id is not 2 there is no replacement to apply.

KOENIG Yvan (VALLAURIS, France) lundi 24 juin 2013 22:00:09

OK, I added a line which returns the unchanged text in case module_id doesn’t match listIndex


property module_id : 2

set my_text to "ab    cd    ef    gh"
set rep_text to "kl"

get_fav(my_text, rep_text, 2)

on get_fav(textToParse, textToReplace, listIndex)
	if listIndex is not module_id then return textToParse
	set {tid, text item delimiters} to {text item delimiters, tab}
	set tabDelimitedList to text items of textToParse
	set item listIndex of tabDelimitedList to textToReplace
	set returnValue to tabDelimitedList as text
	set text item delimiters to tid
	return returnValue
end get_fav

Hi Stefan, Yvan and MacUsr
thank you all.
By the way, i use to paste compiled scripts, but my tabs were converted to spaces.
Interesting is, that Stephan’s first post became compiled correctly, out of the browser.
How does FluxBB work? :lol:

Second question:
how to fetch effectively all alphanumeric strings in a line ? (line, not paragraph)

Hi Joy,

You can’t get lines from paragraphs. The number of lines depends on the size of the text field. I think they used to have end of line before, but that was in an older text editor. Interesting question.

Edited: BTW, Yvan Koenig might have a solution with possibly more information about the text you’re using. He had some great workarounds with AppleWorks.

gl,
kel

Hello.

You can find the displayed line of the text of a document in TextWrangler, that is the only way I think I know of to get every word of a line, to get at the alphanumeric chunks. And then, the displayed line is of course variable, depending of the window width, or the page margin settings in TextWrangler (set in stone by a number, to page guide, or window width ).

Now, if you define a line to be 80 characters long, then it is easier, then you can just return (every word of text 1 thru 80 of paragraph i ) or something like that. The dash (-) counts as an alphanumeric by AppleScript, I believe, if negative numbers are involved.

HTH

Hi,

i tried Stephan’s solutions and i get yet empty paragraphs (after paragraph 2) and truncated strings (paragraph 2).
The mysterious bug continues to run. :o

And about line: Ops, maybe i’ve misspelled something, i meant with line the interruption of a phrase. :smiley:

Hi Joy,

You should probably solve your first question first, but how do you get the text for your second question about lines not paragraphs?

Edited: what do you mean by interruption of phrase?

Either you have stopped once, in the middle of the script, without resetting the delimiters, or you have something else running that also uses delimiters, and don’t behave properly, that is my guess anyway.

It is probably an applet, or some script you have run, shouldn’t be too hard to track down! Happy hunting. :wink:

Hi kel

by phrase i meant words forming a phrase, ending with a point. But this is an ideal case. Sometimes i want only one “line” of a paragraph.
Text Wrangler, for instance, labels every line with a number:

1 | text abc
2 | def
3 | x

Hi MacUsr,
i’ll investigate on it. At the beginning i posted an example to ease the understanding for my request and there i used simple characters, but in reality the contents of the file to parse is more complicated, like this:

set my_text to "Neue Notiz		Neue Referenzen	Neuer Vorlage Brief	Neue Aufgaben"
set rep_text to "Filtere Derivate von X"

Hello.

The code below may see to that your text itemd delimiters are adressed in an atomically way, that is, that no other AppleScript using the same instance of AppleScript gains access to the text item delimiters while you are using them.



property module_id : 2

set my_text to "ab	cd	ef	gh"
set rep_text to "kl"

my get_fav(my_text, rep_text, 2)
on get_fav(my_text, rep_text, ky)
	if ky is not 2 then
		return {my_text, rep_text}
	else
		tell (a reference to AppleScript's text item delimiters)
			set oldTids to contents
			set contents to tab
			set contents to (text item ky of my_text)
			set my_text to text items of my_text
			set contents to rep_text
			set my_text to text 1 thru -2 of (paragraph 1 of (theList as text) as text)
			set contents to oldTids
		end tell
		#log {my_text, rep_text}
		return {my_text, rep_text}
	end if
end get_fav

This is not an idea of my own by the way, I have seen john8 do it, maybe not for this reason, maybe for speed, but when I saw it the first time around, and figured out why, I drew the conclusion, that it was to guarantee atomicity.

I may be wrong in that this will make everything inside the tell block into an atomic opeation, but it is well worth a try, in order to shield your process’s usage of text item delimiters, from some processes using the same text item delimiters, running in parallell.


property module_id : 2

set my_text to "Neue Notiz	Neue Referenzen	Neuer Vorlage Brief	Neue Aufgaben"
set rep_text to "Filtere Derivate von X"
get_fav(my_text, rep_text, 2)

on get_fav(textToParse, textToReplace, listIndex)
	if listIndex is not module_id then return textToParse
	set {tid, text item delimiters} to {text item delimiters, tab}
	set tabDelimitedList to text items of textToParse
	set item listIndex of tabDelimitedList to textToReplace
	set returnValue to tabDelimitedList as text
	set text item delimiters to tid
	return returnValue
end get_fav

Here is the stefan’s script with your late set of datas.
Here (os X 10.8.4) , it behaves flawlessly.

KOENIG Yvan (VALLAURIS, France) mardi 25 juin 2013 12:25:52

Hello.

I never doubted Stefan’s script to work perfectly under normal circumstanses, my version, are made to take care of the extra case of text item delimiters, getting set/reset, by a parallell process, that is, shield the text item delimiters from that, by making the block using the text item delimiters, into a hopefully atomic operation.