Merge File

AppleScripters,

I am working on a project, in which there are two documents (document1 and document2). While this documents have a great deal of differences, they also have a great deal of similarity. Document1 has one word on its own line, whereas the document2 has the same word (as document1) on each line, but contains a tab (\t) a word (myWord) and another tab (\t). For visual aid here is what they would look like

document1:
This\t[word]\t[word]
is\t[word]\t[word]
a\t[word]\t[word]
text\t[word]\t[word]
string\t[word]\t[word]

document2:
This
is
not
a
text
string

Here is automated needs: I need to get “not” into document 1, making it thus:
This\t[word]\t[word]
is\t[word]\t[word]
not
a\t[word]\t[word]
text\t[word]\t[word]
string\t[word]\t[word]

I don’t want or need the \t[text]\t[text] in the final doc. I just need to compare document1 to document2, find the difference between 1 and 2, take the difference back to document1, yet maintaining the structure and information in document1.

Here is what I’ve got thus far:

tell application "BBEdit"
	-- Compare two colums of Text between two documents

	-- Get selected text of document1&2
	set mySelection1 to selection of first window as string
	set mySelection2 to selection of second window as string

	-- Compare two selections of two documents
	set document1 to (name of the first window)
	set document2 to (name of the second window)

	--tell "BBEdit"
	if mySelection1 = mySelection2 then
		display dialog "There is no difference"
	else
		get every word of mySelection1 as item
	end if
	
	--Pseudo-code
	-- read each selection
	-- remove "\t[text]\t[text]" from mySelection1 or mySelection2 (depending on which has it)
	-- find differences between mySelection1 and mySelection2
	(*In this case, find the "not" *)
	-- move the difference to mySelection1 (the selection which has the "\t[text]\t[text]"
	--copy in missing element
	--restore mySelection to:
	(*	this\t[text]\t[text]
			is\t[text]\t[text]
			not
			a\t[text]\t[text]
			column\t[text]\t[text]
			of\t[text]\t[text]
			text\t[text]\t[text]
		*)
end tell

Any wisdom that you could offer would be greatly appreciated. Thanks!

This will need some more error trapping to make sure the windows are in the right order and that the selections correspond:

tell application "BBEdit"
	-- Compare two colums of Text between two documents
	
	-- Get selected text of document1&2
	set mySelection1 to paragraphs of (selection of first window as string)
	if (mySelection1 is {}) then display dialog "There's no selection in window 1." buttons {"Stop"} default button 1 cancel button 1 with icon stop
	set mySelection2 to paragraphs of (selection of second window as string)
	if (mySelection2 is {}) then display dialog "There's no selection in window 2." buttons {"Stop"} default button 1 cancel button 1 with icon stop
end tell

-- Replace any matching strings in mySelection2 with those from mySelection1.
set i to 1
set selection1Count to (count mySelection1)
repeat with j from 1 to (count mySelection2)
	if (item i of mySelection1 begins with item j of mySelection2) then
		set item j of mySelection2 to item i of mySelection1
		set i to i + 1
		if (i > selection1Count) then exit repeat
	end if
end repeat

if (mySelection2 = mySelection1) then
	display dialog "There is no difference." buttons {"OK"} default button 1 with icon note
else
	-- Use the doctored mySelection2 text for the selection in window 1.
	set astid to AppleScript's text item delimiters
	set AppleScript's text item delimiters to linefeed
	set selection1Edit to mySelection2 as text
	set AppleScript's text item delimiters to astid
	
	tell application "BBEdit" to set selection of window 1 to selection1Edit
end if

Thanks, Nigel. This is a great help.

When running the script, I get the wrong results, however.

As of now, this is what I get when I run the script:

mySelection1:
this\t[text]\t[text]
is\t[text]\t[text]
a\t[text]\t[text]
column\t[text]\t[text]
of\t[text]\t[text]
text\t[text]\t[text]

mySelection2:
this
is
not
a
column
of
text

When I run the Script, it moves the contents of mySelection1 to the other document, thus reproducing mySelection1 as follows:
this\t[text]\t[text]
is\t[text]\t[text]
a\t[text]\t[text]
column\t[text]\t[text]
of\t[text]\t[text]
text\t[text]\t[text]

When I am hoping to grab the difference and include it, which would make the output:

this\t[text]\t[text]
is\t[text]\t[text]
not
a\t[text]\t[text]
column\t[text]\t[text]
of\t[text]\t[text]
text\t[text]\t[text]

I don’t need the “not” to have \t[text]\t[text]. I just need the script to include it in the edited selection.

Thanks for the help! It’s very much appreciated.

**DIsregard the above. I was declaring the wrong window to be the front. Thanks!

Hi, James.

This version works with either window in front as long as they’re the front two:

on showError(msg)
	display dialog msg buttons {"Stop"} default button 1 cancel button 1 with icon stop
end showError

tell application "BBEdit"
	set window1 to window 1
	set window2 to window 2
	
	set mySelection1 to selection of window1 as string
	set mySelection2 to selection of window2 as string
end tell

-- Check there's a selection in both windows (ie. that both selections have something in them).
if ((count mySelection1) is 0) then showError("There's no selection in window 1.")
if ((count mySelection2) is 0) then showError("There's no selection in window 2.")

-- Check that the windows are in the right order. Window 1's selection should have the tabs, window 2's shouldn't.
-- If they're juxtaposed, swap the variables. If in doubt, error.
set noTabInSelection1 to (mySelection1 does not contain "\\t")
set tabInSelection2 to (mySelection2 contains "\\t")
if ((tabInSelection2) and (noTabInSelection1)) then
	set {window1, mySelection1, mySelection2} to {window2, mySelection2, mySelection1}
else if ((tabInSelection2) or (noTabInSelection1)) then
	showError("Not sure if the text in the front two windows is correct.")
end if

-- Get the paragraphs of the two selections.
set mySelection1 to mySelection1's paragraphs
set mySelection2 to mySelection2's paragraphs

-- Replace any matching strings in mySelection2 with those from mySelection1.
set i to 1
set selection1Count to (count mySelection1)
repeat with j from 1 to (count mySelection2)
	if (item i of mySelection1 begins with item j of mySelection2) then
		set item j of mySelection2 to item i of mySelection1
		set i to i + 1
		if (i > selection1Count) then exit repeat
	end if
end repeat

if (mySelection2 = mySelection1) then
	display dialog "There is no difference." buttons {"OK"} default button 1 with icon note
else
	-- Use the doctored mySelection2 text for the selection in the window with the tabs.
	set astid to AppleScript's text item delimiters
	set AppleScript's text item delimiters to linefeed
	set selection1Edit to mySelection2 as text
	set AppleScript's text item delimiters to astid
	
	tell application "BBEdit" to set selection of window1 to selection1Edit
end if