Compare text files with BBEdit

Here is a nice short little script for comparing the two selected records with BBEdit.

tell application id "DNtp"
	
	set theRecords to the selection
	set theRecord1 to item 1 of theRecords
	set theRecord2 to item 2 of theRecords
	
	set thePath1 to the path of theRecord1
	set thePath2 to the path of theRecord2
	
end tell
tell application "Finder"
	
	set theFile1 to get POSIX file thePath1 as text
	set theFile1 to theFile1 as alias
	set theFile2 to get POSIX file thePath2 as text
	set theFile2 to theFile2 as alias
	
end tell

tell application "BBEdit"
	set theResult to compare file theFile1 against file theFile2
	activate
end tell

Needless to say, this is for plain text only.

Nice.

Here’s a simpler version (still not error trapped)

tell application id "DNtp"
	
	set theRecords to the selection
	set theRecord1 to item 1 of theRecords
	set theRecord2 to item 2 of theRecords
	
	set thePath1 to the path of theRecord1
	set thePath2 to the path of theRecord2
	
end tell

tell application "BBEdit"
	set theResult to compare file (thePath1 as POSIX file) against file (thePath2 as POSIX file)
	activate
end tell

And here’s a teaching edition version with some error trapping in place…

property err : " is not a plain text file." -- Just a default error message as a convenience

tell application id "DNtp"
	
	set theRecords to the selection
	if (count selection) = 2 then -- Verify only two files are selected
		
		-- Check Record 1
		set theRecord1 to item 1 of theRecords
		set recType to type of theRecord1 -- Get the type
		if (recType as string) = "text" then -- If it's plain text, proceed
			set thePath1 to the path of theRecord1 -- Get the file path
		else
			display alert "Record 1 " & err -- If it's not plain text, alert and stop.
			return
		end if
		
		-- Check Record 2, if 1 passes…
		set theRecord2 to item 2 of theRecords
		set recType to type of theRecord2
		if (recType as string) = "text" then
			set thePath2 to the path of theRecord2
		else
			display alert "Record 2" & err
			return
		end if
		
	else -- If two files aren't selected, alert and stop.
		display alert "Please select two plain text files to compare."
		return
	end if
end tell

tell application "BBEdit"
	set theResult to compare file (thePath1 as POSIX file) against file (thePath2 as POSIX file)
	activate
end tell
2 Likes

Thanks for the teaching edition, @BLUEFROG Jim! Very useful.
I am still learning my way around the different ways of referencing files in the system (POSIX, alias, and so on).

You’re welcome. :slight_smile:

Jim @BLUEFROG, it just occurred to me that, theoretically, it should be possible to compare the text of pdf files as well (that have a text-layer, of course).

Out of the top your head, do you know how to turn the plain text of theRecord into a temp file for comparison?

I would lean toward the shell for this but here’s some info on the AppleScript approach…

https://developer.apple.com/library/archive/documentation/LanguagesUtilities/Conceptual/MacAutomationScriptingGuide/ReadandWriteFiles.html

1 Like