Search & Replace in textfile

I need to make a script which, when I drop a textfile on it,
removes a piece of text until the 5th semicolon (which is the delimiter in this text).
After that I have to replace the next semicolon by text1, the following semicolon by text2, and so on until I reach the end of the line.
The number of semicolons in each line is the same.
After that it should repeat the whole action.
Who can help?
Mac OS X Panther 10.3.3

Regards,
JB

Can you provide an example of the contents of the text files?

– Rob

Example:

a;b;c;d;e;f;g;h;i;j;k

It’s like a file exported from a spreadsheet program. A so called comma delimited file (CSV).
The program where it’s coming from is always exporting all the data, a to k.
I don’t need the first 5 cells, so I have to strip a to e and after that I want to fill in some text or returns in place of the ;

Is this what you mean?

Jon


[This script was automatically tagged for color coded syntax by Convert Script to Markup Code]

Here’s what I think might be close, although I’m not sure that I clearly understand the goal. Save this script as an application (it will become a droplet). To test the script, drop DUPLICATES of the actual files because it will modify the contents of the files. If it works then you can go from there and add error checking. :slight_smile:

property replacements : {"1", "2", "3", "4", "5", "6"} -- the values which will replace the semi-colons.

on open files_
	repeat with file_ in files_
		set text_ to (read file_ using delimiter ";")
		set temp1 to text 6 thru end of text_
		
		set temp2 to ""
		repeat with i from 1 to (count replacements)
			set temp2 to temp2 & (item i of replacements & item i of temp1) as text
		end repeat
		
		try
			set ref_ to open for access file_ with write permission
			set eof of ref_ to 0
			write temp2 to ref_
			try
				close access ref_
			end try
		on error
			try
				close access ref_
			end try
		end try
	end repeat
end open

– Rob

The goal of this script is to create a tagged text for indesign wich is containing al the layout commands.
I just can let it flow in a new document and the basic layout is ready.

I have scripted a quite lot of AppleScripts for InDesign, but it is difficult to help you without seeing your script so far. Why don’t you post it.

Kari

Rob,

Your piece is working great.
I changed the code to this:

This converts the text to tagged text for indesign.
When I import the text in indesign it’s completely formatted including paragraph styles.

Only 1 problem remaines.
When the sender is skipping one of the cells, so there is only a ; and no data in between, the replacement should also be skipped. Otherwise the script is producing unwanted data.
So I tried with an if-statement inside the second repeat (the bold faced text)

but this won’t work
Wrong place? wrong way?

Sorry,

already solved it
This is the result

many,many thanks to you all

:slight_smile: Great! But…

I’m afraid that I don’t have the time to try to set up a test scenario to debug this. Hopefully someone else will offer a solution. Sorry. :?

Edit: I see that you solved the problem as I was responding. I’m glad that you found a solution! :slight_smile:

– Rob