Sunday, November 19, 2017

#26 2013-11-26 11:14:06 am

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

Re: Show differences in two AppleScript files with TextWrangler

Hello.

This is a version that shows the differences between two AppleScript files with FileMerge. Maybe I should have coalesced the two into one script, but then you'd have to do more than simple executing the script.

I did the same to the Show Differences in two "normal" text files with TextWrangler; added a script at the end that used FileMerge as well.

I feel more comfortable by using FileMerge, when the whole differences thing is for viewing purposes. smile

Applescript:



-- © McUsr 2013 and put in Public Domain see: macscripter.net/viewtopic.php?id=33758 (post #26) for reference and terms of use.

(*
    Copyright © 2010 - 2015 McUsr: Fixed a bug in the classifyASfile() handler.
    You may not post this as a work on your own somewhere, including paper.
*)

on run
   tell application "Finder"
       activate
       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 and class of fileA is not application 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 2
           
           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 and class of fileB is not application file then my errMsg()
           tell fw1 to activate
           if contents of fileB is equal to contents of fileA then my errMsg()
       end if
       
       
       set {mustDecompileFileA, mustDecompileFileB} to {my classifyASfile(fileA), my classifyASfile(fileB)}
       -- 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)))
       
       if mustDecompileFileA then
           my decompileAppleScriptTextForDiff((fileA as alias), tempFn1)
       else
           try
               do shell script "cp " & quoted form of POSIX path of (fileA as alias) & " " & tempFn1
           end try
       end if
       if mustDecompileFileB then
           my decompileAppleScriptTextForDiff((fileB as alias), tempFn2)
       else
           try
               do shell script "cp " & quoted form of POSIX path of (fileB as alias) & " " & tempFn2
           end try
       end if
   end tell
   try
       do shell script "(/usr/bin/opendiff " & tempFn1 & " " & tempFn2 & " ) &> /dev/null &"
   end try
end run

on classifyASfile(aFileRef)
   tell application "Finder"
       if file type of aFileRef is "osas" then return true
   end tell
   set aFileRef to aFileRef as alias
   
   tell application id "sevs"
       set ftype to type identifier of (get properties of item (aFileRef as text))
       if ftype contains "com.apple.applescript.script" then
           -- works for "com.apple.applescript.script" and "com.apple.applescript.script-bundle"
           return true
       else
           return false
       end if
   end tell
end classifyASfile

on errMsg()
   beep
   tell (path to frontmost application as text)
       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. If you are trying to get diffs of applets those need to have the original filepropeties as when the applet was saved in the editor in order for the script to run properly.
"
with title "AppleScriptDiff" buttons {"Ok"} default button 1
   end tell
   error number -128
end errMsg

on decompileAppleScriptTextForDiff(fileAlias, qpxTempFileName) -- Thanks to oldmanegan
   
   local theSourceText, qfPoxPath
   set qfPoxPath to quoted form of POSIX path of (fileAlias)
   try
       fileAlias as alias
   on error e number n
       set bad to true
   end try
   try
       beep
       do shell script "/usr/bin/osadecompile " & qfPoxPath & "| /usr/bin/tr -d '\\000' >" & qpxTempFileName
   on error e number n
       tell application (path to frontmost application as text)
           display dialog "AppleScriptDiff: decompileAppleScriptTextForDiff()" & e & " : " & n with title "AppleScriptDiff" buttons {"Ok"} default button 1
       end tell
       error number -128
   end try
end decompileAppleScriptTextForDiff

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 items 1 thru -2 of hfSfileNameAsText as text
   set AppleScript's text item delimiters to tids
   return hfSfileNameAsText
end createPathDenotingFileNameWitoutExt

Last edited by McUsrII (2015-04-07 10:52:55 am)


Filed under: filemerge, Opendiff

Offline

 

#27 2015-04-07 10:44:04 am

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

Re: Show differences in two AppleScript files with TextWrangler

Removed a bug in the Script in post #26 The bug wouldn't kick in, until you tried to compare something different than a file with a filetype of "osas", -it took five years before I did just that, and realized, there were a missing handler in there. The issue is fixed.

Last edited by McUsrII (2015-04-07 10:45:15 am)

Offline

 

#28 2015-04-07 10:53:50 am

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

Re: Show differences in two AppleScript files with TextWrangler

Hello.

I have updated the almost identical script that shows differences in TextWrangler as well (post #24)

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)