Selecting range of text

I need to select a range of delimited text from the beginning of a line to the 7th text delimiter of the line. For example, I have the following line of text:

pine trees, red tomatos, Idaho potatos, 702 Evergreen Terrace, John Doe, 01234, science, arts, spanish, german, french, italian

I need to select and copy all the text from the first character of the line to the 7th comma and paste on the next line.

jo:

Are you using TextEdit? Since I was not sure of the text application, I put together this script using your text line as a string:

set a to "pine trees, red tomatos, Idaho potatos, 702 Evergreen Terrace, John Doe, 01234, science, arts, spanish, german, french, italian"
set comma_off to {}
repeat with b from 1 to count a
	if character b of a is "," then
		set end of comma_off to b
	end if
end repeat
set the_text to characters 1 thru (character (item 7 of comma_off)) of a as string
the_text

The result is a string that actually includes the 7th comma, but that can be easily remedied, if necessary. In addition, the list [comma_off] contains the offset of every comma in the entire string. If your initial string is the basis for a lot of other procedures, this list can be used later on without having to reprocess another string.

As for working within a text editor, I have not got a lot of experience with that, but there may be some other threads out there that can point you in the right direction.

Let me know if this helps or not.

Thanks Craig. I’m a bit further along thanks to your help. I’m using BBEdit 8.2.4. I’ve got a text file that contains student transcrips that I’m trying to convert to a comma delimited file. I’m able to get the text of line 1 delimited, but I need to select the first 7 deliminations that contain the student information. The course information and grades are contained in the rest of the line. I altered the script to the following:

tell application "BBEdit"
	set a to contents of line 1 of window 1
	set comma_off to {}
	repeat with b from 1 to count a
		if character b of a is "," then
			set end of paragraph_off to b
		end if
	end repeat
	set the_text to characters 1 thru (character (item 7 of comma_off)) of a as string
	set selection to the_text
end tell

I need to copy the first 7 deliminations of each line and paste to the next line. The problem is that I end up with a comma after each character. I suspect it is my alteration of a. I can’t set a to a fixed string because it may change from line to line. What do you suggest?

Thanks for your help.
JR

Jacques:

I could not get your line to function properly in the script. I tried a number of variations, but AS could not do the math of backing up one character on a single code line. Although this is bulky, it works:

set a to "pine trees, red tomatos, Idaho potatos, 702 Evergreen Terrace, John Doe, 01234, science, arts, spanish, german, french, italian"
set comma_off to {}
repeat with b from 1 to count a
	if character b of a is "," then
		set end of comma_off to b
	end if
end repeat
set str_end to item 7 of comma_off
set the_texta to characters 1 thru (str_end - 1) of a as string--This is the only way I could get it to skip the final comma.
set tdl to text item delimiters
set text item delimiters to ", "
set the_text to text items of the_texta--This produces a list of the seven items, no commas in the individual items.
set text item delimiters to " "
set final_text to the_text as string--This makes a single line with all the items, no commas.
set text item delimiters to tdl
final_text

I apologize for the bulky code; I really struggle with neatness and efficiency.

Actually, I think I have come up with a simpler solution after all:

set a to "pine trees, red tomatos, Idaho potatos, 702 Evergreen Terrace, John Doe, 01234, science, arts, spanish, german, french, italian"
set tdl to text item delimiters
set text item delimiters to ","
set b to text items 1 thru 7 of a--Just grab the first 7 items, using the commas as delimiters
set text item delimiters to " "
set final_text to b as string--Make the string from the 7 grabbed items.
set text item delimiters to tdl
final_text--The final string of items, all on one line

Hi guys.

Craig just beat me to it with the idea. I was just about to post this - which is a slight refinement, anyway:

to parse_CSV(t, n)
	set d to text item delimiters
	set text item delimiters to ","
	set t to t's text 1 thru text item n
	set text item delimiters to d
	t
end parse_CSV

set t to "pine trees, red tomatos, Idaho potatos, 702 Evergreen Terrace, John Doe, 01234, science, arts, spanish, german, french, italian"

parse_CSV(t, 7)

--> "pine trees, red tomatos, Idaho potatos, 702 Evergreen Terrace, John Doe, 01234, science"

The parse_CSV handler would go outside your application tell block. From inside the tell block, refer to it as: my parse_CSV(t, 7)

(The ‘my’ is significant.) :slight_smile:

kai:

Very nice. joseed’s second post mentioned a comma issue:

Any suggestions on how to efficiently remove the commas from the final string [t]?

Funny - I was discussing this very issue (elsewhere) very recently, Craig. :slight_smile:

It’s a classic gotcha of using the ‘characters as string’ approach - when the text item delimiters are stuck on a value other than {“”}. In this case, they appear to have been set to a comma (and not reset). If jo just runs this in a separate script, it should fix things:

set text item delimiters to {""}

Apart from being substantially faster, the ‘text’ approach that I suggested isn’t prone to this problem.

Incidentally, I’m not currently using BBEdit - but I thought you might be interested in this routine, which converts an entire document in TextEdit. It assumes that all the initial data is in the odd-numbered paragraphs, and that the even-numbered paragraphs are empty…

property linefeed : ASCII character 10

to convert_list(l)
	if l does not end with linefeed then set l's end to linefeed
	script o
		property p : l
	end script
	set text item delimiters to ","
	repeat with i from 1 to count l by 2
		set (o's p)'s item (i + 1) to text 1 thru text item 7 of (o's p)'s item i & linefeed
	end repeat
	set text item delimiters to {""}
	tell o's p to beginning & ({""} & rest)
end convert_list

tell application "TextEdit" to tell document 1 to set its text to my convert_list(paragraphs)

Thanks Jacques. This worked perfectly with no comma problem. Now I need to paste or insert the result at the beginning of the next line.

Again, thanks Craig, Jacques, and Kai for all your help with this.
JR