Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save mixio/7c03838a95c3cf86cfd78827bf7121d5 to your computer and use it in GitHub Desktop.
Save mixio/7c03838a95c3cf86cfd78827bf7121d5 to your computer and use it in GitHub Desktop.
BBEdit scripts to diff two front windows' selections
(*- Local Variables: -*)
(*- coding: utf-16le -*)
(*- End: -*)
(* ‼️ Force Script Editor to UTF-16LE ‼️ *)
--
(*
FILENAME:
0-[⌘…]-[compare_extracts]-[1]-[open_differences_window].applescript
README:
This script:
• extracts the selection of the two frontmost windows to two new temporary documents;
(NOTE: To compare two extracts of the same file use the "Open In Additional Window" in the Sidebar local menu of your file
and select the second extract in the Additional Window.)
• names the temporary documents with the name of the original documents with identification metadata appended;
• opens a differences window allowing to compares and diff those two temporary documents.
When you are done comparing the two extracts you can apply the changes to the original documents
with the companion script "0-[⌘…]-[compare_extracts]-[2]-[apply_changes].applescript"
INSTALL
Copy the script to BBEdit's Scripts folder : ~/Library/Application Support/BBEdit/Scripts
*)
tell application "BBEdit"
set vWindowA to its first window
set vWindowB to its second window
set vDocumentIdA to ID of first document of vWindowA
set vDocumentIdB to ID of first document of vWindowB
tell document id vDocumentIdA
set vNameA to (its name) as string
set vSelectionA to selection of vWindowA
set vLengthA to (length of vSelectionA) as integer
if vLengthA = 0 then
error "Nothing is selected in document: '" & vNameA & "'."
end if
set vContentsA to (text of vSelectionA) as string
set vStartA to (characterOffset of vSelectionA) as integer
set vEndA to vStartA + vLengthA - 1
end tell
tell document id vDocumentIdB
set vNameB to (its name) as string
set vSelectionB to selection of vWindowB
set vLengthB to (length of vSelectionB) as integer
if vLengthB = 0 then
error "Nothing is selected in document: '" & vNameB & "'."
end if
set vContentsB to (contents of vSelectionB) as string
set vStartB to (characterOffset of vSelectionB) as integer
set vEndB to vStartB + vLengthB - 1
end tell
if vDocumentIdA = vDocumentIdB and vStartA ≤ vEndB and vStartB ≤ vEndA then
error "You are using an Additional Window to the same document and the choosen selections in the two windows overlap."
end if
set vDocumentExtractA to make document with properties {contents:vContentsA}
set vDocumentExtractIdA to ID of vDocumentExtractA
set name of document id vDocumentExtractIdA to "¦" & vNameA & "¦" & vDocumentIdA & "," & vDocumentExtractIdA & "," & vStartA & "," & vEndA & "¦"
set vDocumentExtractB to make document with properties {contents:vContentsB}
set vDocumentExtractIdB to ID of vDocumentExtractB
set name of document id vDocumentExtractIdB to "¦" & vNameB & "¦" & vDocumentIdB & "," & vDocumentExtractIdB & "," & vStartB & "," & vEndB & "¦"
set vCompareResults to compare document id vDocumentExtractIdA against document id vDocumentExtractIdB options {ignore RCS keywords:true}
end tell
(*- Local Variables: -*)
(*- coding: utf-16le -*)
(*- End: -*)
(* ‼️ Force Script Editor to UTF-16LE ‼️ *)
--
(*
FILENAME:
0-[⌘…]-[compare_extracts]-[2]-[apply_changes].applescript
README:
This script is the companion script of script "0-[⌘…]-[compare_extracts]-[1]-[open_differences_window].applescript"
Once you are finished comparing the extracts it will allow you to apply the changes to the original document(s).
It will:
• ask confirmation before applying the changes;
• parse the identification metadata contained in the differences window name;
• apply the changes to the respective original document(s);
• close the temporary documents windows;
• close the differences window.
INSTALL
Copy the script to BBEdit's Scripts folder : ~/Library/Application Support/BBEdit/Scripts
*)
tell application "BBEdit"
if class of first window is not differences window then
error "This script can only be applied to a Differences Window."
end if
set vDifferencesWindowId to (ID of first window) as integer
set vName to (name of first window) as string
set {vASDelimiters, AppleScript's text item delimiters} to {AppleScript's text item delimiters, "¦"}
set vParts to text items of vName
set AppleScript's text item delimiters to vASDelimiters
-- log vName
--> "Differences — ¦A.txt¦44,85,2,25¦ vs. ¦B.txt¦41,88,345,365¦"
set {vDiff, vNameA, vIdsA, vSep, vNameB, vIdsB} to vParts
set {vDocumentIdA, vDocumentExtractIdA, vStartA, vEndA} to run script ("{" & vIdsA & "}")
set {vDocumentIdB, vDocumentExtractIdB, vStartB, vEndB} to run script ("{" & vIdsB & "}")
try
set vMessage to "Do you want to apply the changes to the original files:" & linefeed
set vMessage to vMessage & tab & "'" & vNameA & "'" & linefeed
set vMessage to vMessage & tab & "'" & vNameB & "'"
set vDefaultButton to "Apply changes"
set vResult to display dialog vMessage buttons {"Cancel", "Don't apply changes", vDefaultButton} default button vDefaultButton
on error aMessage number aErrorNumber
if aErrorNumber = -128 then -- User cancelled.
return
end if
error aMessage number aErrorNumber
end try
if (vResult's button returned) = vDefaultButton then
tell document id vDocumentExtractIdA
set vExtractA to its text as string
end tell
tell document id vDocumentExtractIdB
set vExtractB to its text as string
end tell
if vStartA < vStartB then -- In case of same source document, update the bottom selection first.
tell document id vDocumentIdB
set its characters vStartB thru vEndB to vExtractB
end tell
tell document id vDocumentIdA
set its characters vStartA thru vEndA to vExtractA
end tell
else
tell document id vDocumentIdA
set its characters vStartA thru vEndA to vExtractA
end tell
tell document id vDocumentIdB
set its characters vStartB thru vEndB to vExtractB
end tell
end if
end if
close document id vDocumentExtractIdA saving no
close document id vDocumentExtractIdB saving no
close window id vDifferencesWindowId
end tell
@mixio
Copy link
Author

mixio commented Dec 8, 2024

Updated the scripts so they will work between a document's window and the same document's Additional Window.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment