[Newbie] Merging two text files... almost there! Tips?

Brand new to Applescript and trying to take two text files, read in one line at a time from each and merge the result with a delimiter into a new file. I got it to where it could do one line (been reading around on this forum… thanks!)

Now I’ve put a loop in and horked the whole thing… not sure how to get a line count. And I can’t seem to get it to copy the result so I can paste it into a new BBEdit doc… following the “copy” docs as best I can, but it ain’t working. Any tips? TIA

set file_one to choose file with prompt “Select the first file.”
set file_two to choose file with prompt “Select the second file.”

set f1_reader to (read file_one using delimiter return)
set f2_reader to (read file_two using delimiter return)

set tot_lines to number of lines in f1_reader – Not sure how to get total count here
set x to 0
if x <= tot_lines then
set x to (x + 1)
set file_three to item x of f1_reader & “|” & item x of f2_reader
end if

– How can I copy file_three to BBEdit?

I just get this as a result so it ain’t looping (I don’t know how yet to echo stuff… first day with AS):

“Mine Hill Trail|44947001.CT.Hike-1.gif”

I wish I could suppress the quotes to, but that’s a super simple grep job after the fact.

:?:

The crux of your problem is that you’re repeatedly setting file_three to the concatenation of the two lines - each time overwriting the previous contents of file_three. You need to append to the end of file_three to keep the previous contents.

There’s also an easier way to do this repeat loop:

set tot_lines to number of items in f1_reader -- reading a file using delimiters gives you a list, so 'number of items' tells you the number of lines in this case

-- start off with a blank line
set file_three to ""
repeat with lineNum from 1 to tot_lines
  set file_three to file_three & item lineNum of f1_reader & "|" & item lineNum of f2_reader & return
  -- file_three now contains the concatenation of the two lines appended to the previous contents
  -- note that I've appended a return character to it
end repeat

so at this point ‘outputText’ the the complete combination of the two text files:

Now, this tells BBEdit to make a new document using file_three as the initial contents.

tell application "BBEdit 6.5"
	make new document with properties {text: file_three}
end tell

Note that the code may fail if file2 doesn’t contain at least the same number of lines as file 1.

Oh, and don’t worry about the quotes - that’s just how the Script Editor shows you have a string as opposed to some other object time (integer, date, etc.)