Hotkey to select next instance of text Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
espla0

Hotkey to select next instance of text  Topic is solved

18 Nov 2016, 15:30

I'm not exactly a pro with autohotkey, so I apologize if this is either obvious or impossible:

Is there any way to build a script that would allow me to select the next instance of a certain string of text (i.e. highlight the textso that I can type and replace it with something else)?

I want to create a hotkey that would allow me to select three asterisks in a row: ***

It would be very handy in filling out templates like this:

*** is a *** year old male with a past medical history of *** who presented for evaluation of ***

Any ideas where I could start?
User avatar
Nextron
Posts: 1391
Joined: 01 Oct 2013, 08:23
Location: Netherlands OS: Win10 AHK: Unicode x32

Re: Hotkey to select next instance of text

18 Nov 2016, 16:38

I use the following the search through AHK's main window, and select the search phrase. It relies on making a copy of the content of am edit control, so it won't work on controls used in Word or something. In that case, com object may be of use, or just send ^f to use the native search.

Code: Select all

#IfWinActive,ahk_exe AutoHotkey.exe
^f::SearchAHK(1)
f3::
SearchAHK(NewSearch:=0){
	Static Needle, hWnd
	If (Needle="" || NewSearch){
		InputBox,Needle,Find text,Search string:,,300,130,,,,,% Needle
		If ErrorLevel || Needle=""
			Return Needle:=""
	}
	
	If !hWnd
		ControlGet,hWnd,Hwnd ,,Edit1,ahk_exe AutoHotkey.exe
	
	ControlGetText,HayStack,Edit1,ahk_exe AutoHotkey.exe
	VarSetCapacity(SelectionStart, 4), VarSetCapacity(SelectionEnd, 4)
	SendMessage, 0xB0, &SelectionStart, &SelectionEnd,, ahk_id %hWnd% ;EM_GETSEL
	SelectionStart := NumGet(SelectionStart, 0, "UInt")
	;SelectionEnd := NumGet(SelectionEnd, 0, "UInt")
	
	Sel1:=InStr(HayStack,Needle,,SelectionStart+2)-1
	If (Sel1=-1)
		Sel1:=InStr(HayStack,Needle)-1
	Sel2:=Sel1+StrLen(Needle)
	If (Sel1=-1)
		Sel1:=Sel2:=0
	
	PostMessage, 0xB1,% Sel1,% Sel2,,ahk_id %hWnd% ;EM_SETSEL
	PostMessage, 0xB7,,,,ahk_id %hWnd% ;EM_SCROLLCARET
}
espla0

Re: Hotkey to select next instance of text

18 Nov 2016, 22:30

Thanks for the reply! But I'm having troubles getting that to work in Microsoft Word. It doesn't come up with an error or anything when I load the script, just nothing happens when I actually try to use it.

How exactly would I change your code to search for a generic text string not within AHK?

Also, it doesn't even have to be within Microsoft word (I'm not sure if it's easier to specify the script within Word or not).
list
Posts: 222
Joined: 26 Mar 2014, 14:03
Contact:

Re: Hotkey to select next instance of text

19 Nov 2016, 05:16

This is not an answer to your question, but perhaps lintalist might be useful - https://autohotkey.com/boards/viewtopic.php?f=6&t=3378
If you add a snippet like so:
[[Input=Name]] is a [[Input=Age]] year old male with a past medical history of [[Input=History]] who presented for evaluation of [[Input=Evaluation]]
it will ask you the questions Name, Age, History and Evaluation and paste the sentence in your application such as Word.
Apart from asking questions you can select items from a list, add dates (or date ranges) and other things including formatting (bold, italics) and images if you know a bit of HTML or Markdown.

Back to your question: Assuming your program(s) has ctrl-f as a shortcut to find text you can do something like this to simply start the search for ***

Code: Select all

F3::
Send ^f ; open search window
Sleep 100 ; wait for it to appear
Send ***{enter}
Return
If you really need it only with MS Word using COM - you do need to to a bit of digging but this seems like a good start http://autohotkey.com/board/topic/91284 ... /?p=577302 (you can remove the bold & alignment lines of course - oWord.Selection.Find.Execute seems to be the magic command. )

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Fredrik F, Joey5, maxkill, Rohwedder and 386 guests