Using PixelSearch() and relative mouse movement I made this script lock the mouse cursor onto a color :
While 1
$pos = MouseGetPos()
$coord = PixelSearch($pos[0]+80, $pos[1]+80, $pos[0]-80, $pos[1]-80, $color, 10,5)
If IsArray($coord) = 1 Then
Local $iX = $pos[0], $iY = $pos[1]
If ($iX < $coord[0]) Then _MouseMoveRelative(1, 0);$iStepSize
If ($iX > $coord[0]) Then _MouseMoveRelative(-1, 0);$iStepSize
If ($iY < $coord[1]) Then _MouseMoveRelative(0, 1);$iStepSize
If ($iY > $coord[1]) Then _MouseMoveRelatsdive(0, -1);$iStepSize
GUISetState()
EndIf
WEnd
If I increase the steps it's faster, but it moves off target. If I decrease, it's slower but more accurate. How to make this as fast as possible?
This would be the fastest way because the mouse will move instantly
While 1
$pos = MouseGetPos()
$coord = PixelSearch( $pos[0]-40, $pos[1]+40, $pos[0]+40, $pos[1]-40, $color, 10,5 )
If IsArray($coord) = 1 Then
Local $iX = $pos[0], $iY = $pos[1]
If ($iX <> $coord[0]) Then _MouseMoveRelative($coord[0] - $iX,0)
If ($iY <> $coord[1]) Then _MouseMoveRelative(0,$coord[1] - $iY)
; GUISetState() This shouldn't be inside of a loop.
EndIf
Wend
If you still want to have a $iStepSize, do it like this:
While 1
$pos = MouseGetPos()
$coord = PixelSearch( $pos[0]-40, $pos[1]+40, $pos[0]+40, $pos[1]-40, $color, 10,5 )
If IsArray($coord) = 1 Then
Local $iX = $pos[0], $iY = $pos[1]
Local $iXOffset = $coord[0] - $iX, $iYOffset = $coord[1] - $iY
ToolTip( $coord[0] & " : " & $iX & #CRLF & $coord[1] & " : " & $iY, $iX, $iY)
If $iXOffset Then
If Abs($iXOffset) < $iStepSize Then
$iMoving = $iXOffset
Else
$iMoving = $iStepSize
If ($iXOffset) < 0 Then $iMoving *= -1
EndIf
_MouseMoveRelative($iMoving, 0)
EndIf
If $iYOffset Then
If Abs($iYOffset) < $iStepSize Then
$iMoving = $iYOffset
Else
$iMoving = $iStepSize
If ($iYOffset) < 0 Then $iMoving *= -1
EndIf
_MouseMoveRelative(0, $iMoving)
EndIf
EndIf
Wend
Related
This AutoIt script counts number of words; characters (with and without spaces); lines; and estimated speaking time (assuming two words per second) for text selected by the user.
However, if the starting position is 0 for the input string (at the upper left-hand corner of the top of the file) the method returns 0 for everything, even when the main function works perfectly.
I cannot figure out why.
$input_str = GUICtrlRead($Textbox)
$selpos = _GUICtrlEdit_GetSel($Textbox)
MsgBox($MB_OK, $selpos[0], $selpos[1])
$selstring = StringMid($input_str, $selpos[0], ($selpos[1] - $selpos[0]))
$WordArray = StringRegExp($selstring, "[\s\.:;,]*([a-zA-Z0-9-_]+)[\s\.:;,]*", 3)
$SingleQuotes = StringRegExp($selstring, "'", 3)
$Result = ""
$Seconds = (UBound($WordArray) - UBound($SingleQuotes)) / 2
If $Seconds >= 3600 Then
If $Seconds / 3600 >= 2 Then
$Result = $Result & Int($Seconds / 3600) & " hours "
Else
$Result = $Result & Int($Seconds / 3600) & " hour "
EndIf
EndIf
If Mod($Seconds, 3600) >= 60 Then
If $Seconds / 60 >= 2 Then
$Result = $Result & Int(Mod($Seconds, 3600) / 60) & " minutes "
Else
$Result = $Result & Int(Mod($Seconds, 3600) / 60) & " minute "
EndIf
EndIf
If Mod($Seconds, 60) > 0 Then
If Mod($Seconds, 60) >= 2 Then
$Result = $Result & Int(Mod($Seconds, 60)) & " seconds "
Else
$Result = $Result & Int(Mod($Seconds, 60)) & " second "
EndIf
EndIf
MsgBox($MB_OK, "Selection Properties", _
"Number of characters (with spaces): " & StringLen($selstring) & #CRLF & _
"Number of Characters (without spaces): " & StringLen(StringStripWS($selstring, 8)) & #CRLF & _
"Number of words: " & (UBound($WordArray) - UBound($SingleQuotes)) & #CRLF & _
"Number of lines: " & _GUICtrlEdit_GetLineCount($selstring) & #CRLF & _
"Estimated speaking time: " & $Result _
)
If $selpos[0] = 0 then StringMid() returns an empty string since your start is out of bounds (as first position for StringMid() is 1).
$sTest = "A sample test string"
MsgBox(0, '' , StringMid($sTest , 0 , 8))
MsgBox(0, '' , StringMid($sTest , 1 , 8))
anyone help please
I have a function that outputs a list of names to an array. I've parsed the list into a string that shows exactly what I want.
I have a second list of names that I want to compare to the first list and print out the details if they are different.
Example: If string = CCleaner 5.5 then prog1 is out of date. I want it to show this
#Include <Date.au3>
#Include <MsgBoxConstants.au3>
Local $prog1 = "CCleaner 5.29"
Local $prog2 = "Defraggler 2.21"
Local $string
Local $aList = _UninstallList("", "","Publisher|InstallLocation|DisplayVersion")
for $1 = 0 to ubound($aList) - 1
$string &= $aList[$1][2] & " " & $aList[$1][6] & #CRLF
Next
msgbox(0, "", $string)
Several years ago, I wrote a function to compare lists or arrays. I think this can help you.
;==================================================================================================
; Function Name: _GetIntersection($Set1, $Set2 [, $GetAll=0 [, $Delim=Default]])
; Description:: Detect from 2 sets
; - Intersection (elements are contains in both sets)
; - Difference 1 (elements are contains only in $Set1)
; - Difference 2 (elements are contains only in $Set2)
; Parameter(s): $Set1 set 1 (1D-array or delimited string)
; $Set2 set 2 (1D-array or delimited string)
; optional: $GetAll 0 - only one occurence of every different element are shown (Default)
; 1 - all elements of differences are shown
; optional: $Delim Delimiter for strings (Default use the separator character set by Opt("GUIDataSeparatorChar") )
; Return Value(s): Succes 2D-array [i][0]=Intersection
; [i][1]=Difference 1
; [i][2]=Difference 2
; Failure -1 #error set, that was given as array, is'nt 1D-array
; Note: Comparison is case-sensitiv! - i.e. Number 9 is different to string '9'!
; Author(s): BugFix (AutoIt#bug-fix.info)
;==================================================================================================
Func _GetIntersection(ByRef $Set1, ByRef $Set2, $GetAll=0, $Delim=Default)
Local $o1 = ObjCreate("System.Collections.ArrayList")
Local $o2 = ObjCreate("System.Collections.ArrayList")
Local $oUnion = ObjCreate("System.Collections.ArrayList")
Local $oDiff1 = ObjCreate("System.Collections.ArrayList")
Local $oDiff2 = ObjCreate("System.Collections.ArrayList")
Local $tmp, $i
If $GetAll <> 1 Then $GetAll = 0
If $Delim = Default Then $Delim = Opt("GUIDataSeparatorChar")
If Not IsArray($Set1) Then
If Not StringInStr($Set1, $Delim) Then
$o1.Add($Set1)
Else
$tmp = StringSplit($Set1, $Delim)
For $i = 1 To UBound($tmp) -1
$o1.Add($tmp[$i])
Next
EndIf
Else
If UBound($Set1, 0) > 1 Then Return SetError(1,0,-1)
For $i = 0 To UBound($Set1) -1
$o1.Add($Set1[$i])
Next
EndIf
If Not IsArray($Set2) Then
If Not StringInStr($Set2, $Delim) Then
$o2.Add($Set2)
Else
$tmp = StringSplit($Set2, $Delim)
For $i = 1 To UBound($tmp) -1
$o2.Add($tmp[$i])
Next
EndIf
Else
If UBound($Set2, 0) > 1 Then Return SetError(1,0,-1)
For $i = 0 To UBound($Set2) -1
$o2.Add($Set2[$i])
Next
EndIf
For $tmp In $o1
If $o2.Contains($tmp) And Not $oUnion.Contains($tmp) Then $oUnion.Add($tmp)
Next
For $tmp In $o2
If $o1.Contains($tmp) And Not $oUnion.Contains($tmp) Then $oUnion.Add($tmp)
Next
For $tmp In $o1
If $GetAll Then
If Not $oUnion.Contains($tmp) Then $oDiff1.Add($tmp)
Else
If Not $oUnion.Contains($tmp) And Not $oDiff1.Contains($tmp) Then $oDiff1.Add($tmp)
EndIf
Next
For $tmp In $o2
If $GetAll Then
If Not $oUnion.Contains($tmp) Then $oDiff2.Add($tmp)
Else
If Not $oUnion.Contains($tmp) And Not $oDiff2.Contains($tmp) Then $oDiff2.Add($tmp)
EndIf
Next
Local $UBound[3] = [$oDiff1.Count,$oDiff2.Count,$oUnion.Count], $max = 1
For $i = 0 To UBound($UBound) -1
If $UBound[$i] > $max Then $max = $UBound[$i]
Next
Local $aOut[$max][3]
If $oUnion.Count > 0 Then
$i = 0
For $tmp In $oUnion
$aOut[$i][0] = $tmp
$i += 1
Next
EndIf
If $oDiff1.Count > 0 Then
$i = 0
For $tmp In $oDiff1
$aOut[$i][1] = $tmp
$i += 1
Next
EndIf
If $oDiff2.Count > 0 Then
$i = 0
For $tmp In $oDiff2
$aOut[$i][2] = $tmp
$i += 1
Next
EndIf
Return $aOut
EndFunc ;==>_GetIntersection
It is possible, in AU3, use WinHttp.WinHttpRequest.5.1 with a progress bar? Actually what I need is to know how many MB have already been downloaded.
I need to use "WinHttp.WinHttpRequest.5.1" because I have to use the parameter "Referer" in the header.
Of course it is
#include "WinHttp.au3"
; Download some gif
;~ http://33.media.tumblr.com/dd3ffab90cc338666f192fd86f6a4f8f/tumblr_n0pefhIpss1swyb6ao1_500.gif
; Initialize and get session handle
$hOpen = _WinHttpOpen()
; Get connection handle
$hConnect = _WinHttpConnect($hOpen, "http://33.media.tumblr.com")
; Specify the reguest
$hRequest = _WinHttpOpenRequest($hConnect, Default, "dd3ffab90cc338666f192fd86f6a4f8f/tumblr_n0pefhIpss1swyb6ao1_500.gif")
; Send request
_WinHttpSendRequest($hRequest)
; Wait for the response
_WinHttpReceiveResponse($hRequest)
;~ ConsoleWrite(_WinHttpQueryHeaders($hRequest) & #CRLF)
ProgressOn("Downloading", "In Progress...")
Progress(_WinHttpQueryHeaders($hRequest, $WINHTTP_QUERY_CONTENT_LENGTH))
Local $sData
; Check if there is data available...
If _WinHttpQueryDataAvailable($hRequest) Then
While 1
$sChunk = _WinHttpReadData_Ex($hRequest, Default, Default, Default, Progress)
If #error Then ExitLoop
$sData &= $sChunk
Sleep(20)
WEnd
Else
MsgBox(48, "Error", "Site is experiencing problems (or you).")
EndIf
Sleep(1000)
ProgressOff()
; Close handles
_WinHttpCloseHandle($hRequest)
_WinHttpCloseHandle($hConnect)
_WinHttpCloseHandle($hOpen)
; Do whatever with data, write to some file or whatnot. I'll just print it to console here:
ConsoleWrite($sData & #CRLF)
Local $hFile = FileOpen(#DesktopDir & "\test.gif", 26)
FileWrite($hFile, $sData)
FileClose($hFile)
Func Progress($iSizeAll, $iSizeChunk = 0)
Local Static $iMax, $iCurrentSize
If $iSizeAll Then $iMax = $iSizeAll
$iCurrentSize += $iSizeChunk
Local $iPercent = Round($iCurrentSize / $iMax * 100, 0)
ProgressSet($iPercent, $iPercent & " %")
EndFunc
Func _WinHttpReadData_Ex($hRequest, $iMode = Default, $iNumberOfBytesToRead = Default, $pBuffer = Default, $vFunc = Default)
__WinHttpDefault($iMode, 0)
__WinHttpDefault($iNumberOfBytesToRead, 8192)
__WinHttpDefault($vFunc, 0)
Local $tBuffer, $vOutOnError = ""
If $iMode = 2 Then $vOutOnError = Binary($vOutOnError)
Switch $iMode
Case 1, 2
If $pBuffer And $pBuffer <> Default Then
$tBuffer = DllStructCreate("byte[" & $iNumberOfBytesToRead & "]", $pBuffer)
Else
$tBuffer = DllStructCreate("byte[" & $iNumberOfBytesToRead & "]")
EndIf
Case Else
$iMode = 0
If $pBuffer And $pBuffer <> Default Then
$tBuffer = DllStructCreate("char[" & $iNumberOfBytesToRead & "]", $pBuffer)
Else
$tBuffer = DllStructCreate("char[" & $iNumberOfBytesToRead & "]")
EndIf
EndSwitch
Local $sReadType = "dword*"
If BitAND(_WinHttpQueryOption(_WinHttpQueryOption(_WinHttpQueryOption($hRequest, $WINHTTP_OPTION_PARENT_HANDLE), $WINHTTP_OPTION_PARENT_HANDLE), $WINHTTP_OPTION_CONTEXT_VALUE), $WINHTTP_FLAG_ASYNC) Then $sReadType = "ptr"
Local $aCall = DllCall($hWINHTTPDLL__WINHTTP, "bool", "WinHttpReadData", _
"handle", $hRequest, _
"struct*", $tBuffer, _
"dword", $iNumberOfBytesToRead, _
$sReadType, 0)
If #error Or Not $aCall[0] Then Return SetError(1, 0, "")
If Not $aCall[4] Then Return SetError(-1, 0, $vOutOnError)
If IsFunc($vFunc) Then $vFunc(0, $aCall[4])
If $aCall[4] < $iNumberOfBytesToRead Then
Switch $iMode
Case 0
Return SetExtended($aCall[4], StringLeft(DllStructGetData($tBuffer, 1), $aCall[4]))
Case 1
Return SetExtended($aCall[4], BinaryToString(BinaryMid(DllStructGetData($tBuffer, 1), 1, $aCall[4]), 4))
Case 2
Return SetExtended($aCall[4], BinaryMid(DllStructGetData($tBuffer, 1), 1, $aCall[4]))
EndSwitch
Else
Switch $iMode
Case 0, 2
Return SetExtended($aCall[4], DllStructGetData($tBuffer, 1))
Case 1
Return SetExtended($aCall[4], BinaryToString(DllStructGetData($tBuffer, 1), 4))
EndSwitch
EndIf
EndFunc
You here find the WinHTTP.au3 here: https://github.com/dragana-r/autoit-winhttp/releases.
It is my favorite UDF!
I have comments all around the code to explain you the process.
I have an AutoIt script that works, mostly. Reads a file, writes out what I want, but it does not preserve the original newline character. If I read a UNIX format file (LF only), it will write out a Windows format file (CR and LF).
Short of switching to something more robust, like Python, how do I solve this in AutoIt?
Opt("MustDeclareVars", 1) ;0 = no, 1 = require pre-declare
#include <File.au3>
#include <Array.au3>
Local $gInPath = $CmdLine[1]
Local $NumberOfLines = $CmdLine[2]
Local $gInDrive, $gInDir, $gInFName, $gInExt, $gOutPath
Local $gMsgBoxTitle = "Error in " & #ScriptName
Local $InLine
Local $LineCount
Local $oFileIn
Local $oFileOut
Local $FileStringAppend
If FileExists($gInPath) Then
Else
MsgBox(4096, $gMsgBoxTitle, "This file does not exist" & #CRLF & $gInPath)
Exit
EndIf
_PathSplit($gInPath, $gInDrive, $gInDir, $gInFName, $gInExt)
If $NumberOfLines >= 1000000 Then
$FileStringAppend = $NumberOfLines / 1000000 & "M"
ElseIf $NumberOfLines >= 1000 Then
$FileStringAppend = $NumberOfLines / 1000 & "K"
Else
$FileStringAppend = $NumberOfLines
EndIf
$gOutPath = _PathMake($gInDrive, $gInDir, $gInFName & "_" & $FileStringAppend, $gInExt)
If FileExists($gOutPath) Then
MsgBox(4096, $gMsgBoxTitle, "File already exists" & #CRLF & $gOutPath)
Exit
EndIf
$oFileIn = FileOpen($gInPath, 0)
$oFileOut = FileOpen($gOutPath, 1)
; Check if file opened for reading OK
If $oFileIn = -1 Then
MsgBox(4096, $gMsgBoxTitle, "Unable to open file for read" & #CRLF & $gInPath)
Exit
EndIf
; Check if file opened for writing OK
If $oFileOut = -1 Then
MsgBox(4096, $gMsgBoxTitle, "Unable to open file for write." & #CRLF & $gOutPath)
Exit
EndIf
; Read in lines of text until the EOF is reached
$LineCount = 0
While 1
$InLine = FileReadLine($oFileIn)
$LineCount += 1
If #error = -1 Then ExitLoop
If $LineCount > $NumberOfLines Then ExitLoop
FileWriteLine($oFileOut, $InLine & #CRLF)
WEnd
FileClose($oFileIn)
FileClose($oFileOut)
Looking at the function documentation at this link - https://www.autoitscript.com/autoit3/docs/functions/FileWriteLine.htm . It appears you can leave off the & #CRLF in your FileWriteLine command.
AutoIt should use the same line terminator that is read in, or
"If the line does NOT end in #CR or #LF then a DOS linefeed (#CRLF)
will be automatically added."
Here's the solution I came up. It works, but I'm not sure it's the cleanest.
Opt("MustDeclareVars", 1) ;0 = no, 1 = require pre-declare
#include <File.au3>
#include <Array.au3>
Local $gInPath = $CmdLine[1]
Local $NumberOfLines = $CmdLine[2]
Local $gInDrive, $gInDir, $gInFName, $gInExt, $gOutPath
Local $gMsgBoxTitle = "Error in " & #ScriptName
Local $InLine
Local $LineCount
Local $oFileIn
Local $oFileOut
Local $FileStringAppend
Local Const $CHAR_READ_BLOCK = 100
Local $CharsRead = 0
Local $CrFound = 0
Local $LfFound = 0
Local $Newline
Local $InBlock
If FileExists($gInPath) Then
Else
MsgBox(4096, $gMsgBoxTitle, "This file does not exist" & #CRLF & $gInPath)
Exit
EndIf
_PathSplit($gInPath, $gInDrive, $gInDir, $gInFName, $gInExt)
If $NumberOfLines >= 1000000 Then
$FileStringAppend = $NumberOfLines / 1000000 & "M"
ElseIf $NumberOfLines >= 1000 Then
$FileStringAppend = $NumberOfLines / 1000 & "K"
Else
$FileStringAppend = $NumberOfLines
EndIf
$gOutPath = _PathMake($gInDrive, $gInDir, $gInFName & "_" & $FileStringAppend, $gInExt)
If FileExists($gOutPath) Then
MsgBox(4096, $gMsgBoxTitle, "File already exists" & #CRLF & $gOutPath)
Exit
EndIf
$oFileIn = FileOpen($gInPath, 0)
$oFileOut = FileOpen($gOutPath, 1)
; Check if file opened for reading OK
If $oFileIn = -1 Then
MsgBox(4096, $gMsgBoxTitle, "Unable to open file for read" & #CRLF & $gInPath)
Exit
EndIf
; Check if file opened for writing OK
If $oFileOut = -1 Then
MsgBox(4096, $gMsgBoxTitle, "Unable to open file for write." & #CRLF & $gOutPath)
Exit
EndIf
While $CrFound = 0 And $LfFound = 0
$CharsRead += $CHAR_READ_BLOCK
$InBlock = FileRead($oFileIn, $CharsRead)
If StringRight($InBlock, 1) = #CR Then
$InBlock = $InBlock & FileRead($oFileIn, $CharsRead)
EndIf
$CrFound = StringInStr($InBlock, #CR)
$LfFound = StringInStr($InBlock, #LF)
If $CrFound > 0 And $LfFound > 0 Then
$Newline = #CRLF
ElseIf $CrFound > 0 Then
$Newline = #CR
Else
$Newline = #LF
EndIf
WEnd
; Read first line of text
$InLine = FileReadLine($oFileIn, 1)
$LineCount = 1
FileWriteLine($oFileOut, $InLine & $Newline)
; Read in lines of text until the EOF is reached
While 1
$InLine = FileReadLine($oFileIn)
$LineCount += 1
If #error = -1 Then ExitLoop
If $LineCount > $NumberOfLines Then ExitLoop
FileWriteLine($oFileOut, $InLine & $Newline)
WEnd
FileClose($oFileIn)
FileClose($oFileOut)
So I am in need of replacing string text with autoit using stringreplace but I need to randomize the output.
An example of what I need is
Stringreplace($string, "and", {also|as well})
My ultimate goal is to randomly replace the text with the following options based on the word "and" with also or as well
I wrote this a long time ago.
It will convert this
My name is {John|Peter|Mark}! {Regards|Cheers|Get lost}!
to something like this
My name is John! Cheers!
It works with line breaks also.
Func SpintaxToTXT($TextWithSpintax)
Dim $MSGMSG
Dim $lines
$lines = StringSplit($TextWithSpintax, #LF)
For $z = 1 To $lines[0]
If $z > 1 Then $MSGMSG &= #LF
$d = StringSplit($lines[$z], "{}")
For $i = 1 To $d[0]
$MSGSplit = StringSplit($d[$i], "|")
If #error Then
$MSGMSG &= $MSGSplit[1]
ContinueLoop
EndIf
$MSGMSG &= $MSGSplit[Random(1, $MSGSplit[0], 1)]
Next
Next
Return $MSGMSG
EndFunc ;==>SpintaxToTXT
Try this:
Global $options_A = StringSplit('also,as well,bla,blubb,that,this', ',', 2)
For $i = 0 To 20
ConsoleWrite($options_A[Random(0, UBound($options_A) - 1, 1)] & #CRLF)
Next
Here's one I made earlier. Works exactly the same as StringReplace except instead of taking a replacement string, it takes a function that returns the replacement string. The using Xenobiologist's array method, you get the desired result.
Local $sTest = "The cat and the dog and the rat."
ConsoleWrite(_StringReplaceCallback($sTest, "and", _MyCallback) & #LF)
Func _MyCallback($s)
Local Static $aOptions = StringSplit("also|as well", "|")
Return $aOptions[Random(1, $aOptions[0], 1)]
EndFunc ;==>_MyCallback
Func _StringReplaceCallback($sString, $sFind, $funcReplace, $iOccurence = 0, $iCaseSense = 0)
Local $sRet = ""
Local $iDir = 1
Local $iPos = 1
If $iOccurence < 0 Then
$iDir = -1
$iPos = StringLen($sString)
EndIf
If $iOccurence = 0 Then $iOccurence = $iDir * StringLen($sString)
While 1
$i = StringInStr($sString, $sFind, $iCaseSense, $iDir, $iPos)
If $iDir > 0 Then
If Not $i Or Not $iOccurence Then
$sRet &= StringMid($sString, $iPos)
ExitLoop
EndIf
$sRet &= StringMid($sString, $iPos, $i - $iPos) & $funcReplace($sFind)
$iPos = $i + StringLen($sFind)
Else
If Not $i Or Not $iOccurence Then
$sRet = StringMid($sString, 1, $iPos) & $sRet
ExitLoop
EndIf
$sRet = $funcReplace($sFind) & StringMid($sString, $i + StringLen($sFind), $iPos - $i - StringLen($sFind) + 1) & $sRet
$iPos = $i - 1
EndIf
If $iOccurence <> 0 Then $iOccurence -= $iDir
WEnd
Return $sRet
EndFunc ;==>_StringReplaceCallback
It's probably worth noting that there is actually a simpler solution for this, provided the replacement strings don't ever include "and" (or else you'll have an infinite loop), that just replaces one instance at a time:
Local $sString = "The cat and the dog and the rat."
Local $aOptions = StringSplit("also|as well", "|")
Do
$sString = StringReplace($sString, "and", $aOptions[Random(1, $aOptions[0], 1)], 1)
Until Not #extended
ConsoleWrite($sString & #LF)