AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Practical use of ASCII capture: locate a control

 
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions
View previous topic :: View next topic  
Author Message
tinypig



Joined: 12 Mar 2005
Posts: 14

PostPosted: Sun Mar 13, 2005 6:43 am    Post subject: Practical use of ASCII capture: locate a control Reply with quote

Code:

;
;MouseLocate by David Bradford
;
;This is an example of how to use this script
;ctrl-alt-l will position my mouse over the submit button on http://www.rottentomatoes.com
;However, to use this, you'll want to scan a control yourself.  Hold down the ctrl key
;and press the left mouse button, then drag the window that appears till it covers the
;control.  Be careful not to highlight or do anything that changes the appearance of the
;control, as the mouse will still do whatever it would normally do on that window.
;
;Go to Notepad and make sure you are using a fixed-width font (as a suggestion, go to
;Format->Font, select Terminal font, Regular style, and the smallest size you can). Then
;paste from the clipboard.
;
;I have switched to all alpha characters as it makes coding easier.
;
;Below, I have put the output after "scanning" the submit button from Rotten Tomatoes.
;Note that the mouse will be put in the center of whatever graphic you use, so narrow it
;down to just the control for best results.
;
;scan_x_start, scan_x_end, scan_y_start, and scan_y_end tell it what rectangle to search
;for the control.
;
;Thanks to Jon for the control selection code
;

^!l::
;Uncomment the next two lines if you want to search the screen instead of the current window
;CoordMode, Pixel, Screen
;CoordMode, Mouse, Screen
scan_x_start=8
scan_x_end  =208
scan_y_start=573
scan_y_end  =707
scan_in_line0 =ttttttcccccccccccccccccccccccccccccccccccccccccccccccccccccccccccntttttt
scan_in_line1 =ttttccctttttttttttttttttttttttttttttttttttttttttttttttttttttttttttnntttt
scan_in_line2 =tttccttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttt
scan_in_line3 =ttnctttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttMtt
scan_in_line4 =ttctttttttttnnnnnnntnnntttnnnnnnnnnnntnnnnttttnnntnnnnnnnnnnnttttttttttt
scan_in_line5 =tncttttttttniiiiiiinnictttnicniiiiiiinniiinttniiinnicciiiiiictttttttttMt
scan_in_line6 =tntttttttttnictttnnnnictttnicnictttnicnicicttcicinnictttiittttttttttttMt
scan_in_line7 =tntttttttttniiiiiiinnictttnicniiccciinniccinniccinnictttiittttttttttttMt
scan_in_line8 =tnttttttttttnnnnncicnictttnicnicnnnnicnictiiiitcinnictttiittttttttttttMt
scan_in_line9 =tntttttttttnicnnncicniinnncicniinnnniinictniintcinnictttiittttttttttttMt
scan_in_line10=tttttttttttnciiiiicntniiiiicnniiiiiiinnicttccttcinnictttiitttttttttttMMt
scan_in_line11=tttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttMtt
scan_in_line12=tttMttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttMMtt
scan_in_line13=tttMtttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttMMttt
scan_in_line14=ttttMMtttttttttttttttttttttttttttttttttttttttttttttttttttttttttttMMMtttt
scan_in_line15=ttttttMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMtttttt
WinActivate ahk_id %dmb_lastwin%
Gosub, MouseLocate
return


MouseLocate:
Gosub, ScanScreen
scan_j=0
scan_found=0
scan_last_index=-1
scan_num_searched=0
scan_output=%scan_current_line%
Loop, Parse, scan_output, 0
{
  Transform, scan_cur_line, deref, `%scan_in_line%scan_j%`%
  If (scan_j > 0 and !scan_cur_line)
  {
    scan_found=1
    break
  }
  StringGetPos, scan_index, A_LoopField, %scan_cur_line%
  If(scan_index > -1 and scan_j = 0 or (scan_j > 0 and scan_last_index = scan_index))
  {
    If(scan_j = 0)
    {
      scan_y_adj=%scan_num_searched%
    }
    scan_j++
    scan_last_index=%scan_index%
    StringLen, scan_str_len, scan_cur_line
    if(scan_str_len > scan_max_len)
    {
      scan_max_len=%scan_str_len%
    }
  }
  else
  {
    scan_j=0
  }
  scan_num_searched++
}
if(scan_found)
{
  scan_center_x := scan_max_len / 2
  scan_center_y := scan_j / 2
  Transform, scan_center_x, Round, %scan_center_x%
  Transform, scan_center_y, Round, %scan_center_y%
  scan_center_x := scan_center_x + scan_last_index + scan_x_start
  scan_center_y := scan_center_y + scan_y_start + scan_y_adj
  MouseMove, %scan_center_x%, %scan_center_y%
}
return


~^LButton::
#persistent
CoordMode, Mouse, Screen
CoordMode, Tooltip, Screen
MouseGetPos, scan_x_start, scan_y_start
currentXpos=%scan_x_start%
currentYpos=%scan_y_start%
ToolTip, ., scan_x_start, scan_y_start

WinSet, Transparent, 100, ahk_class tooltips_class32

Loop
{
  MouseGetPos, scan_x, scan_y
  scan_x-=%currentXpos%
  scan_y-=%currentYpos%
  WinMove, ahk_class tooltips_class32, , , , %scan_x%, %scan_y%

  GetKeyState, state, LButton

  if state=u
  {
    tooltip
    break
  }
}

MouseGetPos, scan_x_end, scan_y_end

TrayTip, , Scanning...., , 1

CoordMode, Pixel, Screen
CoordMode, Mouse, Screen
MouseGetPos, scan_x_end, scan_y_end
Gosub, ScanScreen
StringReplace, Clipboard, scan_current_line, 0, `r`n, 1
TrayTip, , Scan complete, , 1
sleep, 1000
TrayTip
return


ScanScreen:
;results returned in %scan_current_line%
scan_current_y=%scan_y_start%
scan_current_x=%scan_x_start%
scan_current_line=
Loop
{
    scan_current_x++
    if scan_current_x > %scan_x_end%
    {
        scan_current_line=%scan_current_line%0
        scan_current_y++
        if scan_current_y > %scan_y_end%
            break
        scan_current_x=%scan_x_start%
        continue
    }
    PixelGetColor, found_color, %scan_current_x%, %scan_current_y%
    StringMid, scan_rgb_r, found_color, 3, 2
    StringMid, scan_rgb_g, found_color, 5, 2
    StringMid, scan_rgb_b, found_color, 7, 2
    scan_rgb_r=0x%scan_rgb_r%
    scan_rgb_g=0x%scan_rgb_g%
    scan_rgb_b=0x%scan_rgb_b%
    scan_rgb_sum:=scan_rgb_r + scan_rgb_g + scan_rgb_b
    if scan_rgb_sum > 0x280
    {
      scan_current_line =%scan_current_line%i
    }
    else if scan_rgb_sum > 0x200
    {
      scan_current_line =%scan_current_line%c
    }
    else if scan_rgb_sum > 0x180
    {
      scan_current_line =%scan_current_line%n
    }
    else if scan_rgb_sum > 0x100
    {
      scan_current_line =%scan_current_line%t
    }
    else if scan_rgb_sum > 0x80
    {
      scan_current_line =%scan_current_line%I
    }
    else
    {
      scan_current_line =%scan_current_line%M
    }
}
return

Back to top
View user's profile Send private message
Serenity



Joined: 07 Nov 2004
Posts: 1276

PostPosted: Sun Mar 13, 2005 10:58 am    Post subject: Reply with quote

Thats an interesting use for the script, though it will have problems on a system using a different font and color scheme. On the other hand it could produce matches that cannot otherwise be searched for.
_________________
"Anything worth doing is worth doing slowly." - Mae West
Back to top
View user's profile Send private message Visit poster's website
tinypig



Joined: 12 Mar 2005
Posts: 14

PostPosted: Sun Mar 13, 2005 2:09 pm    Post subject: Reply with quote

For some things, yes, but the control in question here is a gif, so that won't apply in this case. For cases such as you are describing, I deal with them like this:

I have the same main .ahk script running on my home and work computers. I check upon loading the script which one I am on (through COMPUTERNAME). Then I take different paths at certain points in the script depending on which machine I am on.
Back to top
View user's profile Send private message
cybrown
Guest





PostPosted: Tue Mar 15, 2005 5:44 pm    Post subject: Reply with quote

This is pretty cool, and I love the original ascii script, but why not just use ImageSearch for this purpose? The nice thing about your method is the "image" is contained right inside the script, but that seems to be the only advantage. Then again I'm a total newbie so maybe I'm missing something subtle?
Back to top
tinypig



Joined: 12 Mar 2005
Posts: 14

PostPosted: Tue Mar 15, 2005 5:52 pm    Post subject: Reply with quote

One other thing is that my way takes less work (just highlight the image you see on the screen and you've got it). It seemed to me that when I was trying to do the same thing with ImageSearch, I had to do a printscreen, go into paint, isolate the smaller image on the screen, resize the image, etc. However, there is probably a speed disadvantage with my way (haven't really checked).

Also, I had tried ImageSearch a while back and had difficulties making it work. I think I need to give it another shot. I'd like to be able to OCR part of the screen to get the text... that would be really cool.
Back to top
View user's profile Send private message
tinypig



Joined: 12 Mar 2005
Posts: 14

PostPosted: Wed Mar 16, 2005 5:54 am    Post subject: Reply with quote

After getting ImageSearch to work and seeing how fast it is, regarding my code in this post, I just want to say: nevermind! Smile I shall use ImageSearch from now on.
Back to top
View user's profile Send private message
cybrown
Guest





PostPosted: Wed Mar 16, 2005 1:17 pm    Post subject: Reply with quote

I just discovered ImageSearch myself and was amazed at the speed. The only thing that bugs me is it doesn't have a WindowName parameter. I'm avoiding this currently by using a Coordmode change, getting the location of the window I want to use, and then sending the search coords relative to that window, but the process would be easier if I could just tell ImageSearch which window to use.
Back to top
Chris
Site Admin


Joined: 02 Mar 2004
Posts: 10480

PostPosted: Wed Mar 16, 2005 1:38 pm    Post subject: Reply with quote

This sounds like a good idea: To have ImageSearch and PixelSearch optionally accept a WinTitle/WinText rather than a set of coordinates. The search could then be confined to the boundaries of that window only.

I'll add this to the to-do list. Thanks.
Back to top
View user's profile Send private message Send e-mail
alex
Guest





PostPosted: Wed Apr 20, 2005 4:13 am    Post subject: Amazing Script! Reply with quote

Wow you really saved my life with this one

I have been trying now for 3 days to develop my own image search because ImageSearch does not work in 256 color mode.

I'm making a script that takes a random mix of units in StarCraft and sorts them into hot-key groups based on unit type.

I was hoping to be able to use your script to search for a few hundred pixels in an area 200x150 but that takes about 1 second. And I need to do it maybe 12-48 times in a second for 8 different unit icons.

Well it turns out only 10 pixels are needed for 100% accuracy in areas of high contrast so it works very fast.

Please post if you make any improvements to the program Smile

thanks

Alex
Back to top
Display posts from previous:   
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions All times are GMT
Page 1 of 1

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group