AutoHotkey Community

It is currently May 27th, 2012, 12:05 pm

All times are UTC [ DST ]




Post new topic Reply to topic  [ 11 posts ] 
Author Message
PostPosted: December 5th, 2005, 2:26 am 
Offline

Joined: February 14th, 2005, 4:05 pm
Posts: 4710
Location: Boulder, CO
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
Code:
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


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: December 5th, 2005, 2:44 pm 
Offline

Joined: March 2nd, 2004, 3:36 pm
Posts: 10720
Looks great and very well documented. I expect to use it for at least one future script.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: December 7th, 2005, 4:58 pm 
Offline

Joined: August 4th, 2005, 3:56 pm
Posts: 41
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.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: December 7th, 2005, 5:42 pm 
Offline

Joined: February 14th, 2005, 4:05 pm
Posts: 4710
Location: Boulder, CO
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.


Report this post
Top
 Profile  
Reply with quote  
PostPosted: October 22nd, 2007, 2:54 pm 
Laszlo wrote:
- Save/Load list from/to a .ini file

Dear Laszlo,
This script seems very useful to me for translation work, see my post on:
http://www.autohotkey.com/forum/topic24808.html
Have you, by any chance, implemented the feature that loads a wordlist from a file? It would enable me to use large wordlists/dictionaries with this script.

Kind regards,
Jilt Dijk


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: October 22nd, 2007, 4:11 pm 
Offline

Joined: May 27th, 2007, 9:41 am
Posts: 4999
Dear Jilt,

have a look at Kollektor by Boskoop
http://www.autohotkey.com/forum/viewtopic.php?t=2534
see screenshots + links almost at the bottom of the first page

or my Kollektor++ adaptation on the second page:
http://www.autohotkey.com/forum/topic2534-15.html
which allows you to manage multiple lists and search multiple lines
of text.

Good luck!


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: October 22nd, 2007, 11:04 pm 
Offline

Joined: September 4th, 2006, 9:50 am
Posts: 24
Dear HugoV,

Thank you for your tip. I've downloaded Kollektor 03, it's indeed very much like the thing I was looking for.
I wonder if it's possible to link the word you find in the list to it's translation(s) and only paste this translation into your document instead of the searched word.
The way I'd use it now for translations is to edit Collection.txt like this

book - boek
table - tafel

insert the entry and remove the source part of it.

Jilt


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: October 23rd, 2007, 8:50 am 
Offline

Joined: May 27th, 2007, 9:41 am
Posts: 4999
Look up the line

Code:
ControlSend, %CursorIn%,{Control down}v{Control up}, AHK_ID%ActiveWindow%


in the script and insert this code just above it:


Code:
IfInString,Clipboard,- ; remove everything after this character
{
   StringGetPos, MarkerPos, Clipboard,-
   StringLeft, Clipboard, Clipboard, %MarkerPos%
}


What it does is it checks if there is a - character in the string,
and if so it removes everthing after the - character.

So you type 'boe' and you can select 'book - boek' from the
result list, but only 'book' will be pasted. With a little work you
could make it work both ways, e.g. just pressing Enter makes
it paste 'book' and pressing Shift Enter it would paste 'boek'

I hope this helps. Another script you may want to look at, which
is much more complicated but more along the lines of your orignal
request is Isense or IntelliSense

You start typing and a popup appears after 3 letters or so.
But you would need to modify those scripts to make them
work with a dictionary. (would be an interesting project for
the coming winter months)

Isense:
http://www.autohotkey.com/forum/viewtopic.php?t=12985

Intellisense is installed in the Autohotkey directory:
Program Files\AutoHotkey\Extras\Scripts\IntelliSense.ahk
you can search the forum for variations of this script as well.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: October 23rd, 2007, 1:33 pm 
Offline

Joined: September 4th, 2006, 9:50 am
Posts: 24
Thanks for the modification.
I definitely want to look into ISense later, but first I'm getting back to my translation assignment and see how this works!

Jilt


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: November 13th, 2008, 2:16 pm 
Offline

Joined: July 22nd, 2008, 1:49 pm
Posts: 151
Hi.

Thanks for making this great script, has been really usefull for me.

I just tried using it along with some gui, and that messes it up totally. Whenever i click ctrl + space i changes focus to another window. If i run your script, and then run the gui in another script the it runs just fine. Don't know what i should do to get it working, hope someone is able to point it out.

Code:
#Include CoHelper.ahk
setbatchlines, -1
Process Priority,,High                 ; little load, but need fast reaction
;Autotrim OFF

Gui, Add, Text, x36 y30 w360 h30 , Print 10 lejligheder af gangen
Gui, Add, Edit, x36 y70 w180 h20 v9,
Gui, Add, Edit, x36 y100 w180 h20 v5,
Gui, Add, Edit, x36 y130 w180 h20 v1 ,
Gui, Add, Edit, x36 y160 w180 h20 v2,
Gui, Add, Edit, x36 y190 w180 h20 v3,
Gui, Add, Edit, x36 y220 w180 h20 v4,
Gui, Add, Edit, x36 y250 w180 h20 v6,
Gui, Add, Edit, x36 y280 w180 h20 v7,
Gui, Add, Edit, x36 y310 w180 h20 v8,
Gui, Add, Edit, x36 y340 w180 h20 v10,
Gui, Show, x356 y228 h486 w498, MegaPrint
                         


; Your text pieces. Last "[" needed for no match detection & move first match to the top

Loop Q:\reception\*.xls
{
pattern = (.*)Nøgleliste
check := RegExMatch(A_LoopFileName, pattern)
if check > 0
Nøgleliste = %A_loopfilefullpath%
}

CoInitialize()
pxl := ActiveXObject("Excel.Application")
;pawb := Invoke(Invoke(pxl, "Workbooks"), "Add")
;paws := Invoke(Invoke(pxl, "Worksheets"), "Item", 1)
;pcls := Invoke(paws, "Cells")

pawb := Invoke(Invoke(pxl, "Workbooks"), "Open", "" nøgleliste "")
paws := Invoke(Invoke(pxl, "Worksheets"), "Item", 1)
pcls := Invoke(paws, "Cells")

Letter = 2
Line = 1

Loop, 600
{
openedcell := Invoke(Invoke(pcls, "Item", Line, Letter), "Value")
list = %openedcell%|%list%
Line := Line  + 1
}

list = %list%[

;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


Thanks :)


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: November 13th, 2008, 2:34 pm 
Offline

Joined: May 27th, 2007, 9:41 am
Posts: 4999
You have two guis, but you only define one so to speak. Read up on multiple GUIs http://www.autohotkey.com/docs/commands ... m#MultiWin

Add a Return below your first GUI, and you may also need to move sections of the script as it will stop after the first Gui (if you add return) and thus will not process the search ...

_________________
AHK FAQ
TF : Text files & strings lib, TF Forum


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 11 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: notsoobvious and 11 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group