Re: BBEdit Cursor Movement & Selection Shortcuts – Adapted to select paragraphs

653 views
Skip to first unread message

Christopher Stone

unread,
Aug 21, 2012, 8:45:30 AM8/21/12
to bbe...@googlegroups.com
On Aug 21, 2012, at 06:32, Pierre Igot <lat...@gmail.com> wrote:
I have other, simpler scripts that sort of work for this, but the grep-based approach is obviously the most effective. If only I could make it work...
______________________________________________________________________

Hey Pierre,

It looks to me like you've exposed a bug.  I've got a query in to support, so we'll see.

In the meantime this rather quick and dirty script works.

------------------------------------------------------------------------------------------------
try

  

  tell application "BBEdit"
    tell text of front text window
      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, extend selection:true} with selecting match
      set foundObject to found object of fRec
      properties of foundObject
      set startmarker to characterOffset of foundObject
      select (characters startmarker thru endMarker)
    end tell
  end tell

  

on error eMsg number eNum
  set {c, s} to {return, "------------------------------------------"}
  set e to s & c & "Error: " & eMsg & c & s & c & "Error Number: " & eNum & c & s
  beep
  set dDlg to display dialog e buttons {"Cancel", "Copy", "OK"} default button "OK"
  if button returned of dDlg = "Copy" then
    set the clipboard to e
  end if
end try
------------------------------------------------------------------------------------------------

--
Best Regards,
Chris

Christopher Stone

unread,
Aug 21, 2012, 2:43:41 PM8/21/12
to bbe...@googlegroups.com
On Aug 21, 2012, at 12:48, Pierre Igot <lat...@gmail.com> wrote:
It seems to be working, but I'd appreciate it if you could have a quick look and let me know if there's anything that I've misunderstood or that might need to be adjusted.
______________________________________________________________________

Hey Pierre,

I talked to Patrick at Bare Bones, and he says the backward find with extending selection not working issue is NOT a bug but a technical problem (read can-of-worms) that is unlikely to change in the near future.  Therefore a workaround is the only viable solution at this time.

This line in my first script is unneeded (I didn't finish cleaning up after writing/testing the script):

      properties of foundObject

For finding forwards I think we can simplify quite a bit:

------------------------------------------------------------------------------------------------
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

Pierre Igot

unread,
Aug 21, 2012, 3:06:08 PM8/21/12
to bbe...@googlegroups.com, listm...@suddenlink.net


On Tuesday, August 21, 2012 3:43:41 PM UTC-3, Christopher Stone wrote:
I talked to Patrick at Bare Bones, and he says the backward find with extending selection not working issue is NOT a bug but a technical problem (read can-of-worms) that is unlikely to change in the near future.  Therefore a workaround is the only viable solution at this time.

Not too surprised to hear this :). Thanks for checking.
 

This line in my first script is unneeded (I didn't finish cleaning up after writing/testing the script):

      properties of foundObject


I wondered what that was for :).
 
For finding forwards I think we can simplify quite a bit:

------------------------------------------------------------------------------------------------
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
------------------------------------------------------------------------------------------------


Works great. Thanks! 

One more thing: I use Keyboard Maestro for shortcuts, and these two scripts (Extend Selection Upwards and Extend Selection Downwards) are used repeatedly, so I am using the "XXX is down" trigger in KM instead of "XXX is pressed", so that I can press and hold the shortcut and have repeated executions of the script. This works fine with the Extend Selection Downwards script above, but with the Extend Selection Upwards script, if I press and hold the shortcut, I get a recurrence of the bug with each selection extension deselecting the current selection. OTOH, if I press repeatedly (releasing the key between each press), the extension works as intended.

Strange. Any idea what might be causing this? 

Pierre

Pierre Igot

unread,
Aug 21, 2012, 3:10:29 PM8/21/12
to bbe...@googlegroups.com, listm...@suddenlink.net
I should add: I use Keyboard Maestro for this because BBEdit does not allow me to assign the option-shift-Up and option-shift-Down shortcuts to scripts. If I use BBEdit's shortcuts pref pane to assign other shortcuts that BBEdit supports, the script repeating works properly in both directions. So it's a problem between BBEdit, AppleScript, and Keyboard Maestro.

But since I am used to these shortcuts elsewhere, I'd very much like to be able to use them in BBEdit as well. Hence the need for KM.

Pierre

Christopher Stone

unread,
Aug 21, 2012, 4:27:06 PM8/21/12
to bbe...@googlegroups.com
On Aug 21, 2012, at 14:10, Pierre Igot <lat...@gmail.com> wrote:
I should add: I use Keyboard Maestro for this because BBEdit does not allow me to assign the option-shift-Up and option-shift-Down shortcuts to scripts.
______________________________________________________________________

Hey Pierre,

Okay.  I'm glad you mentioned this, since it's very germane to the problem.

This slightly modified script should fix the find-backwards problem.

I assume you have the Keyboard Maestro shortcut set to 'Key Pressed' - yes?

------------------------------------------------------------------------------------------------
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

Pierre Igot

unread,
Aug 21, 2012, 5:13:05 PM8/21/12
to bbe...@googlegroups.com, listm...@suddenlink.net


On Tuesday, August 21, 2012 5:27:06 PM UTC-3, Christopher Stone wrote:
Okay.  I'm glad you mentioned this, since it's very germane to the problem.

This slightly modified script should fix the find-backwards problem.

I assume you have the Keyboard Maestro shortcut set to 'Key Pressed' - yes?

No, if I use "is pressed", there is no repeating at all. In order to get the repeating to work, I need to use "is down".
 

------------------------------------------------------------------------------------------------
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
------------------------------------------------------------------------------------------------


Woah. All kinds of funky stuff happening now. It *sort of* works, i.e. the current selection no longer gets deselected when the shortcut is held down to repeat, but there are all kinds of hiccups in the repetition, and sometimes BBEdit gets very confused and extends the selection downwards by a line or two, or extends upwards and then removes what's just been added to the selection, and I also get system beeps. Very strange. 

I tried inserting a small pause in KM after the execution, but that didn't make any difference.

I also asked KM to display the results of the AppleScript script and during the hiccups I get:

/usr/bin/osascript: couldn't save changes to script /Users/igot/Library/Application Support/BBEdit/Scripts/Extend Up - Grep.scpt: error -54.

Not sure what it all means. (On the positive side, there is no longer any "flickering" of the selection as there was with earlier versions of the script.) 

I realize I am probably nit-picking at this point (the script works fine with repeated keystrokes, only has problems when pressing and holding when using Keyboard Maestro), but since we are trying to replicate the standard paragraph selection of other OS X applications with the same standard shortcuts, we might as well try to go all the way :).

I find it rather strange that the simple addition of the IF... THEN... condition would create this new problem, but it obviously has to do with some weird interaction between AppleScript, BBEdit and Keyboard Maestro. I don't know that "error -54" is, but obviously this form of execution is causing OS X to try and save something internal in the script. (Out of curiosity, I tried saving the script as an app, and asking KM to execute that instead, but it doesn't change anything.)

Any further thoughts?

Pierre
Reply all
Reply to author
Forward
0 new messages