The Script:
|
property ParNo : 1
global TheContents, ParCount, CurrentParagraphInfo
tell application "BBEdit 6.0"
if not (exists window 1) then
my ErrMsg("No open document!", {"Oops!"}, 1)
else
if (class of window 1 is not text window) then
my ErrMsg("The front window must be a text document!", {"Oops!"}, 1)
else
try
set TheContents to contents of text window 1 -- get the contents of the frontmost doc
if (count TheContents) is 0 then
my ErrMsg("The front window must contain some text in order to use this script!", {"OK"}, 1)
else
set ParCount to count paragraphs of TheContents -- get the total number of paragraphs
set CurrentParagraphInfo to my GetCurrentParagraphRange()
my ChooseParagraphNo()
end if
on error ErrTxt
if ErrTxt ends with "Can't get every text of window 1." then
set ErrTxt to "The front window must contain some text in order to use this script!"
end if
my ErrMsg(ErrTxt, {"OK"}, 1)
end try
end if
end if
end tell
on ChooseParagraphNo()
if ParNo > ParCount then set ParNo to 1 -- reset value of property ParNo if new doc contains less paragraphs than ParNo
try -- get the desired paragraph number:
set NewParNo to text returned of (display dialog (CurrentParagraphInfo & return & return & ¬
"Select paragraph no.:" & return & "(max: " & ParCount & ")") with icon note default answer ParNo) as number
-- check for valid user entry (i.e. an integer between 1 and 'total number of paragraphs'):
if (NewParNo ≥ 1) and (NewParNo ≤ ParCount) then
set NewParNo to NewParNo as integer
set ParNo to NewParNo
my SelectParagraph() -- all is fine, do action
else
error "Number out of range!"
end if
on error ErrTxt number errNo
if (errNo is -1700) or (errNo is -2700) then -- "Can't make ... into a number" (-1700) or "Number out of range!" (-2700)
my ErrMsg(("You have to enter a number from" & return & "1 to " & ParCount & "!"), {"Cancel", "Try again"}, 2)
if button returned of result is "Try again" then my ChooseParagraphNo()
else if (errNo is not -128) then
my ErrMsg(("The following error has occurred:" & return & "\"" & ErrTxt & "\"" & return & "(Number: " & errNo & ")"), {"OK"}, 1)
end if
end try
end ChooseParagraphNo
on SelectParagraph()
set oldDelims to AppleScript's text item delimiters
set AppleScript's text item delimiters to return
if (ParNo = 1) then -- first paragraph
tell window 1 of application "BBEdit 6.0" to select (lines 1 thru 1)
else -- get the offsets of the first and last char within the specified paragraph:
set SelStart to (length of ((paragraphs 1 thru (ParNo - 1) of TheContents) as string))
set SelEnd to ((length of ((paragraphs 1 thru ParNo of TheContents) as string)) + 1)
if (SelStart + 2) ≤ SelEnd then
set SelStart to SelStart + 2
else if (SelStart + 1) ≤ SelEnd then
set SelStart to SelStart + 1
end if
-- finally, select the paragraph:
tell window 1 of application "BBEdit 6.0" to select (characters SelStart thru SelEnd)
end if
set AppleScript's text item delimiters to oldDelims
end SelectParagraph
on GetCurrentParagraphRange() -- gets the paragraph no. of the current selection (in the frontmost BBEdit 6.x text window)
tell application "BBEdit 6.0"
tell text window 1
set Sel to selection
set SelLength to length of Sel
set SelOffset to offset of Sel
set CharsUptoSelStart to characters 1 thru SelOffset
if SelLength > 0 then -- some text is selected (i.e., length of selection is not 0)
if (last character of Sel as string = return) then -- strip a trailing return from the selection
set CharsUptoSelEnd to characters 1 thru (SelOffset + SelLength - 2)
else
set CharsUptoSelEnd to characters 1 thru (SelOffset + SelLength - 1)
end if
if (first character of Sel as string = return) then
set ParNoSelStart to (count paragraphs of (CharsUptoSelStart as string)) - 1
else
set ParNoSelStart to count paragraphs of (CharsUptoSelStart as string)
end if
set ParNoSelEnd to count paragraphs of (CharsUptoSelEnd as string)
else -- there's no text selected (i.e., length of selection is 0) -> insertion point!
if (character SelOffset as string = return) then
set CharsUptoSelStart to characters 1 thru -2 of CharsUptoSelStart
end if
set ParNoSelStart to count paragraphs of (CharsUptoSelStart as string)
set ParNoSelEnd to ParNoSelStart
end if
end tell
if (ParNoSelStart = ParNoSelEnd) and (SelLength = 0) then
set CurrentParagraphInfo to "Current insertion point is within paragraph no. " & (ParNoSelStart as string) & "."
else if (ParNoSelStart = ParNoSelEnd) and (SelLength ≠ 0) then
set CurrentParagraphInfo to "Current selection is at paragraph no. " & (ParNoSelStart as string) & "."
else if (ParNoSelStart ≠ ParNoSelEnd) and (SelLength ≠ 0) then
set CurrentParagraphInfo to "Current selection spans paragraphs " & (ParNoSelStart as string) & " thru " & (ParNoSelEnd as string) & "."
end if
return CurrentParagraphInfo
end tell
end GetCurrentParagraphRange
on ErrMsg(ErrText, ButtonList, DefaultButton) -- handles error messages:
display dialog ErrText with icon caution buttons ButtonList default button DefaultButton
end ErrMsg
|