AppleScript scripts for paragraph selection in BBEdit (continued)

Posted by Pierre Igot in: Macintosh
August 22nd, 2012 • 2:42 pm

Last week, I wrote about the use of AppleScript scripts to provide commands for paragraph selection in BBEdit, where the usual shortcuts do not work. As indicated in my post, I was able to adapt scripts provided by Oliver Taylor to create rudimentary commands for paragraph selection. But I had a problem with the command for extending the paragraph selection upwards, where each selection extension upwards would cause the existing selection to become deselected. In addition, both scripts suffered from an inability to select the very first / very last paragraph in the document, because of the absence of a return character at the very beginning and at the very end.

Earlier this week, I sent a tweet to Oliver Taylor and received a reply. Simultaneously, I started a thread on the BBEdit mailing list about the issue I was encountering.

BBEdit user Christopher Stone was kind enough to offer his help and I am glad to report that I now have revised scripts that work almost perfectly for extending the selection paragraph by paragraph upwards and downwards.

It turns out that the problem I had with my script for extending the selection upwards was due to a bug, but one that will probably not get fixed any time soon. So Chris had to come up with a workaround and did. The final script is as follows:

tell application "BBEdit"
	tell text of front text window
		if (characterOffset of selection) > 1 then
			set endMarker to (characterOffset of selection) + (length of selection) - 1
			set fRec to find "[^\\r]+" options {search mode:grep, starting at top:false, wrap around:false, backwards:true, case sensitive:false, match words:false} with extend selection
			set startmarker to characterOffset of fRec's found object
			select (characters startmarker thru endMarker)
		end if
	end tell
end tell

For extending the selection downward, the script is simpler:

tell application "BBEdit"
	tell text of front text window
		find "[^\\r]+" options {search mode:grep, starting at top:false, wrap around:false, backwards:false, case sensitive:false, match words:false, extend selection:true} with selecting match
	end tell
end tell

As you can see, these scripts still use grep, but no longer make use of Perl-like “Look-ahead” and “Look-behind” assertions.

Since BBEdit refuses to let me assign the standard shift-option-Up and shift-option-Down shortcuts to my scripts, I use Keyboard Maestro to bypass BBEdit’s limitations:

km-bbedit-extendup

Et voilà! I now have a more or less standard behaviour for these two text selection shortcuts in BBEdit.

There is only one outstanding issue, with the macro for extending the selection upwards. As you can see in the screen shot above, the trigger for the macro has to be shift-option-Upis down” and not “is pressed”. This is required if you want to be able to use key repeat to trigger multiple occurrences of the script, which is a common requirement when you want to select a bunch of paragraphs.

Unfortunately, on my machine at least, the key repeat behaviour for this particular macro is problematic. The key repeat sort of works, but is very choppy, with occasional selection errors. I cannot reproduce the problem when I assign an internal keyboard shortcut (other than shift-option-Up) in BBEdit itself, so the problem only affects the combination of BBEdit, AppleScript and Keyboard Maestro. And Christopher Stone is unable to reproduce the problem on his machine (and I also cannot reproduce it in a separate user environment on my machine with no customizations), so it has to be something specific to my user environment.

I will have to investigate this further, but until then, I can already use the scripts in their current form, as long as I use repeated key strokes for the shortcut for extending upwards instead of one continuous key stroke with key repeat. If you miss the standard OS X paragraph selection shortcuts in BBEdit, you might want to give this solution a try.

PS. I should also note that another reader, Yoto Yotov, also offered the following two scripts:

tell application "BBEdit"
	set SelStart to characterOffset of selection
	if SelStart is not 1 then
		set SelStart to SelStart - 1
		set SelEnd to SelStart + (length of selection)
		tell front text window
			repeat while ((SelStart > 1) and (character (SelStart - 1) as string is not return))
				set SelStart to SelStart - 1
			end repeat
			select (characters (SelStart) thru (SelEnd))
		end tell
	end if
end tell

and

tell application "BBEdit"
	set SelStart to characterOffset of selection
	set SelEnd to SelStart + (length of selection)
	tell front text window
		set TextLength to number of characters
		if SelEnd is not TextLength + 1 then
			repeat while ((SelEnd < TextLength) and (character SelEnd as string is not return))
				set SelEnd to SelEnd + 1
			end repeat
			select (characters (SelStart) thru (SelEnd))
		end if
	end tell
end tell

But these scripts don’t use grep and, because of this, they are slower and don’t work as smoothly as grep-based scripts.


One Response to “AppleScript scripts for paragraph selection in BBEdit (continued)”

  1. Betalogue » AppleScript scripts for paragraph navigation and selection in BBEdit says:

    [...] AppleScript scripts for paragraph selection in BBEdit (continued) [...]