Jump to content

My first AutoIt Script


stroutd
 Share

Recommended Posts

I am trying to write a script that will allow me to run a file, which will then create a text file on disk. The script will then read the file, click a textbox in Firefox, and insert the stored file data into that box, then click a button. It does this every 45 seconds. Here is what I have so far:

while 1

$timer = timerinit()
do

Sleep(10)
until timerdiff($timer) >= 45000

Run("C:\automator\automate1.exe","C:\automator")
$dataFile=FileOpen("C:\automator\data.txt")
$data=FileRead($dataFile)
MouseClick("left",712,187,1,0)
Send($data)
MouseClick("left",782,189,1,0)

wend

The "wrapper" part that repeats the script every 45 seconds is copied from here. The script doesn't seem to be working right for some reason. I know for certain it is performing that final mouse click, as I have seen it move the mouse there and click. I cannot tell if the other actions are being performed though, or even if I am doing them right. I do not see them happening, but maybe they are happening so fast that I can't see them. I just want to know if I am using all these functions correctly.

Edited by stroutd
Link to comment
Share on other sites

Hello stroutd,

First, Welcome to the AutoIt Forums ;)

Second, if you just need to run that script every 45 seconds than this will do:

while 1
    Run("C:\automator\automate1.exe","C:\automator")
    $dataFile=FileOpen("C:\automator\data.txt")
    $data=FileRead($dataFile)
    MouseClick("left",712,187,1,0)
    Send($data)
    MouseClick("left",782,189,1,0)
    Sleep(45000)
wend

However, if you wish to run other code, while waiting 45 seconds, than try this:

$timer = timerinit()
while 1
    If timerdiff($timer) >= 45000 Then
        _RunAutomator()
        $timer = timerinit()
    EndIf
    ;Do Something else
    Sleep(10)
wend

Func _RunAutomator()
    Run("C:\automator\automate1.exe","C:\automator")
    $dataFile=FileOpen("C:\automator\data.txt")
    $data=FileRead($dataFile)
    MouseClick("left",712,187,1,0)
    Send($data)
    MouseClick("left",782,189,1,0)
EndFunc ;==>_RunAutomator()

Realm

My Contributions: Unix Timestamp: Calculate Unix time, or seconds since Epoch, accounting for your local timezone and daylight savings time. RegEdit Jumper: A Small & Simple interface based on Yashied's Reg Jumper Function, for searching Hives in your registry. 

Link to comment
Share on other sites

First, yes, this is the same person. I registered two accounts because my e-mail was down when the first activation e-mail was sent. Then, when I wanted to reply to this thread, I accidentally logged in to the first account, and noticed the "resend activation" link under my profile, so I was able to activate this account, with my preferred username.

Second, thanks for the simplified repeating code, I thought what I had might be slightly overcomplicated. However, I still need help to check the code I wrote. Any help would be appreciated. I know the executable is not being run, because the files it should be creating are not being created.

Link to comment
Share on other sites

Hello dstrout,

The Run command for this only needs the first parameter:

Run("C:\automator\automate1.exe")

Change that line, and see if it works for you.

Realm

Edited by Realm

My Contributions: Unix Timestamp: Calculate Unix time, or seconds since Epoch, accounting for your local timezone and daylight savings time. RegEdit Jumper: A Small & Simple interface based on Yashied's Reg Jumper Function, for searching Hives in your registry. 

Link to comment
Share on other sites

I know that, but I need the files the executable produces to be placed in that directory. Since automate1.exe is just a batch file compiled into a .exe, the working directory has to be set so it will place the files there, so the script can find the files later.

Oh, and BTW, I am away from the computer with AutoIt/my script on it, so I can't test anything just now.

Edited by dstrout
Link to comment
Share on other sites

Hi, welcome to the forums!

I've made some modifications to your code that might help:

- Cleaned up the repeating method. (As the first guy stated).

- Replaced Run () with ShellExecute (). (ShellExecute () is basically the same thing, but might fix your problem.)

- Added a Do...Until loop to wait until 'data.txt' exists.

- Added a small delay between each MouseClick (), Send (). (This helps ensures everything runs smooth. If some of the actions happen to fast or too slow adjust the times.)

- Also, I cleaned up the code and added comments to possibly help you better understand.

Here's the code, hope it helps.

While 1; Start a infinite while loop.
    ShellExecute ('C:\automator\automate1.exe','','C:\automator'); Execute your EXE, ShellExecute () is slightly different from Run () and might fix your problem.
    Do; A little peace of code that will wait until 'data.txt' is created, assuming 'automate1.exe' creates it.
        Sleep (10)
    Until FileExists ('C:\automator\data.txt')
    $Data = FileRead ('C:\automator\data.txt'); Read your file, FileOpen () isn't needed in this case.
    Sleep (1000); Wait one second. (This might need to be adjusted.)
    MouseClick ('Left', 712, 187, 1, 0); Make a left click at 712, 187.
    Sleep (500); Wait half a second. (This might need to be adjusted.)
    Send ($Data); Send the $Data.
    Sleep (500); Wait half a second. (This might need to be adjusted.)
    MouseClick ('Left', 782, 189, 1, 0); Make a left click at 782, 189.
    Sleep (45000); Wait 45 seconds until repeating process again.
WEnd

- John

Edited by z0mgItsJohn

Latest Projects :- New & Improved TCP Chat

Link to comment
Share on other sites

Thanks a lot there John. You'll make me lazy, doing all the work for me ;) Anyway, I'll try this ASAP, once I get back to my PC. Hopefully your script will do the trick.

Edit: Looking at the documentation, I think ShellExecute should be replaced with ShellExecuteWait, to make sure the executable has finished before proceeding. Thanks for pointing out ShellExecute though, I can try running that on every command in the batch file if running ShellExecute on the batch file itself doesn't work for some reason.

Edited by dstrout
Link to comment
Share on other sites

OK, version 1 is up and working, with a few modifications to the batch file and autoit script. Here is my final script:

While 1; Start a infinite while loop.
    ShellExecuteWait ('C:\automator\automator.exe','','C:\automator'); Execute your EXE, ShellExecute () is slightly different from Run () and might fix your problem.
    Do; A little piece of code that will wait until 'data.txt' is created, assuming 'automate1.exe' creates it.
        Sleep (10)
    Until FileExists ('C:\automator\data.txt')
    $Data = FileRead ('C:\automator\data.txt'); Read your file, FileOpen () isn't needed in this case.
    $Data = StringLower ($Data); Make sure the string is lowercase.
    MsgBox (0, "Data", $Data, 1)
    Sleep (1000); Wait one second. (This might need to be adjusted.)
    MouseClick ('Left', 712, 187, 1, 0); Make a left click at 712, 187.
    Sleep (500); Wait half a second. (This might need to be adjusted.)
    Send ($Data); Send the $Data.
    Sleep (500); Wait half a second. (This might need to be adjusted.)
    MouseClick ('Left', 782, 189, 1, 0); Make a left click at 782, 189.
    Sleep (40000); Wait 45 seconds until repeating process again.
WEnd

I put in the MsgBox on line 8 to make sure the file was being read correctly, as it did not seem to be. Once I put that in, though, everything seemed to work. Strange...

Anyway, there is a possiblity that an error will occur on the webpage, which the script will need to deal with. Right now I am setting up a system that checks the MD5 sum of a small portion of a screenshot of the page. This thread can now be considered tentatively solved, unless I run into further problems. Thanks for the help guys!

Edited by dstrout
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...