Create general purpose WinWaitActive in AutoIT - automation

I'm thinking of using the WinWaitActive function to play a sound file every time the currently active program finished a task (ie. After Effect finished rendering, Word finished saving, etc.).
From what I know about WinWaitActive, I'll need to write a script for each internal process of each application. That's a huge amount of automation scripts. Is there any way to make a single script that will work with all programs and processes?

Try something like this:
#include<array.au3>
Global $window_Array[1]
$window = "dokument - wordpad"
checkWin()
;ConsoleWrite(_ArrayToString($window_Array, #CR))
Func checkWin()
Global $var = WinList()
For $i = 1 To $var[0][0]
If $var[$i][0] <> "" Then _ArrayAdd($window_Array, $var[$i][0])
Next
If _ArraySearch($window_Array, $window, 0, 0, 0) <> - 1 Then
MsgBox(0, "", "Found!")
Else
MsgBox(0, "", "Not Found!")
EndIf
EndFunc ;==>checkWin

Related

Error - Unexpected } at the end of While Loop

I'm using while true loop to constantly check run this script. After adding the if expression with whitelist ahk gives an error about unexpected } at the end of code. As far as I know there should be }.
Using Loop command gives the same error.
Removing the problematic } at the end causes the code running in background but not doing anything.
;Setup
Sleep, 1000
whiteList = "none"
;Main Loop
While True
{
siteName = YouTube
WinGetActiveTitle, tabName
Sleep, 10000
if tabName = %whiteList%{
Continue
}
;If current website is Youtube, ask if am I supposted to be here
if InStr(tabName, siteName){
Sleep, 10000
MsgBox, 292, Reality Check, Should you do this?
IfMsgBox, Yes
{
whiteList = tabName
}
;Close tab in mozilla
else
{
WinActivate, %tabName%
Sleep, 10
Send ^w
}
}
}
Code is unfinished, It should run in background and when the user is using YouTube fore some time it should ask him whether is he supposted to watch Youtube.
If he clicks yes, the program should ignore that specific page.
Else it should close it.
The % around the whiteList-var shouldn't be there. When I wrote it like below it seems to work:
if tabName = whiteList
Continue
Also the whiteList = tabName will just assign the string "tabName" to whiteList. Use whiteList := tabName for assigning, and if var1 = var2 for comparison.

Unable to save the file in specified location using Autoit

Followed the below steps to save a file in desired location:
Step1: Save As window getting opened(with the default downloads location, with file name as DOC)
Step2: entering the file name as "D:\temp\sample.pdf" (which is getting entered in the edit bar)
Step3: clicking the save button (button clicked, file downloaded in the default location rather than the "D:\temp" location)
I have created an .exe with the below .au3 script
WinWait("[CLASS:#32770]","",10)
Sleep(2000)
ControlSetText("Save As", "", "Edit1", $CmdLine[1])
Sleep(5000)
ControlClick("Save As", "", "Button1");
Sleep(5000)
On clicking save, it is getting saved in the default location rather than the specified location.
The below code, executing the script.
IO.popen('autoit_script.exe D:\temp') #Ruby Code
Is there a way to sort it out?
It depends on the software you are trying to automate but usually this happens because the software is not recognizing there is a change in the file save path box. The problem is in how ControlSetText works. Try using ControlSend with some error checking to make sure the file path you are try to set is getting put in right. Sometimes you have to play with a few different variations to see what works with the software you are automating. Here are two examples you can try:
Example one:
WinWait("[CLASS:#32770]", "", 10)
If Not #error Then ;make sure the window was found
$hWin = WinGetHandle("[CLASS:#32770]") ;get window handle
ControlSetText($hWin, "", "Edit1", $CmdLine[1]) ;set text
ControlFocus($hWin, "", "Edit1") ;just to make sure we have focus
ControlSend($hWin, "", "Edit1", "{ENTER}")) ;should work like click button 1 but you will have to check
;better then a sleep
$hTimer = TimerInit() ; Begin the timer and store the handle in a variable.
Do
Until WinExists($hWin) = 0 Or TimerDiff($hTimer) >= 10000
EndIf
Example two:
WinWait("[CLASS:#32770]", "", 10)
If Not #error Then ;make sure the window was found
$hWin = WinGetHandle("[CLASS:#32770]") ;get window handle
While 1
ControlSetText($hWin, "", "Edit1", "") ;just makes sure there is no text in the control text
ControlFocus($hWin, "", "Edit1") ;just to make sure we have focus
ControlSend($hWin, "", "Edit1", $CmdLine[1])) ;set text using ControlSend
If ControlGetText($hWin, "", "Edit1") = $CmdLine[1] Then ExitLoop ;makes sure that the control got ALL of the text before exiting loop
WEnd
ControlClick($hWin, "", "Button1");
;better then a sleep
$hTimer = TimerInit() ; Begin the timer and store the handle in a variable.
Do
Until WinExists($hWin) = 0 Or TimerDiff($hTimer) >= 10000
EndIf

Autoit PixelGetColor not working

Hello there everyone,
I have recently downloaded a bot for a game that I play, this bot uses Autoit to automate movements on screen, alongside Autoit I downloaded some scripts that allow me to automate in-game movements. I downloaded these scripts on my main pc and my laptop so I can run them on 2 accounts if need be. The download is exactly the same and although the bot works perfectly on my PC, it does something weird on my laptop:
The cursor seems to be able to go to places I have specificly told it to go but once it needs to imagesearch or pixelsearch it simply stops. The program doesnt stop running and will continue performing the actions that it can perform, at the right timing, however all imagesearching functions get left out.
The only major diference between the pc and laptop is that the laptop is running windows 8.
Things Iv tried by searching prior to asking here:
I have read that imagesearch.dll should be in sysWOW64 in order for it to work and I beleive I have done that by copying the DLL files and pasting them in syswow64. That did not help.
I also tried adding #requireadmin to the top of the script which did not help either.
this is happening with ALL the scripts I downloaded. They all work on pc and none work on laptop. I dont have alot of understanding with this code as I did not write it myself. However I have familirized myself with it a little as I do edit it for my personal uses on my pc and make it work very well. So I thought that it not functioning on my laptop must be some amateur mistake about windows 8 or similar.
here is an example script.
#RequireAdmin
#include <Misc.au3>
HotKeySet("{[}", "MakeLock")
HotKeySet("{z}", "Start")
HotKeySet("{ESC}", "_Exit")
HotKeySet("{x}", "Shafts")
HotKeySet("{c}", "Heads")
HotKeySet("{v}", "bsb")
$handle = WinGetHandle("Wurm Online", "")
Global $running=0
Global $shaftLoc
Global $headLoc
Global $shaft=0
Global $head=0
Global $bsb=0
Global $bsbloc
Func _Exit()
Exit
EndFunc ;==>_Exit
Func Getfrombsb()
MouseMove($bsbloc[0], $bsbloc[1], 5)
MouseDown("left")
Sleep(100)
MouseMove($shaftloc[0], $shaftloc[1], 5)
Sleep(100)
MouseUp("left")
sleep(2500)
send ("4")
sleep(250)
Send("{Enter}")
sleep(3000)
EndFunc
Func Shafts()
MsgBox(0, "", "title of items")
$shaft=1
EndFunc
Func Drop()
MouseMove($shaftloc[0], $shaftloc[1], 5)
MouseDown("left")
Sleep(250)
MouseMove($bsbloc[0], $bsbloc[1], 5)
MouseUp("left")
EndFunc
Func bsb()
MsgBox(0, "", "item in bsb")
$bsb=1
EndFunc
Func Heads()
MsgBox(0, "", "Click on what ur continuing")
$head=1
EndFunc
Func continue()
MouseMove($shaftloc[0], $shaftloc[1], 5)
MouseClick("left")
Sleep(100)
MouseMove($shaftloc[0], $shaftloc[1], 5)
MouseClick("right")
Sleep(1000)
$line = GetLine()
If IsArray($line) Then
Local $random2 = Random(200, 250, 1)
MouseMove($line[0], $line[1]+3*16-8, 5)
Sleep($random2-51)
MouseMove($line[0]+120, $line[1]+3*16-8, 5)
Sleep($random2+38)
MouseMove($line[0]+120, $line[1]+3*16-8+2*16, 5)
Sleep(500)
MouseMove($line[0]+200, $line[1]+3*16-8+2*16, 5)
Sleep($random2+161)
MouseMove($line[0]+200, $line[1]+3*16-8+2*16+44, 25)
Sleep($random2)
MouseClick("left")
EndIf
EndFunc
Func Start()
if $running==0 Then
$running=1
TrayTip("", "Macro started", 5)
ElseIf $running==1 Then
$running=0
TrayTip("", "Macro stopped", 5)
EndIf
EndFunc
Func GetLine()
$pos = MouseGetPos()
$samp = $pos
For $i = 0 To 48 Step 1
$samp[0] = $pos[0]+32
$samp[1] = $pos[1]+$i
$s0 = PixelGetColor($samp[0], $samp[1], $handle)
If $s0 == 0xFFFFFF Then
$s1 = PixelGetColor($samp[0]+1, $samp[1], $handle)
If $s1 == 0xFFFFFF Then
$s2 = PixelGetColor($samp[0]+2, $samp[1], $handle)
$s3 = PixelGetColor($samp[0]+3, $samp[1], $handle)
If $s2 == 0xFFFFFF AND $s3 == 0xFFFFFF Then
$line = $samp
ExitLoop
EndIf
EndIf
EndIf
$line = 0
Next
Return $line
EndFunc
While (1)
if _IsPressed("01") And $shaft==1 Then
$shaftloc = MouseGetPos()
$shaft=0
MsgBox(0, "", "shaft loc loaded")
endif
if _IsPressed("01") And $head==1 Then
$headloc = MouseGetPos()
$head=0
MsgBox(0, "", "head loc loaded")
EndIf
if _IsPressed("01") And $bsb==1 Then
$bsbloc = MouseGetPos()
$bsb=0
MsgBox(0, "", "bsb loc loaded")
EndIf
if $running==1 Then
Getfrombsb()
Local $random = Random(24000, 25000, 1)
continue()
Sleep($random)
sleep(500)
Drop()
sleep(2000)
Drop()
sleep(2000)
EndIf
Sleep(10)
WEnd
After watching exactly what it does and does not do, I can see that problem arrises with the section of code below it does everything up until; $line = GetLine()
$line = GetLine()
If IsArray($line) Then
Local $random2 = Random(200, 250, 1)
MouseMove($line[0], $line[1]+3*16-8, 5)
Sleep($random2-51)
MouseMove($line[0]+120, $line[1]+3*16-8, 5)
Sleep($random2+38)
MouseMove($line[0]+120, $line[1]+3*16-8+2*16, 5)
Sleep(500)
MouseMove($line[0]+200, $line[1]+3*16-8+2*16, 5)
Sleep($random2+161)
MouseMove($line[0]+200, $line[1]+3*16-8+2*16+44, 25)
Sleep($random2)
MouseClick("left")
EndIf
EndFunc
Any help would be appreciated. And although I am not familiar with code that much I do know how to use stack overflow well as my brother is a .net developer. I will mark the question that helps me and upvote and such.
Kind regards.
I know what your problem is.. I think. Try turning the output of PixelGetColor() into Hex.
Also, when it outputs, it doesnt include the "0x" at the beginning of the color tag.
So, you could say:
#Include <Misc.au3> ;for the _ispressed() function
Do
Until _IsPressed("01") ;wait for click
$mouse = MouseGetPos() ;Get cursor info
$color = Hex(PixelGetColor($mouse[0], $mouse[1]), 6) Get info on the pixel under the mouse.
;Lets just say the color is white
If $color == "FFFFFF" Then
MsgBox(0, "Success", "Colors match!")
EndIf
As for the ImageSearch not working, that could be a problem with AutoHotKey's compatability with Windows 8. Because of AutoIt not having a ImageSearch function, developers use AutoHotKey's ImageSearch function, and wrap it up in an AutoIt library. That may be where the problem is cominh

Lua script - Coding a scenario

Situation :
There are two sensors and I want to save the data of values of each sensor in the certain file..But it's not working. I am working on linux system and the file is still empty.
What's wrong with my code? any suggestion please?
my code is:
--Header file
require("TIMER")
require("TIMESTAMP")
require("ANALOG_IN")
function OnExit()
print("Exit code...do something")
end
function main()
timer = "TIMER"
local analogsensor_1 = "AIR_1"
local analogsensor_2 = "AIR_2"
local timestr = os.data("%Y-%m-%d %H:%M:%S")
-- open the file for writing binary data
local filehandle = io.open("collection_of_data.txt", "a")
while true do
valueOfSensor_1 = ANALOG_IN.readAnalogIn(analogsensor_1);
valueOfSensor_2 = ANALOG_IN.readAnalogIn(analogsensor_2);
if (valueOfSensor_1 > 0 and valueOfSensor_2 > 0) then
-- save values of sensors
filehandle:write(timestr, " -The Value of the Sensors: ", tostring(valueOfSensor_1), tostring(valueOfSensor_2)"\n");
end
TIMER.sleep(timer,500)
end
-- close the file
filehandle:close()
end
print("start main")
main()
I do not know what this libs realy do.
But this code is incorrect;
1) you do not close while statement.
if in real code you close it before filehandle:close() then try call filehandle:flush()
2) you forgot comma:
filehandle:write(timestr, " -The Value of the Sensors: ", tostring(valueOfSensor_1), tostring(valueOfSensor_2)"\n")
(it should seay something like attemt call a number value).
3) try print out valueOfSensor_1 and valueOfSensor_2 values. May be there no data.
Beside the typos pointed out by #moteus, shouldn't this:
if (valueOfSensor_1 and valueOfSensor_2 > 0) then
be like this?
if (valueOfSensor_1 > 0 and valueOfSensor_2 > 0) then
Edit, in response to your comment to another answer:
still error..it says "attempt to call field 'data' (a nil value)
I can't be sure without the stack trace, but, most likely, something bad happens in the ANALOG_IN library code. You may not be using it properly.
try to turn this:
valueOfSensor_1 = ANALOG_IN.readAnalogIn(analogsensor_1);
valueOfSensor_2 = ANALOG_IN.readAnalogIn(analogsensor_2);
into this:
success, valueOfSensor_1 = pcall(ANALOG_IN.readAnalogIn, analogsensor_1);
if not success then
print("Warning: error reading the value of sensor 1:\n"..valueOfSensor_1)
valueOfSensor_1 = 0
end
success, valueOfSensor_2 = pcall(ANALOG_IN.readAnalogIn, analogsensor_2);
if not success then
print("Warning: error reading the value of sensor 2:\n"..valueOfSensor_2)
valueOfSensor_2 = 0
end
If the failure in ANALOG_IN is not systematic, it will work around it. If the call fails systematically, you'll get a huge warning log, and an empty collection_of_data.txt.
Please note that ANALOG_IN is not a standard Lua library. You should check its documentation , and pay attention to the details of its usage.

Opening an MS-Cash Drawer, Wrong Code? Bad Code?

Humbling expierence here and I think this one will make a fool of me, but...I'm trying to convert an ancient cash register program to .net. Conquered everything else, but I can't pop open the cash register. Its connected to COM1, you are supposed to send a "trigger" text down COM1 that will cause the register to open.
Here is the .net code.
MsgBox("Opening Drawer")
Dim port As System.IO.Ports.SerialPort
port = New System.IO.Ports.SerialPort("Com1")
port.PortName = "COM1"
port.BaudRate = 9600
port.Parity = IO.Ports.Parity.None
port.DataBits = 8
port.StopBits = IO.Ports.StopBits.One
'port.Handshake = IO.Ports.Handshake.RequestToSend
port.RtsEnable = True
'port.DtrEnable = True
port.Open()
If port.IsOpen Then
'MsgBox("Attempt 1")
port.Write("####################")
MsgBox("Signal Sent: " & Chr(65))
Else
MsgBox("Port is not open")
End If
port.Close()
MsgBox("Pop, durn it!")
I get msgboxes "Signal Sent", "Done Pop Drawer"
Dang thing, just won't pop. It's an MS-Cash Drawer (EP125KC). Definitely connected to COM1, definitely has power. Chr(65) is the old code used to pop drawer and it works:
Open drawerComPort For Output Access Write As #1
Print #1, Chr$(65); "A";
Close #1
NOTE: The above code worked successfully. The root problem was caused by a reveresed power cord (negative was on the wrong side).
Thanks for all the help guys!
You've set your handshake to None but the cash drawer probably has its own idea. Also set DtrEnable to True. Chr(65) is the ASCII code for an "A", your VB code suggests the real command is "AA".
The manual documents that the cash drawer auto-tunes its baudrate. It recommends sending at least 20 # characters. And that the real command is Ctrl+G (Chr(7)). The "AA" command might have worked previously due to a baudrate mismatch. Perhaps.
If I remember my very rusty BASIC.
Print #1, Chr$(65); "A";
means print to port1 the character 65 followed by the string "A", Now the character 65 is 'A', so this looks to me like you should be sending "AA" to port1
port.Write("AA");
or alternately,
port.Write(new byte[]{65,'A'}, 0, 2);
It might be sending Unicode 65, which would be 0065, which would not end well.
Just a thought, can you try sending a raw int?
I dont use .net, but is the port buffered? do you need to send a flush/fflush()?
Are you sure you're supposed to send out this code? I would have always thought that the code is prefixed by ESC i.e. 0x1b hexadecimal...for cash drawers...
"\x1bA"
Interesting that double 'A' is used...oh well... :)
Edit: After thinking about this I realized there is another way of doing it, read on...
I have modified your original BASIC code with a bit of bullet-proofing...save it to opendrawer.bas
Sub OpenDrawer()
drawerComPort = "COM1"
Open drawerComPort For Output Access Write As #1
REM ADDED ERROR HANDLING
ON ERROR GOTO ErrHandler
Print #1, Chr$(65); "A";
Close #1
print "Drawer Ok"
OpenDrawer_Exit:
On Error Goto 0
Exit Sub
ErrHandler:
print "Oops, Write Failed"
Goto OpenDrawer_Exit
End Sub
REM The Main....
OpenDrawer
Download the old QB4.5 MS-Quick Basic compiler, and compile that to an executable, into opendrawer.exe, the QB4.5 can be found here. Now, the onus is on you to make this bulletproof, i.e. what happens if writing to COM1 fails, issue a message like in the example BASIC code I modified
Then you can use the System.Diagnostics.Process to shell out using a hidden window
public class TestDrawer
{
private StringBuilder sbRedirectedOutput = new StringBuilder();
public string OutputData
{
get { return this.sbRedirectedOutput.ToString(); }
}
public void Run()
{
System.Diagnostics.ProcessStartInfo ps = new System.Diagnostics.ProcessStartInfo();
ps.FileName = "opendrawer";
ps.ErrorDialog = false;
ps.CreateNoWindow = true;
ps.UseShellExecute = false;
ps.RedirectStandardOutput = true;
ps.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
using (System.Diagnostics.Process proc = new System.Diagnostics.Process())
{
proc.StartInfo = ps;
proc.Exited += new EventHandler(proc_Exited);
proc.OutputDataReceived += new System.Diagnostics.DataReceivedEventHandler(proc_OutputDataReceived);
proc.Start();
proc.WaitForExit();
proc.BeginOutputReadLine();
while (!proc.HasExited) ;
}
}
void proc_Exited(object sender, EventArgs e)
{
System.Diagnostics.Debug.WriteLine("proc_Exited: Process Ended");
if (this.sbRedirectedOutput.ToString().IndexOf("Oops, write failed") > -1){
MessageBox.Show(this, "Error in opening Cash Drawer");
}
if (this.sbRedirectedOutput.ToString().IndexOf("Drawer Ok") > -1){
MessageBox.Show(this, "Drawer Ok");
}
}
void proc_OutputDataReceived(object sender, System.Diagnostics.DataReceivedEventArgs e)
{
if (e.Data != null) this.sbRedirectedOutput.Append(e.Data + Environment.NewLine);
//System.Diagnostics.Debug.WriteLine("proc_OutputDataReceived: Data: " + e.Data);
}
The process shells out to a hidden window and all output is redirected and handled in the event handler...that should do the trick. Notice, how the redirected output goes into the sbRedirectedOutput (a StringBuilder instance). In the proc_ProcExited event handler, it checks the sbRedirectedOutput for the message 'Oops Write failed' which would be issued from the QB4.5 program.
Be aware, that you may need to include the QB4.5's run-time library in the same directory...not 100% sure...it's being years...
What do you think?
Hope this helps,
Best regards,
Tom.