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
(*
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;
• 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 vDocumentIdA to ID of first document of first window
set vDocumentIdB to ID of first document of second window
if vDocumentIdA = vDocumentIdB then
error "The documents in the first window and second window are the same.\nChoose different documents in each window."
end if
tell document id vDocumentIdA
set vNameA to (its name) as string
set vSelectionA to selection of its window
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 its window
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
-- log {vContentsA, vContentsB}
-- log {vNameA, vNameB}
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
(*
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 documents.
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 documents;
• 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
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
-- log {VDocumentIdA, vDocumentExtractIdA, VDocumentIdB, vDocumentExtractIdB}
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
-- log {vExtractA, vExtractB}
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
close document id vDocumentExtractIdA saving no
close document id vDocumentExtractIdB saving no
close window id vDifferencesWindowId
end tell
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment