AutoHotkey Community

It is currently May 24th, 2012, 1:54 pm

All times are UTC [ DST ]




Post new topic Reply to topic  [ 10 posts ] 
Author Message
PostPosted: December 22nd, 2008, 4:08 pm 
Offline

Joined: May 12th, 2005, 8:20 am
Posts: 331
Location: Münster, Germany
Hi, Community,
this morning I posted a question of how to code a search dialogue
which would highlight the search string in the haystack and which
provides a repetitive search.
HugoV gave me a precious hint of how to find Laszlo's
script and here's the result.
Just load down the script and let it run.
Enter a search phrase in the edit field underneath the search button,
subsequently click the search button.

Known limitation:
When the text is wider than the edit area, the found string will be
marked but no horizontical scrolling will happen. Any ideas?

To be done:
Install hotkeys ctrl-f (start search) and F3 (continue search)

Code:
; a little gui with some text from the manual
Gui 1:Add, Button, gFindString, Search
Gui 1:Add, Edit, w100 vfind
Gui 1:Add, Edit, w330 r3 vContent HSCROLL
Gui 1:Add, StatusBar,, Type the search string to look for and click the Search-button
SB_SetParts(300)
Gui 1:Show,, FindSample
WinGet ControlID, ID, FindSample

someText=
(
Creating a script
Each script is a plain text file containing commands
to be executed by the program (AutoHotkey.exe).
A script may also contain hotkeys and hotstrings, or
even consist entirely of them. However, in the absence
of hotkeys and hotstrings, a script will perform its
commands sequentially from top to bottom the moment
it is launched.

To create a new script:

Open Windows Explorer and navigate to a folder of
your choice.
Pull down the File menu and choose New >> AutoHotkey
Script (or Text Document).
Type a name for the file, ensuring that it ends in .ahk.
For example: Test.ahk
Right-click the file and choose Edit Script.
On a new blank line, type the following:
#space::Run www.google.com
The symbol # stands for the Windows key, so #space means
holding down the Windows key then pressing the spacebar
to activate a hotkey. The :: means that the subsequent
command should be executed whenever this hotkey is
pressed, in this case to go to the Google web site.
To try out this script, continue as follows:

Save and close the file.
In Windows Explorer, double-click the script to launch
it. A new tray icon appears. Hold down the Windows key
and press the spacebar. A web page opens in the default
browser.
To exit or edit the script, right click its tray icon.
Note: Multiple scripts can be running simultaneously,
each with its own tray icon. Furthermore, each script
can have multiple hotkeys and hotstrings.
)
Guicontrol,,Edit2, %someText%
return

FindString:
   Gui, Submit, Nohide

   if (find != lastFind) {
    offset = 0
    hits = 0
   }

   GuiControl 1:Focus, Content                           ; focus on main help window to show selection
   SendMessage 0xB6, 0, -999, Edit2, ahk_id %ControlID%  ; Scroll to top
   StringGetPos pos, Content, %find% ,,offset            ; find the position of the search string
   if (pos = -1) {
    if (offset = 0) {
      SB_SetText("'" . find . "' not found", 1)
      SB_SetText("", 2)
    }
    else {
      SB_SetText("No more occurrences of '" . find . "'")
      SB_SetText("", 2)
      offset = 0
      hits = 0
    }
    return
   }
   StringLeft __s, Content, %pos%                        ; cut off end to count lines
   StringReplace __s,__s,`n,`n,UseErrorLevel             ; Errorlevel <- line number
   addToPos=%Errorlevel%
   SendMessage 0xB6, 0, ErrorLevel, Edit2, ahk_id %ControlID% ; Scroll to visible
   SendMessage 0xB1, pos + addToPos, pos + addToPos + Strlen(find), Edit2, ahk_id %ControlID% ; Select search text
   ; http://msdn.microsoft.com/en-us/library/bb761637(VS.85).aspx
   ; Scroll the caret into view in an edit control:
   SendMessage, EM_SCROLLCARET := 0xB7, 0, 0, Edit2, ahk_id %ControlID%
   offset := pos + addToPos + Strlen(find)
   lastFind = %find%
   hits++
   SB_SetText("'" . find . "' found in line " . addToPos + 1, 1)
   SB_SetText(hits . (hits = 1 ? " hit" : " hits"), 2)
Return

Let me know what you think and of course report me all the bugs.
I'm aware that I only played around with the excellent code of
Laszlo, honour to whom honour is due!

Have a nice and peaceful Christmas and a happy new year,
Klaus


Last edited by Klaus on December 23rd, 2008, 9:53 am, edited 1 time in total.

Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: December 22nd, 2008, 8:55 pm 
Offline

Joined: May 2nd, 2006, 11:16 pm
Posts: 800
Location: Greeley, CO
I used your code to modify TheWeatherGuy's Wx Script.

Thanks!

_________________
Image
SoggyDog
Dwarf Fortress:
"The most intriguing game I've ever played."


Last edited by SoggyDog on January 4th, 2009, 6:28 am, edited 1 time in total.

Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: December 23rd, 2008, 6:58 am 
Offline

Joined: August 24th, 2005, 5:29 pm
Posts: 549
Location: Berlin / Germany
Klaus wrote:
Known limitation:
When the text is wider than the edit area, the found string will be
marked but no horizontical scrolling will happen. Any ideas?

Code:
   ; SendMessage 0xB6, 0, ErrorLevel, Edit2, ahk_id %ControlID% ; Scroll to visible
   SendMessage 0xB1, pos + addToPos, pos + addToPos + Strlen(find), Edit2, ahk_id %ControlID% ; Select search text
   ; http://msdn.microsoft.com/en-us/library/bb761637(VS.85).aspx
   ; Scroll the caret into view in an edit control:
   SendMessage, EM_SCROLLCARET := 0xB7, 0, 0, Edit2, ahk_id %ControlID%

Have a nice and peaceful Christmas and a happy new year,
nick :wink:

BTW: http://de.autohotkey.com/forum/topic4004.html

_________________
nick :wink:


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: December 23rd, 2008, 9:54 am 
Offline

Joined: May 12th, 2005, 8:20 am
Posts: 331
Location: Münster, Germany
Hi, nick,

thanks for your most welcome contribution!
That horizontal scrolling was necessary.

Merry Christmas and a new year as you desire it!
Klaus

PS: Gruß nach Berlin!


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: January 8th, 2009, 10:38 pm 
Offline

Joined: May 2nd, 2006, 11:16 pm
Posts: 800
Location: Greeley, CO
Okay...
Here I go again twiddling with other peoples code...
I have modified this script to automatically populate the 'Content' (text to search) using OnClipboardChange.

Code:
; http://www.autohotkey.com/forum/viewtopic.php?t=39120

#NoEnv
#SingleInstance Ignore
#Persistent
SendMode Input

Gui 1: Add, Button, x9 y5 w46 h23 gFindString Default, Search
Gui 1: Add, Edit, x9 y34 w100 h21 vfind,
Gui 1: Add, Edit, x9 y61 w330 h64 r3 vContent HSCROLL,
Gui 1: Add, Text, x116 y37 w200 h20 , <----- Enter 'Search String' here
Gui 1: Add, StatusBar,, Copy text to search, enter Search String and click 'Search'
SB_SetParts(300)
Gui 1: Show,, Simple Text search
WinGet ControlID, ID, Simple Text search

searchText = Initializing...

Guicontrol,,Edit2, %searchText%
Return

OnClipboardChange:
if A_EventInfo = 1
{
   If searchText = Initializing...
   {
      sleep, 1000
      searchText = Ready...`nCopy text to search here.
   }
   else
   {
      searchText = %clipboard%
   }
   GuiControl,,Content,%searchText%
   Gui, Show
}
Return

FindString:
   Gui, Submit, Nohide

   if (find != lastFind) {
    offset = 0
    hits = 0
   }

   GuiControl 1:Focus, Content
   SendMessage 0xB6, 0, -999, Edit2, ahk_id %ControlID%
   StringGetPos pos, Content, %find% ,,offset
   if (pos = -1) {
    if (offset = 0) {
      SB_SetText("'" . find . "' not found", 1)
      SB_SetText("", 2)
    }
    else {
      SB_SetText("No more occurrences of '" . find . "'")
      SB_SetText("", 2)
      offset = 0
      hits = 0
    }
    Return
   }
   StringLeft __s, Content, %pos%
   StringReplace __s,__s,`n,`n,UseErrorLevel
   addToPos=%Errorlevel%
   SendMessage 0xB1, pos + addToPos, pos + addToPos + Strlen(find), Edit2, ahk_id %ControlID%
   SendMessage, EM_SCROLLCARET := 0xB7, 0, 0, Edit2, ahk_id %ControlID%
   offset := pos + addToPos + Strlen(find)
   lastFind = %find%
   hits++
   SB_SetText("'" . find . "' found in line " . addToPos + 1, 1)
   SB_SetText(hits . (hits = 1 ? " hit" : " hits"), 2)
Return

GuiClose:
ExitApp

_________________
Image
SoggyDog
Dwarf Fortress:
"The most intriguing game I've ever played."


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: January 10th, 2009, 3:58 pm 
Offline

Joined: December 29th, 2004, 8:22 pm
Posts: 75
SoggyDog,

Try this:
Code:
; a little gui with some text from the manual
Gui 1:Add, Button, gFindString, Search
Gui 1:Add, Edit, w100 vfind
Gui 1:Add, Edit, w330 r40 vcontent HSCROLL
Gui 1:Add, StatusBar,, Type the search string to look for and click the Search-button
;SB_SetParts(300)
Gui 1:Show,, FindSample
WinGet ControlID, ID, FindSample


FindString:
Gui, Submit, Nohide

if (find != lastFind) {
offset = 0
hits = 0
}

GuiControl 1:Focus, Content ; focus on main help window to show selection
SendMessage 0xB6, 0, -999, Edit2, ahk_id %ControlID% ; Scroll to top
StringGetPos pos, Content, %find% ,,offset ; find the position of the search string
if (pos = -1) {
if (offset = 0) {
SB_SetText("'" . find . "' not found", 1)
SB_SetText("", 2)
}
else {
SB_SetText("No more occurrences of '" . find . "'")
SB_SetText("", 2)
offset = 0
hits = 0
}
return
}
StringLeft __s, Content, %pos% ; cut off end to count lines
StringReplace __s,__s,`n,`n,UseErrorLevel ; Errorlevel <- line number
addToPos=%Errorlevel%
SendMessage 0xB6, 0, ErrorLevel, Edit2, ahk_id %ControlID% ; Scroll to visible
SendMessage 0xB1, pos + addToPos, pos + addToPos + Strlen(find), Edit2, ahk_id %ControlID% ; Select search text
; http://msdn.microsoft.com/en-us/library/bb761637(VS.85).aspx
; Scroll the caret into view in an edit control:
SendMessage, EM_SCROLLCARET := 0xB7, 0, 0, Edit2, ahk_id %ControlID%
offset := pos + addToPos + Strlen(find)
lastFind = %find%
hits++
SB_SetText("'" . find . "' found in line " . addToPos + 1, 1)
SB_SetText(hits . (hits = 1 ? " hit" : " hits"), 2)
Return


What I'm trying to figure out is why the script skips characters during a search...

Paste this into the search for box:
Code:
Q


...& this in the text to be searched box:
Code:
QQQ
QQQQ
QQQQQ


When the search button is repeadedly pressed, why does the script skip characters during the search?

_________________
-buttons, buttons,...
I like to push all the buttons!!!


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: January 12th, 2009, 5:28 am 
Offline

Joined: May 2nd, 2006, 11:16 pm
Posts: 800
Location: Greeley, CO
@mAdDoG

a) I like a larger 'Content' area as well, but why did you disable the hit counter?

b) I hadn't noticed the problem with repeating characters (though you are clearly correct).
I never really tested the code Klaus posted, rather just made some mods to his existing 'engine'.
Perhaps he can explain why this happens.

_________________
Image
SoggyDog
Dwarf Fortress:
"The most intriguing game I've ever played."


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: November 7th, 2009, 1:13 am 
Offline

Joined: December 29th, 2004, 8:22 pm
Posts: 75
Can someone help me figure this out?

If you run a copy of this script & repeatedly hit the Search-button,
You'll find an obviuos skipping pattern.

Code:

#NoEnv
SendMode Input
SetWorkingDir %A_ScriptDir%

text = xxxxxx`nxxxxxx`nxxxxxx`nxxxxxx

Gui 1:Add, Button, , Search
Gui 1:Add, Edit, w100 vfind, x
Gui 1:Add, Edit, w330 r30 vContent HSCROLL, %text%
Gui 1:Add, StatusBar,, Type the search string to look for and click the Search-button
SB_SetParts(300)
Gui 1:Show,, FindSample
WinGet ControlID, ID, FindSample
return


ButtonSearch:


   Gui, Submit, Nohide
   if (find != lastFind)
   {
    offset = 0
    hits = 0
   }


   GuiControl 1:Focus, Content                                ; focus on main help window to show selection
   SendMessage 0xB6, 0, -999, Edit2, ahk_id %ControlID%       ; Scroll to top
   StringGetPos pos, Content, %find% ,,offset                 ; find the position of the search string


   if (pos = -1)
   {
    if (offset = 0)
    {
      SB_SetText("'" . find . "' not found", 1)
      SB_SetText("", 2)
    }
    else
    {
      SB_SetText("No more occurrences of '" . find . "'")
      SB_SetText("", 2)
      offset = 0
      hits = 0
    }
    return
   }


   StringLeft __s, Content, %pos%                        ; cut off end to count lines
   StringReplace __s,__s,`n,`n,UseErrorLevel             ; Errorlevel <- line number
   addToPos=%Errorlevel%


   SendMessage 0xB6, 0, ErrorLevel, Edit2, ahk_id %ControlID%                                    ; Scroll to visible
   SendMessage 0xB1, pos + addToPos, pos + addToPos + Strlen(find), Edit2, ahk_id %ControlID%     ; Select search text
   SendMessage, EM_SCROLLCARET := 0xB7, 0, 0, Edit2, ahk_id %ControlID%      ; Scroll the caret into view in an edit


   offset := pos + addToPos + Strlen(find) 
   lastFind = %find%


   hits++
   SB_SetText("'" . find . "' found in line " . addToPos + 1, 1)
   SB_SetText(hits . (hits = 1 ? " hit" : " hits"), 2)


Return


guiclose:
ExitApp



I've spent hours trying to figure out why it behaves like this & need some help.

_________________
-buttons, buttons,...
I like to push all the buttons!!!


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: November 7th, 2009, 8:35 am 
Offline
User avatar

Joined: March 19th, 2008, 12:43 am
Posts: 5459
Location: the tunnel(?=light)
I'm betting the answer is here:

Code:
offset := pos + addToPos + Strlen(find)


I can't figure out why addToPos is added to the offset, but with each increase in the ErrorLevel moving to the next line the offset (and thus the results) gets more skewed. Try modifying that line to this:

Code:
offset := pos + Strlen(find)

_________________
Image
Try Quick Search for Autohotkey or see the tutorial for newbies.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: November 7th, 2009, 3:36 pm 
Offline

Joined: December 29th, 2004, 8:22 pm
Posts: 75
That was it.

I was just about to rip everything up & start from scratch, & thought to see if anyone answered the post.
Thanks!

_________________
-buttons, buttons,...
I like to push all the buttons!!!


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

All times are UTC [ DST ]


Who is online

Users browsing this forum: Bing [Bot], cmulcahy, Exabot [Bot], MSN [Bot], tomoe_uehara and 14 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