Using Delimiters to Split Each line of text (I've read Unscripted)

I’ve read the Delimiters section in Unscripted, but its still taking me for a ride.

Trying to take a txt file and set each line to an item. Further, I want to take these items and split into a left and a right based on the delimiter “:” and then save two txt files with one side in each txt. In the .txt each left or right should be on a new line. I think I botched my question, I’ll try to illustrate below.

[b]file.txt/b

left1:right1
left2:right2

left.txt

left1
left2

right.txt

right1
right2

set theFile to choose file
set theText to read theFile

-- turn the text into a list to loop over it
set theTextList to paragraphs of theText

-- put every 1 line into a list and add it to the finalList variable
set listCount to count of theTextList
set finalList to {}
repeat with i from 1 to listCount by 1
	if (i + 0) is not greater than listCount then
		set end of finalList to items i thru (i + 0) of theTextList
	else
		set end of finalList to items i thru listCount of theTextList
	end if
end repeat

--return finalList

-- I dont know what the hell I'm doing here? -DrLulz
repeat with i from 1 to the count of finalList
	set oldTIDs to AppleScript's text item delimiters
	--set str to str as string
	--if str does not contain del then return str
	set AppleScript's text item delimiters to return
	set newString to finalList
	set AppleScript's text item delimiters to oldTIDs
	set finalString to newString as string
	
	
	--set AppleScript's text item delimiters to ":"
	--set theSplit to every list in newString as alias
	--set theLeft to theSplit's first item
	--set theRight to theSplit's second item
	
	--set AppleScript's text item delimiters to oldTIDs
	return finalString
end repeat

Hi,

try this


set theFile to choose file
-- get the parent folder of the file
tell application "System Events" to set parentFolder to path of container of theFile
set theText to read theFile

set leftList to {}
set rightList to {}
-- turn the text into a list to loop over it
set theTextList to paragraphs of theText

set {TID, text item delimiters} to {text item delimiters, ":"}
repeat with anItem in theTextList
	-- split each line by : and put the text items in two separated lists 
	set {end of leftList, end of rightList} to text items of anItem
end repeat

-- build the destination paths, get the file name by cutting the last character of each first list item
set lefttextFile to parentFolder & text 1 thru -2 of item 1 of leftList & ".txt"
set righttextFile to parentFolder & text 1 thru -2 of item 1 of rightList & ".txt"

-- turn each list into paragraphs of text
set text item delimiters to return
set leftList to leftList as text
set rightList to rightList as text
set text item delimiters to TID

-- write text to disk
writeToDisk from leftList into lefttextFile
writeToDisk from rightList into righttextFile


-- handler to write text to disk

on writeToDisk from theData into theFile
	try
		set fileRef to open for access file theFile with write permission
		write theData to fileRef
		close access fileRef
		return true
	on error
		try
			close access file theFile
		end try
		return false
	end try
end writeToDisk

EDIT:

let me provide more text, as what I deem trivial is in fact not.

Firstly, the script works perfectly within the context I provided. Further, in reference to the below text, when I separate each line into its own text file the script runs. When the lines are in one file (on their respective line) and I run the script it yields the error below.

accessory joint motions: See secondary joint motion.
accessory movements: Movements used to potentiate, accentuate, or compensate for an impairment in a physiologic motion (e.g., the movements needed to move a paralyzed limb).
accommodation: A self-reversing and non- persistent adaptation.

ORIGINAL:

thank you sir, but I’m receiving an error at:

set {end of leftList, end of rightList} to text items of anItem

the error is:

Result:
error "Can't get item 1 of {}." number -1728 from item 1 of {}

I was assuming that all lines in the the source text file contains a left[x]right[x] pair.
This code checks the number of items


.
repeat with anItem in theTextList
	-- split each line by : and put the text items in two separated lists 
	if (count text items of contents of anItem) is 2 then
		set {end of leftList, end of rightList} to text items of anItem
	end if
end repeat
.

yep that got it, but can you tell me why?

An alternative might be to check each line for the first “:” and separate accordingly.

the original code in my first answer splits the code into two parts
If there is no colon in the line or the line is empty there is no text item 1 and an error is thrown

the code in my second answer checks if there is exactly 1 colon in the line

much thanks