Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

Incremental search for text to send


  • Please log in to reply
10 replies to this topic
Laszlo
  • Moderators
  • 4713 posts
  • Last active: Mar 31 2012 03:17 AM
  • Joined: 14 Feb 2005
This short script sends predefined pieces of text found by the beginnings of their first word.
It is based on the built-in incremental search functionality of ListBoxes, so its features list longer than the script itself.

The text-pieces are delimited by pipe, "|", can contain spaces, special characters.
The last entry has to be "[" for technical reasons.

When the Ctrl-Space HotKey is hit, the script takes the (partial) word left to the insertion point and looks in the user-defined List if there are continuations.
- If there is none, nothing happens, otherwise a ListBox pops up with the first match highlighted.
Usually there is just one match, so keeping the Ctrl key pressed & hitting the space bar again sends the selected text to the current window (the most convenient way, but Enter works, too).

If you keep typing, an incremental search highlights always the first match, while the original window shows the typed text, which is being used to search for the long pieces of text. Here you can type space and other characters, too, which would delimit the initial word.
If there is no more match, the search aborts, leaving the typed search text intact.

Up, Down and Mouse clicks change the selection in the ListBox.
Mouse click on Scroll bar or the mouse wheel scrolls the text.
Double click on an entry in the list box sends that text to the window, erasing the text typed after the last Ctrl-Space.

Ctrl-Space:
1. No abbreviation defined: no effect.
2. There are abbreviations: pop up GUI, first match selected in a ListBox.
-- a. 2nd Ctrl-Space or Enter: Send selected text from List, replace typed text
-- b. Esc or TAB: Return, no text insertion
-- c. Keep typing: Select incremental matches, keys echoed in original window.
---- If there is no more match, exit
-- d. BackSpace:
---- If no more key left since Ctrl-Space, exit
---- Delete keys typed since Ctrl-Space activation, update selection
-- e. Up, Down, Mouse click: change selection
-- f. Double click sends the corresponding text

Limitations:
- Only works in applications using Shift-Ctrl-Left to select the word left of the insertion point
- Ctrl-X/Send to cut/paste might have side effects (change indentation, paragraph format...)
- Changing the active window makes text sent there

ToDo:
- Replace GoTo's with functions, for the purists
- Suspend while other windows are active
- Save/Load list from/to a .ini file
- Dynamically Add/Remove entries from the list
- Movable, resizable GUI
- Save/Load GUI position
Process Priority,,High                 ; little load, but need fast reaction
Autotrim OFF                           ; Space can be appended

; Your text pieces. Last "[" needed for no match detection & move first match to the top
List = Péter-Pál|South America|South Carolina|South Dakota|South Pole*|Southern|Souter, David (American jurist & statesman)|time|XEROX|Zeppelin|####|[

Gui -Caption AlwaysOnTop
Gui Margin, 2,2
Gui Add, ListBox, x2 y2 r5 w180 Sort vChoice gClicked, %List%
Return

^Space::                               ; Search/send-text HotKey
   ek =
   ClipBoard =                         ; empty the ClipBoard
   Send ^+{Left}^x                     ; cut out word on the left
   ClipWait 2                          ; wait until it gets to the ClipBoard
   c = %ClipBoard%                     ; shorthand
   SendRaw %c%                         ; send word back (^v has side effects)
   Gui Show, x0 y200                   ; show GUI: <-- your favorite location!
   WinGet GuiID, ID, A                 ; ID of the GUI for ControlSend below
   Send !{TAB}                         ; back to caller
   Loop
   {                                   ; key from the previous Loop iteration
      If ek in Up,Down                 ; navigate in the ListBox
         ControlSend ListBox1, {%ek%}, ahk_id %GuiID%
      Else {                           ; select bottom entry, then abbreviation
         GuiControl ChooseString,Choice,[    ;... to show it on top
         GuiControl ChooseString,Choice,%c%
         GuiControlGet Choice          ; get selected
         If Choice = [                 ; still on "[" if no match
            GoTo OUT                   ; exit
      }
      Input k, I L1 M, {Enter}{Esc}{TAB}{BS}{Up}{Down}
      StringTrimLeft ek, ErrorLevel, 7 ; Remove "Endkey:"
      If ek in Escape,TAB
         GoTo OUT                      ; exit
      If ek = Enter
         GoTo SendText                 ; send full text, exit
      If (k = " " and GetKeyState("Ctrl"))
         GoTo SendText                 ; send full text, exit
      If ek = Backspace
      {
         IfEqual c,, GoTo Out          ; all have been deleted
         StringTrimRight c,c,1         ; remove last char
         Send {BS}                     ; delete last char in application window
      }
      Send %k%                         ; echo key in application window
      c = %c%%k%                       ; incremental search
  }

Clicked:                               ; @ mouse click in the ListBox
   IfNotEqual A_GuiControlEvent,DoubleClick, Return
SendText:
   Gui Submit
   Send % "{BS " StrLen(c) "}"         ; remove typed abbreviation
   SendRaw %Choice%                    ; send full text
OUT:
   Gui Show, Hide                      ; hide GUI
Return


Chris
  • Administrators
  • 10727 posts
  • Last active:
  • Joined: 02 Mar 2004
Looks great and very well documented. I expect to use it for at least one future script.

Harrie
  • Members
  • 41 posts
  • Last active: Dec 13 2007 04:25 PM
  • Joined: 04 Aug 2005
Very beautiful from the end-user's point of view indeed! Great script! Why, how perfect for putting in a whole list of doctor names or the like. Just tried it out by putting several "Dr. So-and-So's in the script. Might change it around and leave the "Dr." out of the script since MS Word wants to see a period as a word. Truly looking forward to experimenting with this jewel.

Laszlo
  • Moderators
  • 4713 posts
  • Last active: Mar 31 2012 03:17 AM
  • Joined: 14 Feb 2005
Thanks for the compliments! If all of your abbreviations end with a period or comma, or you always use more than one letter abbreviations, you could modify the script (4th line of the hotkey subroutine) to send +{Left}^+{Left}^x instead. Or, check if the ClipBoard contains a punctuation mark only, and than do this longer selection.