Show differences between two frontmost scripts in your script editor.

Hello.

I provide two versions of the same script here, one for ApplescriptEditor and the other for Script Debugger, so it should be fairly easy to adapt to other editors.

I use Filemerge for showing the differences, so you need either to install Developer tools, or rework it to work with for instance twdiff that comes with TextWrangler.

It is up to you to learn what you can do with FileMerge.

property scriptTitle : “ASEDiff”

tell application "AppleScript Editor"
	if ((count its documents) < 1) then
		tell me to display notification "You really need to have two documents open" subtitle my scriptTitle
		error number -128
	end if
	set shortNm to name of its document 1
	set text1 to quoted form of ("Active File" & linefeed & contents of its document 1)
	set text2 to quoted form of ("Second File" & linefeed & contents of its document 2)
end tell

set theDir to do shell script "/usr/bin/mktemp -d -t " & quoted form of shortNm
set frontFile to "Active File"
set secondFile to "Second File"
set tempFn1 to quoted form of (theDir & "/" & frontFile)
set tempFn2 to quoted form of (theDir & "/" & secondFile)
do shell script "( cat <<<" & text1 & " >" & tempFn1 & "; cat <<<" & text2 & " >" & tempFn2 & "; /usr/bin/opendiff " & tempFn1 & " " & tempFn2 & " ) &> /dev/null &"


property scriptTitle : "ScriptDebuggerDiff"

tell application "Script Debugger 4.5"
	if ((count its documents) < 1) then
		tell me to display notification "You really need to have two documents open" subtitle my scriptTitle
		error number -128
	end if
	set shortNm to name of its document 1
	set text1 to quoted form of ("Active File" & linefeed & script source of its document 1)
	set text2 to quoted form of ("Second File" & linefeed & script source of its document 2)
end tell

set theDir to do shell script "/usr/bin/mktemp -d -t " & quoted form of shortNm
set frontFile to "Active File"
set secondFile to "Second File"
set tempFn1 to quoted form of (theDir & "/" & frontFile)
set tempFn2 to quoted form of (theDir & "/" & secondFile)
do shell script "( cat <<<" & text1 & " >" & tempFn1 & "; cat <<<" & text2 & " >" & tempFn2 & "; /usr/bin/opendiff " & tempFn1 & " " & tempFn2 & " ) &> /dev/null &"

I threw this in as an afterthought, as it may be okay to have the same functionality in TextEdit as well! :slight_smile:

property scriptTitle : "TextEditDiff"

tell application "TextEdit"
	if ((count its documents) < 1) then
		tell me to display notification "You really need to have two documents open" subtitle my scriptTitle
		error number -128
	end if
	set shortNm to name of its document 1
	set text1 to quoted form of ("Active File" & linefeed & text of its document 1)
	set text2 to quoted form of ("Second File" & linefeed & text of its document 2)
end tell

set theDir to do shell script "/usr/bin/mktemp -d -t " & quoted form of shortNm
set frontFile to "Active File"
set secondFile to "Second File"
set tempFn1 to quoted form of (theDir & "/" & frontFile)
set tempFn2 to quoted form of (theDir & "/" & secondFile)
do shell script "( cat <<<" & text1 & " >" & tempFn1 & "; cat <<<" & text2 & " >" & tempFn2 & "; /usr/bin/opendiff " & tempFn1 & " " & tempFn2 & " ) &> /dev/null &"


Thanks for the code. It will be very useful.

I changed it slightly, Only to take into account if the script is run from Applescript Editor or is open.
Probably not really needed as I suspect most will run it from the Applescript Menu.

set doc1 to 1
set doc2 to 2
tell application "AppleScript Editor"
	if ((count its documents) < 1) then
		tell me to display notification "You really need to have two documents open" subtitle my scriptTitle
		error number -128
	end if
	set myName to name of (info for (path to me))
	
	set shortNm to name of its document 1
	if myName is equal to shortNm then
		set doc1 to 2
		set doc2 to 3
	end if
	set text1 to quoted form of ("Active File" & linefeed & contents of its document doc1)
	set text2 to quoted form of ("Second File" & linefeed & contents of its document doc2)
	
end tell

set theDir to do shell script "/usr/bin/mktemp -d -t " & quoted form of shortNm
set frontFile to "Active File"
set secondFile to "Second File"
set tempFn1 to quoted form of (theDir & "/" & frontFile)
set tempFn2 to quoted form of (theDir & "/" & secondFile)
do shell script "( cat <<<" & text1 & " >" & tempFn1 & "; cat <<<" & text2 & " >" & tempFn2 & "; /usr/bin/opendiff " & tempFn1 & " " & tempFn2 & " ) &> /dev/null &"

Hello.

I guess there is a lot of ways to doing this, or anything else related to scripting. :slight_smile:

Too true.I don’t mind removing it if you think it is too minor… I don’t want to clutter the place.

Hello Mark.

I like seeing different variations and alternatives, on the contrary, I’d mind if you move it. :slight_smile:

I really hadn’t thought about doing it that way at all, and the dates show that I have posted “diff scripts” for some three years so I can honestly say that it showed me a new way of doing it!