Why Function "if ProcessExist" Don't Work in Others Computers - autoit

I'm trying to execute an IF ProcessExist.
in my win 10 64bit computer it works, but when i execute in other PC with win 7 or even with win 10. It do not execute.
#AutoIt3Wrapper_UseX64=N
If ProcessExists ("program.exe") Then
MsgBox ("", "Hold", "Test", 10)
Exit
Else
#RequireAdmin
Run(#ComSpec & " /c " & "C:\folder\file.bat", "", #SW_HIDE)
EndIf
Keep getting as if program.exe exist, but it's not.

#RequireAdmin must be on the very top of your script.
I assume it's not working when you compile the executable. It's probably called program.exe and it's finding its own process.
As for the Singleton see this:
_Singleton ( $sOccurrenceName [, $iFlag = 0] )
Enforce a design paradigm where only one instance of the script may be running
Example
#include <Misc.au3>
#include <MsgBoxConstants.au3>
If _Singleton("test", 1) = 0 Then
MsgBox($MB_SYSTEMMODAL, "Warning", "An occurrence of test is already running")
Exit
EndIf
MsgBox($MB_SYSTEMMODAL, "OK", "the first occurrence of test is running")

Related

Is the following codes right? Can I make just one with this two codes?

I'm trying to make a script to start two applications 8:00pm and stop it 8:00am, removing a dir when it stops.
I can't test it right now and I'm new with AutoIt so if someone have any suggestions to my code or know another way to do it (I tried with Task Manager calling batch files but I had some issues). I'll be very happy with your help!
My ideia is to make this script an exe and schedule with Task Manager to start on logon and when 8:00pm it start's the two applications (verifying the OS Arch). I need to make this applications stop 8:00am and I'm thinking in make a second script to do it, but if can be maked with just one script will be better.
It's for Windows machines, x64 or x86.
FIRST SCRIP:
#include <Timers.au3>
#NoTrayIcon
#persistent
loop {
If (A_Hour = 20) and (A_Min = 00) { ; is time 8:00pm?
If #OSArch = "X64" Then
RunWait (C:\ProgramData\...\FirstApp.exe)
Run (C:\ProgramData\...\SecondApp.exe)
Elseif #OSArch = "X86" Then
RunWait (C:\ProgramData\...\FirstApp_x86.exe)
Run (C:\ProgramData\...\SecondApp_x86.exe)
EndIf
}
sleep, 1000 * 60 ; sleep for 60 seconds so only loop once per minute
}
return
SECOND SCRIPT:
#include <Timers.au3>
#NoTrayIcon
#persistent
loop {
if (A_Hour = 08) and (A_Min = 00) { ; is time 8:00am?
Run (#COMSPEC & "taskkill /F /IM FirstApp.exe", #SW_HIDE)
Run (#COMSPEC & "taskkill /F /IM SecondApp.exe", #SW_HIDE)
Run (#COMSPEC & "RMDIR C:\ProgramData\MyDir\ /S /Q", #SW_HIDE)
}
sleep, 1000 * 60 ; sleep for 60 seconds so only loop once per minute
}
return
Try putting your commands inside a function.
#include <Misc.au3> ; needed for _Singleton
_Singleton(#ScriptName, 0) ; allows one one instance of running script
HotKeySet("{ESC}", _close) ; [optional to exit script]
Do
Sleep(20)
;waits for 8 AM
If #HOUR = "08" And #MIN = "00" And #SEC = "00" Then
one()
EndIf
;waits for 8 PM
If #HOUR = "20" And #MIN = "00" And #SEC = "00" Then
two()
EndIf
Until GUIGetMsg() = -3 ; $GUI_EVENT_CLOSE
Func one()
;Add the commmands you want executed at 8AM here
EndFunc ;==>one
Func two()
;Add the commmands you want executed at 8PM here
EndFunc ;==>two
Func _close()
Exit
EndFunc

Running Autoit from command line and see errors/results

I am trying to run some autoit.au3 script from command line and see results there. I have put some ConsoleWrite inside script and also Exit(1) but after I run script nothing is shown in console. It just stop script on Exit and ConsoleWrite is not displayed.
I have use command:
"C:...(path to my AutoIt3.exe)" /ErrorStdOut "path_to_my_script.au3"'
Also I have tried to run script.exe with this same command but with similar (no) result. I would like to see output in console and/or custom error messages when script fail (I don't know if that is possible).
AutoIt3.exe is a GUI program. So the STD streams of a GUI program are not printed at a console by default.
The /ErrorStdOut argument redirects errors messages to Console instead of a Msgbox.
This argument does not enable print at the Console.
Command Prompt:
To print at a Command Prompt, you could pipe to more, i.e.
"C:...(path to my AutoIt3.exe)" /ErrorStdOut "path_to_my_script.au3" 2>&1|more
more reads from the Stdin stream and prints to Console.
I intentionly added 2>&1 so the Stderr stream is merged with
Stdout so you get the merged streams printed.
If you do not want the errors, then you can redirect the Stderr stream to nul i.e.
replace 2>&1 with 2>nul.
If you used a for loop at a Command Prompt, it would be i.e.
for /f "delims=" %A in ('"C:...(path to my AutoIt3.exe)" /ErrorStdOut test1.au3') do echo %A
If you use the for loop in batch-file, use %%A instead of %A. To also capture the Stderr, insert 2^>&1 into the for command or to ignore, insert 2^>nulinto the for command i.e.
for /f "delims=" %A in ('2^>nul "C:...(path to my AutoIt3.exe)" /ErrorStdOut test1.au3') do echo %A
The previous methods will not get the Exitcode.
AutoIt code:
An AutoIt script can get the Stdout and the Exitcode.
#pragma compile(Out, 'consoleau3.exe')
#pragma compile(Console, True)
$sAutoit = 'C:...(path to my AutoIt3.exe)'
$iPid = Run('"' & $sAutoit & '" /ErrorStdout ' & $CMDLINERAW, '', #SW_SHOW, 2) ; 2 = Get Stdout stream.
If #error Then Exit
; Open process handle.
$hPid = _ProcessOpenHandle($iPid)
; Get Stdout stream and then print to Console.
$sStdout = ''
Do
Sleep(10)
If $sStdout Then ConsoleWrite($sStdout & #CRLF)
$sStdout = StdoutRead($iPid)
Until #error
; Require process to be closed before calling _ProcessGetExitCode()
ProcessWaitClose($iPid)
; Get exitcode of process.
$iExitcode = _ProcessGetExitCode($hPid)
; Close process handle.
_ProcessCloseHandle($hPid)
Exit $iExitcode
Func _ProcessOpenHandle($iPID)
; Get the process handle of the process to query\n Return: Success Handle as array. Failure 0
Local Const $PROCESS_QUERY_INFORMATION = 0x400
Local $hPID = DllCall('kernel32.dll', 'ptr', 'OpenProcess', 'int', $PROCESS_QUERY_INFORMATION, 'int', 0, 'int', $iPID)
If #error Then Return SetError(#error, #extended, 0)
Return $hPID[0]
EndFunc
Func _ProcessGetExitcode($hPID)
; Get exitcode of the closed process\n Return: Success Exitcode as integer. Failure 0
Local $vPlaceholder
$hPID = DllCall('kernel32.dll', 'ptr', 'GetExitCodeProcess', 'ptr', $hPID, 'int*', $vPlaceholder)
If #error Then Return SetError(#error, #extended, 0)
Return $hPID[2]
EndFunc
Func _ProcessCloseHandle($hPID)
; Close the handle of a process\n Return: Success 1. Failure 0
DllCall('kernel32.dll', 'ptr', 'CloseHandle', 'ptr', $hPID)
If #error Then Return SetError(#error, #extended, 0)
Return 1
EndFunc
Correct the path to AutoIt.exe in the code.
Compile to AutoIt code to executable. It will be a Console program
and will be named consoleau3.exe as to the #pragma compile directives.
Usage:
consoleau3 "path_to_my_script.au3"
Script arguments can be added i.e.
consoleau3 "path_to_my_script.au3" arg1 arg2 arg3 ...

RF-AutoIT: Cant upload files in Chrome

I am trying to create a common exe for 3 browser based file upload using below code.
IE (Choose File to Upload),
Chrome (Open),
Firefox (File Upload)
This is working for IE but not for Chrome and FireFox. There is no error but its not showing given file along with path.
I will be using this .exe in my Robot Framework script.
#include <MsgBoxConstants.au3>
SelectWindowBasedOnTitle()
Func SelectWindowBasedOnTitle()
$winList = WinList()
$wTitle = CheckWindows($winList)
IF $wTitle == "Choose File to Upload" Then
IE()
ElseIf $wTitle == "Open" Then
Chrome()
Else
FireFox()
EndIf
EndFunc
Func CheckWindows($aArray)
For $i = 1 To Ubound($aArray) - 1
If WinActive($aArray[$i][1]) Then $wTitle= $aArray[$i][0] ;MsgBox(0, "Window Check", $aArray[$i][0] & " is active.")
Next
Return $wTitle
EndFunc
Func Example()
$wText = WinGetText("[ACTIVE]")
EndFunc
Func IE()
ControlFocus("Choose File to Upload","","Edit1")
ControlSetText("Choose File to Upload","","Edit1",$CmdLine[1])
ControlClick("Choose File to Upload","","Button1")
EndFunc
Func Chrome()
ControlFocus("Open","","Edit1")
ControlSetText("Open","","Edit1",$CmdLine[1])
ControlClick("Open","","Button1")
EndFunc
Func FireFox()
ControlFocus("File Upload","","Edit1")
ControlSetText("File Upload","","Edit1",$CmdLine[1])
ControlClick("File Upload","","Button1")
EndFunc
This way i am using in my Robot script. Could you please help me understand is there a way to automate
${FileInfo} ${CURDIR}\\Xpaths.txt
${AutoIT} ${CURDIR}\\BrowserBasedWindowSelection.exe
Run Process ${AutoIT} ${FileInfo}

Click on the outlook calendar by searching the calendar Image (_ImageSearch)

I am using Imagesearch.au3 to search the calendar image on outlook . Below is the code .
Created a new folder --> ImageSearch.au3 , ImageSearchDLL.dll , and the screenshot of the calendar is placed
#include <ImageSearch.au3>
$x=0
$y=0
start()
while 1
sleep(1000)
WEnd
Func Start()
while 1
$result = _ImageSearch("cal.png",1,$x,$y,0)
if $result=1 Then
ConsoleWrite("Sucesfull")
MouseMove($x,$y,10)
Else
ConsoleWrite("Img not found")
EndIf
WEnd
EndFunc
Problem : Throws an error message
"C:\Users...\Desktop\AutoIT\New folder\img\ImageSearch.au3" (44) : ==> Subscript used on non-accessible variable.:
if $result[0]="0" then return 0
if $result^ ERROR
The dll will not load while running as a script. I'm not sure why but it seems others have the same problem. I was able to compile the script as a 64 bit application and then the application loaded the DLL. After changing the tolerance, I was able to find the image.
Also, I would rewrite your code. You have two infinite while loops for no reason. I added a sleep to the remain while loop. I would increase the tolerance.
#include <ImageSearch.au3>
start()
Func Start()
while 1
$x=0
$y=0
$result = _ImageSearch("cal.png",1,$x,$y,20)
if $result=1 Then
MsgBox(0,"Found Image","moving mouse")
MouseMove($x,$y,10)
Else
MsgBox(0,"Img not found","not found")
EndIf
sleep(10000)
WEnd
EndFunc
I tested with the 64bit imagesearch located at http://www.autoitscript.com/forum/topic/148005-imagesearch-usage-explanation/page-2

Run many AutoIt script files using a main file (XML, Java, AutoIt or any language)

I'm using AutoIt for testing my application. There are different test cases for that. I will create these test cases as au3 files. Now I want to run all these scripts one after other. That is, a main script which call all sub script files one after other. How can I do that?
In a main AutoIt script (main.au3) you just add:
#include <UDF_function_1.au3>
#include <UDF_function_2.au3>
#include <UDF_function_3.au3>
Func _lanch_all()
; In order to run AutoIt function
_function_1() ; from UDF_function_1.au3
_function_2() ; from UDF_function_2.au3
_function_3() ; from UDF_function_3.au3
Run(PATH_to_script\"script_1.bat") ; In order to run a batch script windows
Run(PATH_to_executable\"script_2.exe") ; In order to run an executable
EndFunc
And for example UDF_function_1.au3 contains:
#include-once
Func _function_1()
ConsoleWrite("Call of _function_1"&#CRLF)
EndFunc
If you don't want to compile these test cases, you just compile the main exe:
Compiled.exe [/ErrorStdOut] [/AutoIt3ExecuteScript file] [params ...]
Execute another script file from a compiled AutoIt3 Script File. Then you don't need to fileinstall another copy of AutoIT3.exe in your compiled file.
#include <File.au3>
$Path = #ScriptDir & 'TestCases\'
$files = _FileListToArray($Path, "*.au3")
For $i = 1 To $files[0]
RunWait(#ScriptFullPath & '/AutoIt3ExecuteScript "' & $Path & $files[$i] & '.au3"')
Next
Just create a master AutoIt program and have this program run the other sub programs. If they are present in .au3 format, simply have the master program include the sub programs via the include keyword:
#include "[path\]filename"
This will allow the master program to call functions of the included sub-program.
#include <File.au3>
$Path = #ScriptDir & 'TestCases\'
$files = _FileListToArray($Path, "*.au3")
For $i = 1 To $files[0]
RunWait(#ScriptFullPath & '/AutoIt3ExecuteScript "' & $Path & $files[$i] & '.au3"')
Next
modified code as follows:
#include <File.au3>
$Path = #ScriptDir & '\TestCases\'
$files = _FileListToArray($Path, "*.au3")
MsgBox(0,"path", $files[0])
For $i = 1 To $files[0]
RunWait(#AutoItExe & " /AutoIt3ExecuteScript "& '"'& $Path & $files[$i] & '"')
Next

Resources