Comparing files with BBEdit/TextWrangler

Hi

I want to select two files in the Finder, calls this script and have TextWrangler launch in the File Diff view.


tell application "Finder"
	set sel to the selection
	if (count of sel) is 2 then
		tell application "TextWrangler"
			compare (item 1 of sel) against (item 2 of sel)	
		end tell
	end if
end tell

Here is the problem message

I don’t get it, what am I doing wrong? Can you help?

Cheers
Simon

I believe you have to open the files in TextWrangler, at least you do in BBEdit.

Hello!

I think I wrote a quite similiar script for comparing applescripts that can be found herehttp://macscripter.net/viewtopic.php?id=33758

I used that as a basis, and just hacked together this one for you, if you say how you want it, you’ll get it! that is how you want to access the two files and so on.

Now I believe you must have the files selected in a folder or two.

Come back if you want it to work differently and I’ll see what I can do! :slight_smile:




-- The Idea and implementation and any faults is totally mine. © McUsr 2010 and put in the Public Domain.
-- The usually guarrantees about nothing what so ever applies, use it at your own risk.
-- Read the documentation.
-- You are not allowed to post this code elsewhere, but may of course refer to the post at macscripter.net.
-- http://macscripter.net/post.php?tid=39085
(*
TERMS OF USE. 
This applies only to posting code, as long as you don't post it on other websites, or displays it (the code) on your own, you are welcome to do
whatever you want to do with it without any further permission. 
Except for the following: Selling the code as is, or removing copyright statmentents and the embedded link in the code (without the http:// part) from the code. 
I don't require you to keep this whole novel, but you must keep the link which refers to where it was originally posted. And my copyright.
You must also state what you eventually have done with the original source. This obviously doesn't matter if you distribute AppleScript as read only. I do not require you to embed any properties helding copyright notice for the code when you distribute as read only.

Credit for having contributed to your product would in all cases be nice!

If you use this code as part of script of yours you are of course welcome to post that code with my code in it here at macscripter.net. If you then wish to post your code elsewhere after having uploaded it to MacScripter.net please email me and ask for permission.

The ideal situation is however that you then refer to your code by a link to MacScripter.net
The sole reason for this, is that it is so much better for all of us to have a centralized codebase which are updated, than having to roam the net to find any usable snippets. Which some of us probabaly originated in the first hand.

I'm picky about this. If I find you to have published parts of my code on any other site without previous permission, I'll do what I can to have the site remove the code, ban you, and sue you under the jurisdiction of AGDER LAGMANNSRETT of Norway. Those are the terms you silently agree too by using this code. 

The above paragraphs are also valid if you violate any of my terms.

If you use this or have advantage of this code in a professional setting, where professional setting means that you use this code to earn money by keeping yourself more productive. Or if you as an employee share the resulting script with other coworkers, enhancing the productivity of your company, then a modest donation to MacScripter.net would be appreciated.

*)
-- © McUsr 2010 and put in Public Domain see: macscripter.net/viewtopic.php?id=33758 for reference and terms of use.

on run
	-- check if text wrangler of bbedit is installed.
	-- Dette er blokka for at finder er oppe.
	tell application "Finder"
		activate
		set theSel to (get selection)
		
		set selCount to count theSel
		
		--	if selCount is 0 then my errMsg() det er alltid et finder window og det er desktop
		
		if selCount is 2 then
			set {fileA, fileB} to {item 1 of theSel, item 2 of theSel}
		else
			if selCount is greater than 3 then my errMsg()
			
			if (count its windows) is 1 or selCount is 0 then my errMsg()
			-- only the desktop window or no items in the selection, -we don't handle folders - yet?
			set fileA to item 1 of theSel
			if class of fileA is not document file then my errMsg()
			set fw1 to id of its Finder window 1
			tell its Finder window 2 to activate
			
			set theSel to (get selection) -- of Finder window 1
			
			set selCount to count theSel
			if selCount ≠ 1 then my errMsg()
			set fileB to item 1 of theSel
			if class of fileB is not document file then my errMsg()
			tell Finder window id fw1 to activate
			if contents of fileB is equal to contents of fileA then my errMsg()
		end if
		
		
		-- there were something applescript a like so we will commence
		set shortNm to name of fileA
		set {tids, AppleScript's text item delimiters} to {AppleScript's text item delimiters, "."}
		set shortNm to text item 1 of shortNm
		set AppleScript's text item delimiters to ""
		set shortNm to shortNm as text
		
		
		set theDir to do shell script "/usr/bin/mktemp -d -t " & quoted form of shortNm
		
		set tempFn1 to quoted form of (theDir & "/" & my createPathDenotingFileNameWitoutExt((fileA as alias as text)))
		set tempFn2 to quoted form of (theDir & "/" & my createPathDenotingFileNameWitoutExt((fileB as alias as text)))
		
		try
			do shell script "cp " & quoted form of POSIX path of (fileA as alias) & " " & tempFn1
		end try
		
		try
			do shell script "cp " & quoted form of POSIX path of (fileB as alias) & " " & tempFn2
		end try
		
	end tell
	-- make the differences show up in text wrangler!
	try
		set myres to (do shell script "/usr/bin/twdiff --ignore-spaces " & tempFn1 & " " & tempFn2)
		tell me
			activate
			display alert "TextWranglert Diff: The files are alike!"
		end tell
	on error e number n
		tell application "TextWrangler"
			activate
		end tell
	end try
end run



on errMsg()
	beep
	tell me
		activate
		display dialog "You need to select two and only two two different Apple Script files in the frontmost Finder Window or one file in each of the two frontmost Finder windows in order to run this script.
" with title "AppleScriptDiff" buttons {"Ok"} default button 1 with icon file "Macintosh HD:System:Library:CoreServices:CoreTypes.bundle:Contents:Resources:Unsupported.icns"
		error number -128
	end tell
end errMsg



on createPathDenotingFileNameWitoutExt(hfSfileNameAsText)
	set {tids, AppleScript's text item delimiters} to {AppleScript's text item delimiters, ":"}
	-- set hfsFilenameAsText to {""} & text items 2 thru -1 of hfsFilenameAsText
	-- Uses the line above instead of line under if  you want to convert to unix alike filename.
	set hfSfileNameAsText to text items of hfSfileNameAsText
	set AppleScript's text item delimiters to "_"
	set hfSfileNameAsText to hfSfileNameAsText as text
	set AppleScript's text item delimiters to "."
	set hfSfileNameAsText to text item 1 of hfSfileNameAsText as text
	set AppleScript's text item delimiters to tids
	return hfSfileNameAsText
end createPathDenotingFileNameWitoutExt



You have to coerce the Finder references in the selection to aliases so that TextWrangler can understand them.


tell application "Finder" to set sel to the selection

if (count of sel) is 2 then
	tell application "TextWrangler"
		compare (item 1 of sel as alias) against (item 2 of sel as alias)
	end tell
end if

Hello! :slight_smile:

Now I have to make something to compare to dirctory trees with files. :smiley:

In due time, not this (iso) week…

You guys are AWESOME, thanks so much!! I ended up using @McUsr 's solution, though I simplified the last part a bit.


-- The Idea and implementation and any faults is totally mine. © McUsr 2010 and put in the Public Domain.
-- The usually guarrantees about nothing what so ever applies, use it at your own risk.
-- Read the documentation.
-- You are not allowed to post this code elsewhere, but may of course refer to the post at macscripter.net.
-- http://macscripter.net/post.php?tid=39085
-- http://macscripter.net/viewtopic.php?pid=153133
(*
TERMS OF USE. 
This applies only to posting code, as long as you don't post it on other websites, or displays it (the code) on your own, you are welcome to do
whatever you want to do with it without any further permission. 
Except for the following: Selling the code as is, or removing copyright statmentents and the embedded link in the code (without the http:// part) from the code. 
I don't require you to keep this whole novel, but you must keep the link which refers to where it was originally posted. And my copyright.
You must also state what you eventually have done with the original source. This obviously doesn't matter if you distribute AppleScript as read only. I do not require you to embed any properties helding copyright notice for the code when you distribute as read only.

Credit for having contributed to your product would in all cases be nice!

If you use this code as part of script of yours you are of course welcome to post that code with my code in it here at macscripter.net. If you then wish to post your code elsewhere after having uploaded it to MacScripter.net please email me and ask for permission.

The ideal situation is however that you then refer to your code by a link to MacScripter.net
The sole reason for this, is that it is so much better for all of us to have a centralized codebase which are updated, than having to roam the net to find any usable snippets. Which some of us probabaly originated in the first hand.

I'm picky about this. If I find you to have published parts of my code on any other site without previous permission, I'll do what I can to have the site remove the code, ban you, and sue you under the jurisdiction of AGDER LAGMANNSRETT of Norway. Those are the terms you silently agree too by using this code. 

The above paragraphs are also valid if you violate any of my terms.

If you use this or have advantage of this code in a professional setting, where professional setting means that you use this code to earn money by keeping yourself more productive. Or if you as an employee share the resulting script with other coworkers, enhancing the productivity of your company, then a modest donation to MacScripter.net would be appreciated.

*)
-- © McUsr 2010 and put in Public Domain see: macscripter.net/viewtopic.php?id=33758 for reference and terms of use.

on run
	-- check if text wrangler of bbedit is installed.
	-- Dette er blokka for at finder er oppe.
	tell application "Finder"
		activate
		set theSel to (get selection)
		
		set selCount to count theSel
		
		--    if selCount is 0 then my errMsg() det er alltid et finder window og det er desktop
		
		if selCount is 2 then
			set {fileA, fileB} to {item 1 of theSel, item 2 of theSel}
		else
			if selCount is greater than 3 then my errMsg()
			
			if (count its windows) is 1 or selCount is 0 then my errMsg()
			-- only the desktop window or no items in the selection, -we don't handle folders - yet?
			set fileA to item 1 of theSel
			if class of fileA is not document file then my errMsg()
			set fw1 to id of its Finder window 1
			tell its Finder window 2 to activate
			
			set theSel to (get selection) -- of Finder window 1
			
			set selCount to count theSel
			if selCount ≠ 1 then my errMsg()
			set fileB to item 1 of theSel
			if class of fileB is not document file then my errMsg()
			tell Finder window id fw1 to activate
			if contents of fileB is equal to contents of fileA then my errMsg()
		end if
	end tell
	
	-- make the differences show up in text wrangler!
	tell application "TextWrangler"
		compare (fileA as alias) against (fileB as alias)
		activate
	end tell
end run



on errMsg()
	beep
	tell me
		activate
		display dialog "You need to select two and only two two different Apple Script files in the frontmost Finder Window or one file in each of the two frontmost Finder windows in order to run this script.
" with title "AppleScriptDiff" buttons {"Ok"} default button 1 with icon file "Macintosh HD:System:Library:CoreServices:CoreTypes.bundle:Contents:Resources:Unsupported.icns"
		error number -128
	end tell
end errMsg



I’ve posted a comment in McUser’s original thread about the hard-coded drive name.

Hello! :slight_smile:

Thanks for that! I have updated the script.