Skip to content

Instantly share code, notes, and snippets.

@mixio
Last active March 15, 2022 09:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mixio/0cc693aa710e42ebae2a6094141b2971 to your computer and use it in GitHub Desktop.
Save mixio/0cc693aa710e42ebae2a6094141b2971 to your computer and use it in GitHub Desktop.
BBEdit script to select inner contents of Nth tag of front document
(*
FILENAME:
select_contents_of_nth_tag.scpt
README:
This script:
• asks for a "tag#index" pair separated by a '#' hash mark:
- "tag" is the name of the seeked tag,
- "index" is the number of the seeked tag;
• selects the inner contents of the choosen tag in BBEdit's front document;
For example "Session#200" will look for inner contents of the 200th <Session>...</Session> tag.
INSTALL
Copy the script to BBEdit's Scripts folder : ~/Library/Application Support/BBEdit/Scripts
*)
property pTagName : "Session"
property pIndex : 200
try
try
set vResult to display dialog "Choose a \"tag#index\" pair." default answer (my pTagName) & "#" & (my pIndex) buttons {"Cancel", "Ok"} default button "Ok"
on error aMessage number aErrorNumber
if aErrorNumber = -128 then -- User cancelled.
return
else
error aMessage number aErrorNumber
end if
end try
if not (vResult's button returned) is "Ok" then
error "Unsupported button \"" & vResult's button returned & "\"."
end if
set {vDelimiters, AppleScript's text item delimiters} to {AppleScript's text item delimiters, "#"}
set vParts to text items of vResult's text returned
set AppleScript's text item delimiters to vDelimiters
if (length of vParts) ≠ 2 then
error "Specify a tag name and index separated by a hash mark '#'. (i.e., \"Session#200\")"
end if
set {vTagName, vIndex} to vParts
set my pTagName to (first word of vTagName) as string
set my pIndex to vIndex as integer
set vParts to 0
tell application "BBEdit"
set vTag to "<" & (my pTagName) & ">"
set vEndTag to "</" & (my pTagName) & ">"
set vDocument to first text document of first window
ignoring case
-- Find all the closing tags.
set vOptions to {starting at top:true, wrap around:false, returning results:true, search mode:literal, case sensitive:false}
set vResult to find vEndTag searching in vDocument options vOptions
if not vResult's found then
error "BBedit's front document has no closing " & vEndTag & " tags."
end if
set vMatches to vResult's found matches
set vMatchesCount to (length of vMatches) as integer
if (my pIndex) > vMatchesCount then
error "BBedit's front document has less than " & (my pIndex) & " " & vTag & " tags."
end if
-- Got the Nth closing tag.
set vNthClosingTag to item (my pIndex) of vMatches
set vEndOffset to ((vNthClosingTag's start_offset) as integer) - 1
set vResult to 0
set vMatches to 0
select insertion point after character vEndOffset of vDocument
end ignoring
balance tags
end tell
on error aMessage
display alert aMessage
end try
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment