ToolBox to create Picture clips for Imagesearch

Post your working scripts, libraries and tools for AHK v1.1 and older
ToKa
Posts: 26
Joined: 23 Apr 2016, 04:37

ToolBox to create Picture clips for Imagesearch

21 Jun 2016, 23:04

Code: Select all

This is one of my earlier creations ToolBox.ahk was intended to house a variety of tools to use with AHK.
So far (could not think of anything else to put in it) it has only one function.
ImageSearch will create the Picture clip to use with the ImageSearch command using msPaint.
Saves me a lot of work to manualy get the clip made and figuring out the coordinates.
Have been using this for several weeks and it works well for me.

Tested on an old Windows XP machine with Screen resolution 1280x1024 and 1024x768
I am just a beginner level AHK person so the coding style is very simple.
And a big Thanks for all the Help i have gotten on this site.

Code: Select all

/*
	written by Torsten J. Kammerer 04/20/2016 ver: 1.0
	
	ToolBox.ahk was intended to house a number of useful utilities for
	scripting with Autohotkey. Currently there is only one. 
	ImageSearch is a simple tool to get the Picture clip image to use
	with the ImageSearch command using msPaint.
	usage:
	make sure the Picture you want to get a clip off is on Screen.
	
	1)	run ToolBox.ahk
	
	2)	click on the ImageSearch Tool Button.
	
	3)	A Tooltip with info will attach to your cursor.
		Activate the Window to which the coordinates will be relative to by
		clicking on its TitleBar or click on the Screen to make it relative
		to the Screen.
		
	4)	Move the Mouse to the Top Left of your search Field.
		(the large rectangular area to look for your clip later)
		Press [Alt]-F keys once.
		(your Tooltip will show these coordinates on the 1st F column)
		Move the Mouse to the Bottom Right of your search Field.
		Press [Alt]-F keys once again.
		(your Tooltip will show these coordinates on the 2nd F column)
		
	5)	Move the Mouse to the Top Left of your Picture clip.
		(the rectangular are of your Picture)
		Press [Alt]-P keys once.
		(your Tooltip will show these coordinates on the 1st P column)
		Move the Mouse to the Bottom Right of your Picture clip.
		Press [Alt]-P keys once again.
		(your Tooltip will show these coordinates on the 2nd P column)
		
	6)	Move the Mouse to the Spot you want it to click on later when
		you run your script.
		Press [Alt]-C keys once.
		
	7)	A InputBox will apear prompting you for a Filename for your Picture.
		(Dont enter a path, use Filename only it will end up in the Folder 
		you run your script from.)
		Move the InputBox clear of your Picture area then enter a Filename
		or click OK.
		
	8)	After a second or two msPaint will pop up and go through the motion
		of creating your Picture.
		(sit back and enyoy, do not interrupt its actions
		after it is done creating the picture it will reset msPaint to a
		the way i like it to apear when started up.)
		if there where no errors msPaint will close when its done.
		
	10) ToolBox will Show the script sniplet it created in its edit Window.
		(make sure the Picture is in the Folder you want, may have to move
		it to a different folder depending on where your target script is.)
		Now just copy and paste the sniplet into your code and your good to go.
		
	Here we go ten easy little steps, well it is much easier when you actually
	do it. I have been using this for a few weeks now and it is so much faster
	then doing all the footwork by hand.
	One more thing if you make a mistake with your inputs 
	press [ALT]-X to start over.
	You can also Press [Alt]-T to toggle the Tooltip on and off if it gets in 
	the way.
	
	It has been working well for me, use as you want i hope it saves you some 
	time. If you find any bugs or make improvements let us know by posting them.
	
*/

#SingleInstance,Force
#Persistent
#KeyHistory 0
SendMode Input
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.

OnExit, ExitSub

Gui, Add, Button, gSubImageSearch, create ImageSearch
Gui, Add, Edit, r10 w450 vMyEdit
Gui, Show,, Toolbox
Msg := ""

Hotkey, $Esc, ExitSub

!f::myKey := "F"
!p::myKey := "P"
!c::myKey := "C"
!t::myKey := "T"
!x::myKey := "X"

Hotkey, !f, Off  	;Disables the [Alt]-F hotkey.
Hotkey, !p, Off  	;Disables the [Alt]-P hotkey.
Hotkey, !c, Off  	;Disables the [Alt]-C hotkey.
Hotkey, !t, Off  	;Disables the [Alt]-T hotkey.
Hotkey, !x, Off  	;Disables the [Alt]-X hotkey.

return		;autoexecute section ends here


SubImageSearch:
Hotkey, !f, On  	;Re-enables the [Alt]-F hotkey.
Hotkey, !p, On  	;Re-enables the [Alt]-P hotkey.
Hotkey, !c, On  	;Re-enables the [Alt]-C hotkey.
Hotkey, !t, On  	;Re-enables the [Alt]-T hotkey.
Hotkey, !x, On  	;Re-enables the [Alt]-X hotkey.
fx1 := fy1 := fx2 := fy2 := px1 := py1 := px2 := py2 := cx := cy := 0
PicName := ""
myKey := ""
PreMsg =
(
ImageSearch
Activate the Window to focus first.
all inputs made with the [Alt] key
then the key for the command.
F for Field, P for Picture, 
C for click Pos. (Last),
T to toggle Tooltip,  X to quit
--------------------------------------------
)

PostMsg =
(
--------------------------------------------
C    x:  0    y:  0
)
ttip := 1
Loop
{
if ttip
	{
	Gosub S_IS_Msg
	Tooltip, %PreMsg%`n%Msg%`n%PostMsg%
	}
if (myKey <> "")
	{
	if (myKey = "X")
	break		;exits the loop
	if (myKey = "T")
	if ttip
		{
		Tooltip
		ttip := false
		}
	else
		ttip := true
	MouseGetPos, x, y
	if (myKey = "F")
		{
		if (fx1 = 0)
			{
			fx1 := x
			fy1 := y
			}
		else if (fx2 = 0)
			{
			fx2 := x
			fy2 := y
			} 
		}
	if (myKey = "P")
		{
		if (px1 = 0)
			{
			px1 := x
			py1 := y
			}
		else if (px2 = 0)
			{
			px2 := x
			py2 := y
			} 
		}
	if (myKey = "C")
		{
		if (cx = 0)
			{
			cx := x
			cy := y
			}
		}
	myKey := ""
	;end of if (myKey <> "")
	}
if fx2 and px2
	{
	if cx
		{
		Tooltip
		;before we popup the InputBox lets get the active window coordinates
		WinGetPos, Scr_off_x, Scr_off_y, AW_Width, AW_Height, A
		AWx := AW_Width + Scr_off_x		;for later use
		AWy := AW_Height + Scr_off_y
		Msg = 
		(LTrim
		Click coordinates where x %cx%  y %cy%
		now
		Enter a name for this Clip
		)
		InputBox, PicName, Picture name, %Msg%,,300,200,,,,, myClip.bmp
		if ErrorLevel
			{
			PicName := ""
			break 	; CANCEL was pressed, quiting.
			}
		else
			break	;good name we will use it!
		;end of if cx
		}
	}
;end of Loop
}
Tooltip
Hotkey, !f, Off  	;Disables the [Alt]-F hotkey.
Hotkey, !p, Off  	;Disables the [Alt]-P hotkey.
Hotkey, !c, Off  	;Disables the [Alt]-C hotkey.
Hotkey, !t, Off  	;Disables the [Alt]-T hotkey.
Hotkey, !x, Off  	;Disables the [Alt]-X hotkey.
if (PicName <> "")
{
xoff := cx - px1
yoff := cy - py1
csx := px2 - px1
csy := py2 - py1
Locstr := "found at x %x%  y %y% "
Temp =
(
Tooltip
ImageSearch, x, y, %fx1%, %fy1%, %fx2%, %fy2%, *30 %PicName%
if Not ErrorLevel
	{
	MouseMove, x + %xoff%, y + %yoff%
	click
	Tooltip, clicked on %PicName% %Locstr%
	Sleep, 2000
	}`n
)
MyEdit .= "`n" . Temp
guicontrol, , MyEdit, %MyEdit%
gui, submit
Tooltip
Sleep, 200
;Copy Screen to clipboard
Sendinput  {Alt}{PrintScreen}
Sleep, 2000
Run mspaint
WinWaitActive, ahk_class MSPaintApp,, 2
WinMaximize
WinWait, A
Sleep, 600
;keeping track if Menu-View Tab is up or down
;click right side off Menu View
click 90, 38
M_V_up := false
Sleep, 600
cm_Color_Box := 0
cm_Status_Bar := 0
cm_Tool_Box := 0
Paint_black := 0x000000
PixelSearch, x, y, 71, 62, 73, 64, Paint_black, 20
if Not Errorlevel
	cm_Tool_Box := 1
PixelSearch, x, y, 71, 79, 73, 81, Paint_black, 20
if Not Errorlevel
	cm_Color_Box := 1
PixelSearch, x, y, 71, 95, 73, 97, Paint_black, 20
if Not Errorlevel
	cm_Status_Bar := 1
if cm_Color_Box
	{
	;turn it off
	click 100, 76
	WinWait, A
	Sleep, 600
	M_V_up := true
	}
if cm_Status_Bar
	{
	if M_V_up
		{
		;click on Menu View
		click 80, 38
		Sleep, 600
		M_V_up := false
		}
	;turn it off
	click 100, 94
	WinWait, A
	Sleep, 600
	M_V_up := true
	}
if Not cm_Tool_Box
	{
	if M_V_up
		{
		click 80, 38		;click on Menu View
		Sleep, 600
		M_V_up := false
		}
	click 100, 60		;click on ViewSubMenu Toolbox turn-on
	WinWait, A
	Sleep, 600
	M_V_up := true
	}
click 43, 66		;Select the Select Tool rectangular   
Sleep, 600
if M_V_up
	{
	click 80, 38		;click on Menu View
	Sleep, 600
	M_V_up := false
	}
;click on ViewSubMenu Toolbox turn-off
click 100, 60
WinWait, A
Sleep, 600
M_V_up := true
;Tool Rectangular selected
M_I_up := true
if M_I_up
	{
	click  117, 38		;click on Menu Image
	Sleep, 600
	M_I_up := false
	}
Tooltip
cm_Opaque := 0
PixelGetColor, col, 105, 148, RGB    ;Check for Draw_Opaque setting
if (col = Paint_black)
	cm_Opaque := 1
if Not cm_Opaque
	{
	click  150, 145		;Select the Draw_Opaque setting
	Sleep, 600
	click  117, 38		;click on Menu Image
	M_I_up := false
	}
;Draw_Opaque setting on
Sleep, 500
click 150, 110		;click on ImageSubMenu Attributes
M_I_up := true
WinWait, A
Sleep, 600
ToolTip
;Check Menu-Image-Attributes Checkmark Units-Pixels
PixelGetColor, col, 167, 150, RGB    ;Check for Pixels
if (col <> 0x38B935)
	{	
	click  167, 150		;Select Pixels
	Sleep, 200
	}
;Check Menu-Image-Attributes Checkmark Colors-Colors
PixelGetColor, col, 167, 202, RGB    ;Check for Colors
if (col <> 0x38B935)
	{
	click  167, 202		;Select Colors
	Sleep, 200
	}
sendinput 15 {Tab} 10 {Enter}
WinWait, A
Sleep, 600
M_E_up := true
;click on Menu Edit
click  42, 34
M_E_up := false
Sleep, 600
;click on EditSubmenu Paste - the big Picture
click  76, 136
M_E_up := true
WinWait, A
Sleep, 600
Send {Esc}
Sleep, 100
;lets put some numbers in
P_Largest_wos_Width := %A_ScreenWidth% - 10			; largest img without scrollbars
P_Largest_wos_Height := %A_ScreenHeight% - 90
if (px2 > px1)
	Lx := px2					;longest x move inside window
else
	Lx := px1
if (py2 > py1)
	Ly := py2					;longest y move inside window
else
	Ly := py1
Lx += Scr_off_x		;total x
Ly += Scr_off_y		;total y
if (AWx > P_Largest_wos_Width) or (AWy > P_Largest_wos_Height)	;wont fit
	{
	;with the scrollbars up we have about 19 pixels less available.	
	P_Largest_wos_Width -= 19
	P_Largest_wos_Height -= 19
	if (Lx > P_Largest_wos_Width) or (Ly > P_Largest_wos_Height)
		{
		cm_R_scroll := 0			;target not on screen, move scrollbars
		cm_D_scroll := 0
		Paint_scrollarrow := 0x85614D
		;right sideway scrollarrow
		ssax := %A_ScreenWidth% - 22
		ssay := %A_ScreenHeight% - 39
		PixelSearch, x, y, %ssax% - 1, %ssay% - 1, %ssax% + 1, %ssay% + 1, Paint_scrollarrow, 30
		if Not Errorlevel
			cm_R_scroll := 1
		;Bottom vertical scrollarrow
		vsax := %A_ScreenWidth% - 7
		vsay := %A_ScreenHeight% - 56
		PixelSearch, x, y, %vsay% - 1, %vsay% - 1, %vsay% + 1, %vsay% + 1, Paint_scrollarrow, 30
		if Not Errorlevel
			cm_D_scroll := 1
		if cm_R_scroll
			{
			MouseMove, %ssax%, %ssay%
			Click down
			Sleep, 1000
			Click up
			Scr_off_x -= AWx - P_Largest_wos_Width - 3	;big num more x (4)
			}
		if cm_D_scroll
			{
			MouseMove, %vsax%, %vsay%
			Click down
			Sleep, 1000
			Click up
			Scr_off_y -= AWy - P_Largest_wos_Height - 3 	;big num more y (4)
			}
		}
	}
CoordMode, Mouse, Client
;adding Paints client window shift values to x and y
x1 := px1 + Scr_off_x + 5
y1 := py1 + Scr_off_y + 5
x2 := px2 + Scr_off_x + 5
y2 := py2 + Scr_off_y + 5
Sleep, 100
MouseClickDrag, L, x1, y1, x2, y2
Sleep, 100
CoordMode, Mouse, Window
;click on Menu Edit
click  42, 34
M_E_up := false
Sleep, 600
;click on EditSubmenu Copy
click 76, 120
M_E_up := true
WinWait, A
Sleep, 600
;click on Menu File
click 18, 38
M_F_up := false
Sleep, 600
;click on FileSubmenu New
click 40, 60
Sleep, 600
M_F_up := true
;popup Messagebox asking if we want to save the file we are closing
;tab shifts focus to No Button then Enter
WinWait, A
Sleep, 600
sendinput {Tab}{Enter}
;ToolTip, Popup - No Entered, 300, 300
WinWait, A
Sleep, 600
;click on Menu Edit
click 42, 34
M_E_up := false
Sleep, 600
;click on EditSubmenu Paste
click 76, 136
WinWait, A
;ToolTip, Menu - Edit - Paste selected, 300, 300
Sleep, 600
;click on Menu File
click 18, 38
M_F_up := false
Sleep, 600
;click on FileSubmenu Save as
click 50, 110
WinWait, A
;ToolTip, Menu - File - Save as - selected, 300, 300
Sleep, 600
Full_FileName = %A_ScriptDir%\%PicName%
Sleep, 100
sendinput %Full_FileName% {Enter}
WinWait, A
;ToolTip, Filename entered, 300, 300
Sleep 600
;ToolTip
;-----------------cleanup-----------------
;click on Menu File
click 18, 38
Sleep, 600
;click on FileSubmenu New
click 40, 60
WinWait, A
Sleep, 600
click 80, 38			;click on Menu View
Sleep, 600
click 100, 60		;click on ViewSubMenu Toolbox turn on
Sleep, 600
click 80, 38			;click on Menu View
Sleep, 600
click 100, 96		;click on ViewSubMenu Statusbar turn on
Sleep, 600
click 80, 38			;click on Menu View
click 100, 76		;click on ViewSubMenu ColorBox turn on
Sleep, 600
click 117, 38		;click on Menu Image
Sleep, 600
click 150, 110		;click on ImageSubMenu Attributes
WinWait, A
Sleep, 600
sendinput 800 {Tab} 600 {Enter}
WinWait, A
Sleep, 600
click 1248, 15		;click on Restore Window
WinWait, A
Sleep, 600
;Shortcut Key to Close Paint Window exit
click 17, 39			;click on Menu File
Sleep, 600
click 33, 369		;click on Menu File-Exit
WinWait, A
Sleep, 600
sendinput {Tab}{Enter}
}
Tooltip
msg := ""
Gui, Show,, Toolbox
return

S_IS_Msg:
Msg =
(
F    x1:  %fx1%     y1:  %fy1%
F    x2:  %fx2%     y2:  %fy2%
--------------------------------------------
P    x1:  %px1%    y1:  %py1%
P    x2:  %px2%    y2:  %py2%
)
return

ExitSub:
GuiClose:
ToolTip		;turns it off
ExitApp  	;OnExit sub will not terminate unless the subroutine uses ExitApp.

ToKa
Posts: 26
Joined: 23 Apr 2016, 04:37

Re: ToolBox to create Picture clips for Imagesearch

22 Jun 2016, 18:50

I Fixed the coding of the screen resolution, should work with all now.
updated the description.

Return to “Scripts and Functions (v1)”

Who is online

Users browsing this forum: No registered users and 136 guests