Why does my AutoIt script compile but doesn't do anything? - autoit

My AutoIt script compiles but doesn't do what it's supposed to do. Code:
HotKeySet(+"{G}","gather")
While 1
sleep(100)
WEnd
Global $charRunTimeS = 2;
Global $collectKey = "{LWIN}";
Global $vehicleInvKey = "{T}";
Global $charRunTimeMS = $charRunTimeS * 1000
Global $posItemX = 820;
Global $posItem1Y = 300;
Global $posItem2Y = 340;
Global $posItem3Y = 365;
Global $posItem4Y = 397;
Global $posBtnStore = 678;
Global $posItemSelectedY = $posItem4Y
Func gather()
$counter = 0;
While $counter <= 2
Send("{s down}")
Sleep($charRunTimeMS)
Send("{s up}")
$counter1 = 0;
While $counter1 <= 4
Send($collectKey)
Sleep(100)
Send($collectKey)
Sleep(30000)
$counter1 = $counter1 + 1
WEnd
Send("{w down}")
Sleep($charRunTimeMS)
Send("{w up}")
Send($vehicleInvKey)
MouseClick("primary", $posItemX, $posItemSelectedY)
$counter2 = 0;
While $counter2 <= 30
MouseClick($posItemX, $posBtnStore)
$counter2 = $counter2 + 1
WEnd
Send("{ESC}")
$counter = $counter + 1
WEnd
EndFunc
Output from de-bug. First line is when compiling and running, the second line is when I stop the code:
"C:\Program Files (x86)\AutoIt3\SciTE..\autoit3.exe" /ErrorStdOut "C:\Users\RichusX\Desktop\AltisGatherScript.au3"
Process failed to respond; forcing abrupt termination...
Exit code: 1 Time: 14.39

I guess all you need to do is write the call like this:
HotKeySet("G", "gather")
You do not need the +, because of the capital G. If you want to then you have to put it into to "". Have a look at the send function.

Related

Object Shell.Application Open Folder In Same Explorer Window

I want to ask you a question regarding Shell.Application COM Object. The programming language I work in is AutoIT. I would like to know, how can I open a folder in the exact same Window a previous folder has been opened.
For instance, if I open the C:\Folder in a Window and it has a subfolder, then how can I make the Script open that subfolder in the same window the "parent folder" has been opened, without creating a new window for that. The only way I can open a Folder is like this:
global $ShellApplication = ObjCreate("Shell.Application")
$ShellApplication.Explore("C:\Folder")
Sleep(100)
$ShellApplication.Explore("C:\Folder\Subfolder")
but this way is the wrong way. I hope someone has got any idea about how this could be achieved.
I don't know whether this is what you want, but you can automate the current window like this:
#include <Array.au3>
Opt("SendKeyDelay", 1) ;5 milliseconds
Opt("SendKeyDownDelay", 1) ;1 millisecond
$pid = ShellExecute("explorer.exe ", #DesktopDir)
Sleep(300)
;~ $aWinList=WinList("[REGEXPCLASS:(Explore|Cabinet)WClass]")
;~ _ArrayDisplay($aWinList)
ConsoleWrite($pid & #CRLF)
$handle = _WinGetHandleByPID($pid)
Sleep(2000)
SendKeepActive($handle)
Send('{F4}')
Send('^a')
Send(#AppDataCommonDir & "{ENTER}")
Func _WinGetHandleByPID($vProcess, $nShow = 1)
; Get Window Handle by PID
; Author Hubertus
; derived from Smoke_N's script with the same name,
; but to handle more than one process with the same name
; and to return handle of newest window ($nShow = 2).
;
; $vProcess = Processname(e.g. "Notepad.exe") or Processnumber(e.g. 4711 )
; $nShow = -1 "All (Visble or not)", $nShow = 0 "Not Visible Only", $nShow = 1 "Visible Only", $nShow = 2 "return handle of newest window "
; #error = 0 Returns array of matches. Array[0][0] and #extended shows number of matches.
; #error = 0 and $nShow = 2 return handle of newest window
; Array[n][0] shows windows title. Array[n][1] shows windows handle. n = 1 to #extended
; #error = 1 Process not found.
; #error = 2 Window not found.
If Not ProcessExists($vProcess) Then Return SetError(1, 0, 0) ; no matching process
Local $iWinList, $aWinList = WinList()
Local $iResult, $aResult[UBound($aWinList)][2]
Local $iProcessList, $aProcessList = ProcessList($vProcess)
If $aProcessList[0][0] = 0 Then Local $aProcessList[2][2] = [[1, 0], ["", $vProcess]]
For $iWinList = 1 To $aWinList[0][0]
For $iProcessList = 1 To $aProcessList[0][0]
If WinGetProcess($aWinList[$iWinList][1]) = $aProcessList[$iProcessList][1] Then
If $nShow > -1 And Not $nShow = (2 = BitAND(WinGetState($aWinList[$iWinList][1]), 2)) Then ContinueLoop
$iResult += 1
$aResult[$iResult][0] = $aWinList[$iWinList][0]
$aResult[$iResult][1] = $aWinList[$iWinList][1]
EndIf
Next
Next
If $iResult = 0 Then Return SetError(2, 0, 0) ; no window found
ReDim $aResult[$iResult + 1][2]
$aResult[0][0] = $iResult
If $nShow = 2 Then Return SetError(0, $iResult, $aResult[1][1])
Return SetError(0, $iResult, $aResult)
EndFunc ;==>_WinGetHandleByPID

Autoit - how to find dupes in two strings

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

Autoit Error with iframe

$aArray = _IEFrameGetCollection($ObjIE)
$max = #extended
ConsoleWrite("Array Length: " & $max & #CR)
For $item in $aArray
ConsoleWrite("Value" & $item.src)
Next
The code causes the following error:
--> COM Error Encountered in ITSM-GUI-Automation.au3
----> $IEComErrorScriptline = 106
----> $IEComErrorNumberHex = 80020003
----> $IEComErrorNumber = -2147352573
----> $IEComErrorWinDescription = Member not found.
----> $IEComErrorDescription =
----> $IEComErrorSource =
----> $IEComErrorHelpFile =
----> $IEComErrorHelpContext = 0
----> $IEComErrorLastDllError = 0
The weird thing is that $max is getting set to 3 but then it cannot find the iframes. How can it find it and immediately not find it?
This is because of my lack of understanding of the Autoit objects. This does not return an array of objects but a collection which must be accessed through a different way.
Hope this helps someone else.
$collection = _IEFrameGetCollection($ObjIE)
$max = #extended
ConsoleWrite("Array Length: " & $max & #CR)
For $item = 0 to $max - 1
$obj = IEFrameGetCollection($ObjIE, $item)
ConsoleWrite("Value" & $item.src)
Next
#include <IE.au3>
Local $oIE = _IE_Example("frameset")
Local $oFrames = _IEFrameGetCollection($oIE)
Local $iNumFrames = #extended
For $i = 0 To ($iNumFrames - 1)
Local $oFrame = _IEFrameGetCollection($oIE, $i)
$FrameHTML = _IEDocReadHTML($oFrame); or
$FrameHTML = _IEPropertyGet($oFrame, "innerhtml"); or
$FrameHTML = _IEPropertyGet($oFrame, "outerhtml")
ConsoleWrite($FrameHTML & #LF)
Next

My script is returning 0 even though the CSV source is not empty

I'm in the process of making a small program to tell me on the fly how many employees are currently punched in via AvayaCMS Reporting.
Right now, the way that it is setup is that I have an Avaya script to take a quick report and export it in a CSV that is used by my Autoit script.
In terms of debugging it, I feel like I missing something and need another set of eyes.
launching the Staffing.au3 triggers the CSV script I'm using against the report. Even having the exact same data my message box still reports "0"
#include <Array.au3>
#include <CSV.au3>
$i_AgentCount = AvayaData(#ScriptDir & '\Report.csv', #ScriptDir & '\Name List.csv')
MsgBox(0x40, "", $i_AgentCount)
Func AvayaData($s_ReportSource, $s_NameList)
$av_LiveData = _ParseCSV($s_ReportSource)
If #error Then Return -1
$av_NameList = _ParseCSV($s_NameList)
If #error Then Return -1
Local $i_AgentCount = 0
For $i_RowCnt = 1 To (UBound($av_LiveData, 1) - 1) Step +1
For $i_AgtLst = 1 To (UBound($av_NameList) - 1) Step +1
If StringStripWS($av_LiveData[$i_RowCnt][1], 3) = StringStripWS($av_NameList[$i_AgtLst][0], 3) Then
$i_AgentCount += 1
EndIf
Next
Next
;Return the Agent Count
Return $i_AgentCount
EndFunc
Name List.csv
Agent Name
"Doe, Jane"
"Doe, John"
Report.csv
,Agent Name,Login ID,Extn,AUX Reason,State,Split/Skill,Time,VDN Name
5,"Doe, John",5930001,1000001,7,AUXOUT,999,51:32:00,
2,"Doe, Jane",5930002,1000002,7,AUXOUT,999,52:32:00,
Tested! It works well with the files supplied (copied them and names them as in your script)
please note the followings
_ParseCsv() has been rewritten add parameters like $Dchar as delimeter etc. (see script)
Note how I locate the names in the file (you can easily add feature to extend search but it won't be becessary)
msgboxes are for explanatory purposes; comment them out if not need them anymore
array.au3 is not needed
and the code:
$i_AgentCount = AvayaData(#ScriptDir & '\Report.csv', #ScriptDir & '\Name List.csv')
MsgBox(0x40, "", $i_AgentCount)
Func AvayaData($s_ReportSource, $s_NameList)
$av_LiveData = _ParseCSV($s_ReportSource,",","oops...",True)
If #error Then Return -1
$av_NameList = _ParseCSV($s_NameList,",","oops: 2",True)
If #error Then Return -1
MsgBox(0,default,"$av_NameList (Number of lines) -> " & $av_NameList[0][0])
MsgBox(0,default,"$av_NameList (Number of data in each line) -> " & $av_NameList[0][1])
MsgBox(0,default,"$av_LiveData (Number of lines) -> " & $av_LiveData[0][0])
MsgBox(0,default,"$av_LiveData (Number of data in each line) -> " & $av_LiveData[0][1])
Local $i_AgentCount = 0
for $i = 1 to $av_NameList[0][0] Step 1
For $j= 1 To $av_LiveData[0][0] Step 1
;we can have names from $av_NameList as well from $av_LiveData but in Live Data 2nd abd 3rd values are the names
MsgBox(0,default,$av_NameList[$i][1] & $av_NameList[$i][2] & " <------------> " & $av_LiveData[$j][2] & $av_LiveData[$j][3])
;~ let's compare them if matched with any of the liveData lines
If StringCompare($av_NameList[$i][1] & $av_NameList[$i][2],$av_LiveData[$j][2] & $av_LiveData[$j][3]) == 0 Then
$i_AgentCount += 1
MsgBox(0,default,"Match found: counter: " & $i_AgentCount)
EndIf
Next
Next
;Return the Agent Count
Return $i_AgentCount
EndFunc
Func _ParseCSV($f,$Dchar,$error,$skip)
Local $array[500][500]
Local $line = ""
$i = 0
$file = FileOpen($f,0)
If $file = -1 Then
MsgBox(0, "Error", $error)
Return False
EndIf
;skip 1st line (since it is the header)
If $skip Then $line = FileReadLine($file)
While 1
$i = $i + 1
Local $line = FileReadLine($file)
If #error = -1 Then ExitLoop
;skip 1st line
$row_array = StringSplit($line,$Dchar)
If $i == 1 Then $row_size = UBound($row_array)
If $row_size <> UBound($row_array) Then MsgBox(0, "Error", "Row: " & $i & " has different size ")
$row_size = UBound($row_array)
$array = _arrayAdd_2d($array,$i,$row_array,$row_size)
WEnd
FileClose($file)
$array[0][0] = $i-1 ;stores number of lines
$array[0][1] = $row_size -1 ; stores number of data in a row (data corresponding to index 0 is the number of data in a row actually that's way the -1)
Return $array
EndFunc
Func _arrayAdd_2d($array,$inwhich,$row_array,$row_size)
For $i=1 To $row_size -1 Step 1
$array[$inwhich][$i] = $row_array[$i]
Next
Return $array
EndFunc
#region ;************ Includes ************
#include "csv.au3"
#include <Array.au3>
#endregion ;************ Includes ************
$i_AgentCount = AvayaData(#ScriptDir & '\Report.csv', #ScriptDir & '\Name List.csv')
MsgBox(0x40, "", $i_AgentCount)
Func AvayaData($s_ReportSource, $s_NameList)
Local $i_AgentCount = 0
Local $s = FileRead($s_NameList)
Local $loggedInNames = FileRead($s_ReportSource)
$av_NameList = _ParseCSV($s_NameList)
If #error Then Return -1
;~ _ArrayDisplay($av_NameList)
ConsoleWrite($s & #CRLF)
For $i = 1 To UBound($av_NameList) - 1
ConsoleWrite($i & " " & $av_NameList[$i][0] & #CRLF)
If StringInStr($s, $av_NameList[$i][0]) <> 0 Then
$i_AgentCount += 1
EndIf
Next
Return $i_AgentCount
EndFunc ;==>AvayaData

Which was the key pressed in HotKeySet function

If I have many calls to HotKeySet pointing to the same function, how I know which hotkey called it?
You can use the #HotKeyPressed variable.
for example:
$char = 0
while $char < 126
HotKeySet(Chr ( $char ),"writekeys")
$char = $char + 1
WEnd
while 1
sleep(10)
WEnd
Func writekeys()
HotKeySet(#HotKeyPressed)
send(#HotKeyPressed)
;do something
HotKeySet(#HotKeyPressed,"writekeys")
EndFunc

Resources