# Getting distance in pixels?

## Recommended Posts

Hello,

I currently have a problem that deals with moving the mouse to a given location + or - a certain distance.

\$search1 =_WaitForImageSearch("envelope.bmp", 10, 1, \$x, \$y, 20)
\$search2 =_WaitForImageSearch("envelope2.bmp", 10, 1, \$x, \$y, 20)

If \$search1 = 1 Then
Sleep(100)
MouseMove(\$x, \$y, 10)
sleep(99)
MouseClick("left")
Sleep(99)
ElseIf \$search2 = 1 Then
Sleep(100)
MouseMove(\$x, \$y2, 10)
sleep(99)
MouseClick("left")
Sleep(100)
Else
Sleep(150)
MouseMove(\$x2, \$y, 10)
Sleep(150)
MouseClick("left")
Sleep(150)

EndIf

The problem about this is that i defined \$x2 = \$x - 175  and \$y2 = \$y + 35

I got these numbers ( 175 and 35) with the autoit helper and just subtracted point where it is to the point its supposed to move but it doesnt work.

is there any proper way to find the distance between 2 points in pixels?

Imgsearch doesnt always work for this picture so i want it to have a backup

edit: could it be that the definition of \$x and \$y  from the previous image search arent there anymore and thats why it moves my mouse to strange places?
is there a way to preserve the \$x \$y from the previous img search untill the next succesful one?

Edited by tiye0405
##### Share on other sites

Old as he is Pythagoras is still your friend!

Looks like you're more after the correct origin for the coordinates than actual distance between pixels.

BTW why on earth do you need to compute mouse position this way? What are you trying to automete? There must be a more reliable way to perform thetask at hand.

Edited by jchd

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

##### Share on other sites

edit:

Something must be off with my original text

my testing script works just fine going just where i want it to but my original script always goes to: ( current(x), 50)

```global \$y = 0, \$x = 0, \$x2 = \$x - 308, \$y2 = \$y + 40

\$search1 =_WaitForImageSearch("name.bmp", 5, 1, \$x, \$y, 20)
\$search2 =_WaitForImageSearch("star.bmp", 5, 1, \$x, \$y, 20)
If \$search1 = 1 Then
Sleep(100)
MouseMove(\$x, \$y, 10)
sleep(99)
MouseClick("left")
Sleep(99)
ElseIf \$search2 = 1 Then
Sleep(100)
MouseMove(\$x, \$y2, 10)
sleep(99)
MouseClick("left")
Sleep(100)
Else
Sleep(199)
\$pos = MouseGetPos()
Sleep(150)
\$x = \$pos[0]
\$y = \$pos[1]
MouseMove( \$x, \$y - 1100, 10)
Sleep(150)
MouseClick("left")
Sleep(150))
EndIf```

i cant find the problem of this specific script that doesnt work when my test ones do :/

i also tried

```Sleep(199)
\$pos = MouseGetPos()
Sleep(150)
MouseMove( \$pos[0], \$pos[1] - 1100, 10)
Sleep(150)
MouseClick("left")
Sleep(150)```

Edited by tiye0405
##### Share on other sites

Have you tried for loops?

Example of script moving from bottom to top

```For \$i = @DesktopHeight To 0 Step - 1
MouseMove(400, \$i)
Next```

36 minutes ago, tiye0405 said:

My problem is that no matter how much i subtract it always moves to the same spot.

Sleep(150)
\$pos = MouseGetPos()
Sleep(150)
MouseMove(\$pos[0], \$pos[1] - 50, 10)
Sleep(150)
MouseClick("left")
Sleep(150)

I want it to move a little higher than it already is and it works on the desktop but as soon as it's on google chrome it seems to go to the current xcoord and to the y coordinate that is supposed to be subtracted (in this case (x, 50)).

if you throw a while loop in the code like so it will work;

```While 1
Sleep(150)
\$pos = MouseGetPos()
Sleep(150)
MouseMove(\$pos[0], \$pos[1] - 50, 10)
Sleep(150)
MouseClick("left")
Sleep(150)
WEnd```

I assume the reason why it always moves the mouse to the same spot is because you already have a loop running a series of mouse clicks and every time it gets to the line:

`MouseMove(\$pos[0], \$pos[1] - 50, 10)`

it uses the previous mouse move location (not provided)... which won't work. If you provide more code I'm sure you can get an answer extremely quick

##### Share on other sites

Yes thank you for the quick reply, here is the whole code

```global \$y = 0, \$x = 0, \$x2 = \$x - 308, \$y2 = \$y + 40
Opt("MouseClickDownDelay", 30)
Global \$Paused

#include <ImageSearch2015.au3>
#include <Misc.au3>

Func End()
Exit
EndFunc

Func Pause()
\$Paused = NOT \$Paused
While \$Paused
sleep(100)
WEnd
EndFunc

Sleep(3000)
\$search1 = _WaitForImageSearch("Log.bmp", 30, 1, \$x, \$y, 10)
If \$search1 = 1 Then
Sleep(1000)
Send("{ESC}")
Sleep(200)
Send("{ESC}")
Sleep(3000)
Send("{K}")
sleep(100)
EndIf

\$search1 = _WaitForImageSearch("TxtBar.bmp", 30, 1, \$x, \$y, 30)
If \$search1 = 1 Then
MouseMove(\$x, \$y, 10)
sleep(99)
MouseClick("left")
Sleep(100)
Sleep(100)
Send("{x}")
Send("{x}")
Send("{x}")
Send("{x}")
Send("{x}")
Send("{Enter}")
Send("{x}")
Send("{x}")
Send("{x}")
Send("{x}")
Send("{x}")
Send("{x}")
Send("{x}")
Send("{x}")
Sleep(99)
Send("{Enter}")
Sleep(299)
EndIf

\$search1 =_WaitForImageSearch("name.bmp", 5, 1, \$x, \$y, 20)
\$search2 =_WaitForImageSearch("star.bmp", 5, 1, \$x, \$y, 20)
If \$search1 = 1 Then
Sleep(100)
MouseMove(\$x, \$y, 10)
sleep(99)
MouseClick("left")
Sleep(99)
ElseIf \$search2 = 1 Then
Sleep(100)
MouseMove(\$x, \$y2, 10)
sleep(99)
MouseClick("left")
Sleep(100)
Else
Sleep(199)
\$pos = MouseGetPos()
Sleep(150)
\$x = \$pos[0]
\$y = \$pos[1]
MouseMove( \$x, \$y - 1100, 10)
Sleep(150)
MouseClick("left")
Sleep(150)```

and i want it to continue afterwards just didnt get there yet

continue as in more imgsearches

Edited by tiye0405
##### Share on other sites
```HotKeySet("{F1}", "myExit")

global \$y = 0, \$x = 0, \$x2 = \$x - 308, \$y2 = \$y + 40
Opt("MouseClickDownDelay", 30)
Global \$Paused

#include <ImageSearch2015.au3>
#include <Misc.au3>

Func End()
Exit
EndFunc

Func Pause()
\$Paused = NOT \$Paused
While \$Paused
sleep(100)
WEnd
EndFunc

While 1

Sleep(3000)
\$search1 = _WaitForImageSearch("Log.bmp", 30, 1, \$x, \$y, 10)
If \$search1 = 1 Then
Sleep(1000)
Send("{ESC}")
Sleep(200)
Send("{ESC}")
Sleep(3000)
Send("{K}")
sleep(100)
EndIf

\$search1 = _WaitForImageSearch("TxtBar.bmp", 30, 1, \$x, \$y, 30)
If \$search1 = 1 Then
MouseMove(\$x, \$y, 10)
sleep(99)
MouseClick("left")
Sleep(100)
Sleep(100)
Send("{x}")
Send("{x}")
Send("{x}")
Send("{x}")
Send("{x}")
Send("{Enter}")
Send("{x}")
Send("{x}")
Send("{x}")
Send("{x}")
Send("{x}")
Send("{x}")
Send("{x}")
Send("{x}")
Sleep(99)
Send("{Enter}")
Sleep(299)
EndIf

\$search1 =_WaitForImageSearch("name.bmp", 5, 1, \$x, \$y, 20)
\$search2 =_WaitForImageSearch("star.bmp", 5, 1, \$x, \$y, 20)
If \$search1 = 1 Then
Sleep(100)
MouseMove(\$x, \$y, 10)
sleep(99)
MouseClick("left")
Sleep(99)
ElseIf \$search2 = 1 Then
Sleep(100)
MouseMove(\$x, \$y2, 10)
sleep(99)
MouseClick("left")
Sleep(100)
Else
Sleep(199)
\$pos = MouseGetPos()
Sleep(150)
\$x = \$pos[0]
\$y = \$pos[1]
MouseMove( \$x, \$y - 1100, 10)
Sleep(150)
MouseClick("left")
Sleep(150)
EndIf

WEnd

Func myExit()
Exit
EndFunc```

Have you looked at While Loops in the help file?

## Create an account

Register a new account

• ### Similar Content

• By Hermes
Hi, I am struggling in setting the value of a textarea based on the value of clipboard (that contains a long web page source codes). If I use _WD_SetElementValue, it freezes after some time, or appears to be pressing tab and goes out of focus. I can also use send keys but i need the script to run in the background.
Here is the full script:
#Include "Chrome.au3" #Include "wd_core.au3" #Include "wd_helper.au3" #Include "WinHttp.au3" #include <MsgBoxConstants.au3> #include <WinAPIFiles.au3> #include <Array.au3> #include <AutoItConstants.au3> #include <WinAPIFiles.au3> #include <GDIPlus.au3> #include <Excel.au3> Local \$sDesiredCapabilities, \$sSession SetupChrome() _WD_Startup() \$sSession = _WD_CreateSession(\$sDesiredCapabilities) _WD_LoadWait(\$sSession) _WD_Navigate(\$sSession, "http://demo.borland.com/testsite/stadyn_largepagewithimages.html") _WD_LoadWait(\$sSession) Global \$sSource = _WD_GetSource(\$sSession) Local \$Paste = ClipPut(\$sSource) Local \$sData = ClipGet() Local \$aArray = 0, _ \$iOffset = 1 While 1 \$aArray = StringRegExp(\$sData, '(?s)<p>.*</p>', \$STR_REGEXPARRAYMATCH, \$iOffset) If @error Then ExitLoop \$iOffset = @extended For \$i = 0 To UBound(\$aArray) - 1 Local \$Paste = ClipPut(\$aArray[\$i]) Local \$sRegExData = ClipGet() ;MsgBox(0, "", "\$sRegExData = " & \$sRegExData) Next WEnd _WD_Navigate(\$sSession, "https://www.w3schools.com/tags/tryit.asp?filename=tryhtml5_textarea_placeholder") _WD_WaitElement(\$sSession, \$_WD_LOCATOR_ByCSSSelector, "iframe#iframeResult") Local \$sElement1 = _WD_FindElement(\$sSession, \$_WD_LOCATOR_ByCSSSelector, "iframe#iframeResult") _WD_FrameEnter(\$sSession, \$sElement1) _WD_WaitElement(\$sSession, \$_WD_LOCATOR_ByXPath, "//html/body/textarea") \$textarea = _WD_FindElement(\$sSession, \$_WD_LOCATOR_ByXPath, "//html/body/textarea") _WD_ElementAction(\$sSession, \$textarea, 'click') ;WD SetElementValue(SsSession, Stextarea, \$sRegExData) <-- I can do this but the focus goes out, or the browser freezes _WD_FrameLeave(\$sSession) sleep(2000) Send("^v") _WD_LoadWait(\$sSession) _WD_Shutdown() Func SetupChrome() _WD_Option('Driver', 'chromedriver.exe') _WD_Option('Port', 9515) _WD_Option('DriverParams', '--log-path="' & @ScriptDir & '\chrome.log"') \$sDesiredCapabilities = '{"capabilities": {"alwaysMatch": {"goog:chromeOptions": {"w3c": true, "args":["start-maximized","disable-infobars"]}}}}' EndFunc ;==>SetupChrome Can someone help me please, or re-direct me to the right path? TIA!
• By VIP
Use MouseClick()  mybe must/need:
DLL incuded on script!
; #INDEX# =============================================================== ; Title .........: ImageSearch ; AutoIt Version : 3.x ; Language ......: English ; Description ...: Check image Appears or Not and Return the position of an image on the desktop ; Author(s) .....: Dao Van Trong - TRONG.LIVE ; ======================================================================= ; #CURRENT# ============================================================= ; _ImageSearch ; _ImageSearch_Area ; _ImageSearch_Wait ; _ImageSearch_WaitArea ; ======================================================================== ;========================================================================= ; ; Author:...........: AutoIT VietNam : Dao Van Trong - TRONG.LIVE ; Description:......: Check image Appears or Not ; Find and return the position of an image on the desktop ; Syntax:........... _ImageSearch_Area, _ImageSearch ; Parameter(s):..... \$_ImagePath: The image to locate on the desktop ; May be a list of image by delimited by "|" ; i.e: \$_ImagePath = "image1.bmp|image2.bmp|image3.bmp" ; \$P_x1 \$P_y1: Position of 1st point ; \$P_x2 \$P_y2: Position of 2nd point - Default is last botton right of desktop ; \$_Tolerance: 0 for no tolerance (0-255). Needed when colors of image differ from desktop. e.g GIF ; \$_CenterPos: boolen. True will return \$array[1] x \$array[2] is center of image found. ; False will return top-left position ; Return Value(s):.. Return an array has 3 item ; On Success: \$array[0] 1 ; On Failure: \$array[0] 0 ; DLL not found or other error: \$array[0] -1 ; \$array[1] x \$array[2]: position of image what found on desktop ; ; Note:............. Use _ImageSearch to search the entire desktop ; _ImageSearch_Area to specify a desktop region to search ; \$_ImagePath with more item need more time appear on screen before function can detect. ; Decrease sleep time in the loop to detect faster. But less performance. I.e CPULoad increased ; ;======================================================================== EG 1:
;~ Opt("MustDeclareVars", 1) ;~ #AutoIt3Wrapper_UseX64=y ;~ #AutoIt3Wrapper_Change2CUI=y #RequireAdmin #include "_ImageSearch_UDF.au3" HotKeySet("{Esc}", "_Exit") ; Press ESC for exit Func _Exit() Exit 0 EndFunc ;==>_Exit Global Const \$Ask_On_Found = 0 Global Const \$Mouse_Move_On_Found = 1 Global Const \$Mouse_Click_On_Found = 0 Global Const \$iSleep_Time=500 Global \$sCount = 0, \$_Image_1 = @ScriptDir & "\example.bmp" ; First, use this function to create a file bmp, maybe a desktop icon for example') MsgBox(64 + 262144, 'ImageSearch', 'At first, create a file bmp,' & @CRLF & 'photos that will search on the screen!') _ImageSearch_Create_BMP(\$_Image_1) ConsoleWrite("! Search for images: " & \$_Image_1 & @CRLF & '! Searching on the screen ...' & @CRLF) While 1 ToolTip('(Press ESC for EXIT) Searching ...', 1, 1) Sleep(\$iSleep_Time) \$sCount += 1 Local \$return = _ImageSearch(\$_Image_1) If \$return[0] = 1 Then ConsoleWrite('- [' & \$sCount & '] Image found:' & " X=" & \$return[1] & " Y=" & \$return[2] & @CRLF) If \$Mouse_Move_On_Found Then MouseMove(\$return[1], \$return[2]) Sleep(\$iSleep_Time) EndIf If \$Mouse_Click_On_Found Then MouseClick("left", \$return[1], \$return[2]) ToolTip('(Press ESC for EXIT) - [' & \$sCount & "] Image found:" & " X=" & \$return[1] & " Y=" & \$return[2], 1, 1) If \$Ask_On_Found Then Local \$ask = MsgBox(6 + 262144, 'Success [' & \$sCount & ']', 'Image found:' & " X=" & \$return[1] & " Y=" & \$return[2]) If \$ask = 2 Or \$ask = 3 Or \$ask = 5 Or \$ask = 7 Then Exit ;No, Abort, Cancel, and Ignore If \$ask = 10 Then _ImageSearch_Create_BMP(\$_Image_1) ; Continue ;Try Again EndIf EndIf Sleep(200) WEnd
Video demo:

[+] When any problem or error occurs, please make sure that:
- Set screen Screen Scale and layout = 100%
- Installed display driver.
- Tried turning off the antivirus
- Full installation: Microsoft Visual C++ Redistributable  2005->2022
[+] You can download the AIO version of the Visual C++ Redistributable here:
-> https://www.mediafire.com/file/0ak8dcj9mdn7nyq/VisualCppRedist_AIO_2005-2022_x86_x64_%5Btrong.live%5D.zip/file
-> FOR Windows XP: https://www.mediafire.com/file/5m5lnr1kfg73tc9/VisualCppRedist_AIO_2005-2019_x86_XP_%5Btrong.live%5D.zip/file

[+] The last full version of SCITE4AutoIT supports windows XP:

• By DJ143
I have a autoit exe file which is used in upload/browse file functionality.  This has been integrated with selenium framework and I am invoking the autoit exe using Java process and runtime.
Now the issue is when I run the scripts and invoke the autoit exe in local it works perfectly.  But when I use selenium grid or jenkins to run the scripts in another windows server it is not working.
Can anyone please suggest any solution for this?
• By Hermes
Hello, the script below will read column A from an excel file - and if a value matches in the browser, it will click the corresponding link and click on a specific button to paste the data, then writes "Completed" in Column B. It will continue to read from the excel file and do the same thing for all the remaining rows.
What i am trying figure out now is if the excel crashes while the script/loop is running, I want to relaunch the excel file continue to the last row before the excel crashed. So if the value of column B is not marked as "completed", it should continue from that row
Appreciate any help that I can get to achieve this.
table1.html test.xlsx

• Hello ,
I am trying to use Websockets in AutoIt.
It is to fetch live stock market prices , API is provided and documentation available for python language.
The link for the code snippet is :
https://symphonyfintech.com/xts-market-data-front-end-api-v2/#tag/Introduction
https://symphonyfintech.com/xts-market-data-front-end-api-v2/#tag/Instruments/paths/~1instruments~1subscription/post

https://github.com/symphonyfintech/xts-pythonclient-api-sdk

Second Link is to subscribe to a list of ExchangeInstruments.
Now I would like to get live stock ltp (LastTradedPrice) for a few stocks whose "ExchangeInstrumentID" I know.
I am able to use the WinHttp object to perform actions using simple codes like below :
I have the secretKey and appkey and can generate the needed token. And get the unique ExchangeInstrumentID.

Below code is just for example of how I am using WinHttp. Unrelated to socket part.
Global \$InteractiveAPItoken = IniRead(@ScriptDir & "\Config.ini", "token", "InteractiveAPItoken", "NA") \$baseurl = "https://brokerlink.com/interactive/" \$functionurl = "orders" \$oHTTP = ObjCreate("winhttp.winhttprequest.5.1") \$oHTTP.Open("POST", \$baseurl & \$functionurl, False) \$oHTTP.SetRequestHeader("Content-Type", "application/json;charset=UTF-8") \$oHTTP.SetRequestHeader("authorization", \$InteractiveAPItoken) \$pD = '{ "exchangeSegment": "NSEFO", "exchangeInstrumentID": ' & \$exchangeInstrumentID & ', "productType": "' & \$producttype & '", "orderType": "MARKET", "orderSide": "' & \$orderside & '", "timeInForce": "DAY", "disclosedQuantity": 0, "orderQuantity": ' & \$qty & ', "limitPrice": 0, "stopPrice": 0, "orderUniqueIdentifier": "' & \$orderidentifier & '"}' \$oHTTP.Send(\$pD) \$oReceived = \$oHTTP.ResponseText \$oStatusCode = \$oHTTP.Status

But am struggling to understand and use socket.
Would be of great help if you can have a look at the link mentioned above and help with the code sample for AutoIt.
To connect and listen to a socket.
Thanks a lot

×

• Wiki

• Back

• #### Beta

• Git
• FAQ
• Our Picks
×
• Create New...