Tuesday, January 23, 2018
  • Index
  •  » Code Exchange
  •  » Show differences between two "Normal" text files with TextWrangler

#1 2010-08-03 10:30:20 am

McUsr
Member
From:: Southern Norway
Registered: 2010-04-07
Posts: 1776

Show differences between two "Normal" text files with TextWrangler

Hello.

This is a spinoff of a script for showing the differences among two applescripts, wether in script format or as applescript text.

Come to think of it, this is a little bit easier than dragging the files into the FileMerge tray. Therefore this.

Applescript:


-- 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.
-- macscripter.net/viewtopic.php?pid=131490#p131490
(*
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?pid=131490#p131490 for reference and terms of use.
property UnsuppIcon : a reference to file ((path to library folder from system domain as text) & "CoreServices:CoreTypes.bundle:Contents:Resources:Unsupported.icns")
on run
   -- check if text wrangler of bbedit is installed.
   
   tell application "Finder"
       
       set theSel to (get selection)
       
       set selCount to count theSel
       
       
       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 a reference to 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 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!
   try
       set myres to (do shell script "/usr/bin/twdiff --ignore-spaces " & quoted form of POSIX path of (fileA as alias) & " " & quoted form of POSIX path of (fileB as alias))
       tell me
           activate
           display alert "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 Text files in the frontmost Finder Window or one Text file in each of the two frontmost Finder windows in order to run this script.
"
with title "TextFileDiff" buttons {"Ok"} default button 1 with icon UnsuppIcon
       error number -128
   end tell
end errMsg

Last edited by McUsr (2012-09-13 06:43:35 am)


Mercurial vcs is a joy to use for scripting.


Filed under: Diff TextFiles

Offline

 

#2 2010-08-04 06:42:41 am

McUsr
Member
From:: Southern Norway
Registered: 2010-04-07
Posts: 1776

Re: Show differences between two "Normal" text files with TextWrangler

Hello.

I have updated TextFileDiff to take files from the two frontmost finder windows if there were only one file selected in the fronmost Finder Window.

Please check that your harddisk name is "Macintosh Hd" if it's not then you will have to edit the path to the icons file in the errMsg() dialog in order for the dialog to show up when it should!


Mercurial vcs is a joy to use for scripting.


Filed under: TextFileDiff

Offline

 

#3 2013-11-20 08:37:32 am

McUsrII
Member
Registered: 2012-11-21
Posts: 3046
Website

Re: Show differences between two "Normal" text files with TextWrangler

Hello.

I learned a long time ago, how I'd use FileMerge as a commandline tool by the opendiff command, so here is a version, that still takes two files from either the same, or the two frontmost Finder Windows, and runs them through FileMerge.

You need to have Developer tools installed in order to run this version.

Applescript:

-- 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.
-- macscripter.net/viewtopic.php?pid=131490#p131490
# Revised 2013/11/20
-- Revised 13/09/12
-- © McUsr 2010 and put in Public Domain see: macscripter.net/viewtopic.php?pid=131490#p131490 for reference and terms of use.
property UnsuppIcon : a reference to file ((path to library folder from system domain as text) & "CoreServices:CoreTypes.bundle:Contents:Resources:Unsupported.icns")
on run
   -- check if text wrangler of bbedit is installed.
   
   tell application "Finder"
       
       set theSel to (get selection)
       
       set selCount to count theSel
       
       
       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 a reference to 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 fw1 to activate
           if contents of fileB is equal to contents of fileA then my errMsg()
       end if
   end tell

   try
       set myres to (do shell script "/usr/bin/opendiff " & quoted form of POSIX path of (fileA as alias) & " " & quoted form of POSIX path of (fileB as alias))
   on error e number n
   end try
end run

on errMsg()
   beep
   tell me
       activate
       display dialog "You need to select two and only two two different Text files in the frontmost Finder Window or one Text file in each of the two frontmost Finder windows in order to run this script.
"
with title "TextFileDiff" buttons {"Ok"} default button 1 with icon UnsuppIcon
       error number -128
   end tell
end errMsg

Last edited by McUsrII (2013-12-17 11:23:10 pm)

Offline

 
  • Index
  •  » Code Exchange
  •  » Show differences between two "Normal" text files with TextWrangler

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)