Jump to content
Sign in to follow this  
nadare

Need tips for a very simple script.

Recommended Posts

nadare

Hello everyone~

Im making a script to automate something extremely simple, a button press.

I have a home sever running 2003 server with automatic log in and then it locks the keyboard and such using screensaver, recently I got a D-Link

camera so I can have surveilance. The bad thing is that the program starts up as if "defaulted" every time in terms of choices. There is

3 buttons you can press to change between motion-recording, manual recording and time recording and its default on as manual.

What I want the script to do is check for the window, create and maintain a logfile before the script closes ( for debugging purposes only), move

the mouse to the grid point ( as said, program is defaulted back to the same posistion on the screen everytime) and click the button and finally send

the windows keys to lock it ( Windows button - L ).

It should be very simple but im slightly stuck at the creation of the logfile and how to write to it, and the gridsystem I have no idea how to make.

On the log file issue, would I have to tell it to open notepad and write in it and then save it ?

Grid system is.. beyond me and what I have found in the forum sofar.

Here is what I got sofar, sorry if its not tidy or following any standards as I have no education at all in coding so this is purely from what I could find:

#cs ----------------------------------------------------------------------------

 AutoIt Version: 3.2.10.0
 Author:         Nadare

 Script Function:
    Webcam monitoring button presser

#ce ----------------------------------------------------------------------------

; Options

; Script Start - Is program running? If it does, continue to log function. If not then run wait() function to wait for window.
start()

; Once start() is complete it will have made the grid of the screen and sendt the keypresses.
Exit
; ______________________ FUNCTIONS ______________________________________

; Function start() begins the entire script by checking for the IPView SE window and or waiting for it before moving on.

Func start()
if WinExists( "IPView SE" )
    Then logdircheck() 
    Else waitwindow()
EndFunc

; Function waitwindow() waits for window to become active

Func waitwindow()
WinWait ("IPView SE", "", 600)
    Then start() 
Else MsgBox (64, "Program not started", "The IPView SE program has not started within 10 minutes or has failed to be detected. Writing out log file.", 300)
    Then logdircheck()
EndFunc

; Function logdircheck() checks if a logfolder exists and then either creates it or continues to check if a logfile is created.

Func logdircheck()
if FileExists ( "C:\scriptlog\" ) 
    Then logfilecheck() 
Else MsgBox ( 64, "Folder missing!", "Folder C:\scriptlog\ is missing, creating folder now." ) 
    Then DirCreate ( "C:\scriptlog\" )
    Then logdircheck()
EndFunc

; Function logfilecheck() checks that the log file has been created and continues the script towards button press.

Func logfilecheck() 
    If FileExists ( "C:\scriptlog\log.txt" )
    ; Add log file line to confirm everything ok to this point.
        Then mkgrid()
    Else; Make a new logfile and do logfilecheck() over again to confirm the file to be made.
EndFunc

; Make grid of the screen

Func mkgrid()
; Check how to best make grid and add here.
; Add log file line to confirm everything ok to this point.
EndFunc

; Function press() moves mouse cursor to button to be pressed.

Func press()
; Move mouse to grid for button and send mouse click.
EndFunc

Any tips or hints would be most welcome :)

-Nadare

Share this post


Link to post
Share on other sites
herewasplato

Welcome to the forum.

My first "hint" would be to do one thing at a time. Get that working and then add features.

If the IPView software starts when the computer starts, you might want to turn that option off and let your AutoIt script start the application. Have your AutoIt script start when the OS starts.

If your computer is doing a lot of things at startup, the first line of your AutoIt script could be a Sleep(20000) to let things settle down for 20 seconds. Make that longer if need be.

Use the AutoIt Run() function to start the IPView app.

Then use a WinWaitActive line to wait for the app to be active.

Read the help file on the ControlClick() function to click on the button of interest.

If ControlClick will not work, then use MouseClick()

Stay away from Func/EndFunc for now.

So, your simple starting script will have 4 lines of code:

Sleep...

Run...

WinWaitActive ...

ControlClick...

Once you get that working, you can read up on

FileOpen

FileWrite

FileClose

You do not need to write into a Notepad window for your log file.

...hope this helps...


[size="1"][font="Arial"].[u].[/u][/font][/size]

Share this post


Link to post
Share on other sites
nadare

o.o'

But something so simple and effective.. is it redundant? I'll check over it and see if I can really make it that

simple, though it would mean I have wasted a few hours over-complticating things.

Thanks on the tips for mouseclick and FileOpen, I dont think functionclick will work as the button has no ID or anything in the program.

Oh well, I still get paid for those hours spendt :)

--Nadare

Share this post


Link to post
Share on other sites
nadare

Well, despite how easy it could be done I still decided to go for the more advanced and sure to be way past me.

Here is what I got when I consider it complete:

#cs ----------------------------------------------------------------------------

 AutoIt Version: 3.2.10.0
 Author:         Christer Helland
 Script Version: 0.9 Beta
 Script Function:
    Webcam monitoring button presser

#ce ----------------------------------------------------------------------------

;  The beginning of the script starts with a 20 second wait time to let things settle after a startup upon which it begins the start() function.

Sleep (200)
start()

; Once start() is complete it will perform the button click and send the keypresses to lock the server.

FileWriteLine ("scriptlog.txt", "Moving mouse to coordinates.")

MouseMove (334, 418)

; Writing it to the log for debugging purposes.

FileWriteLine ( "scriptlog.txt", "Mouse moved, clicking button next.")

MouseClick ( "left" )

; Also writing to log for debugging purposes

FileWriteLine ( "scriptlog.txt", "Button clicked, motion recording should now be set.")

; Now to send the lock keys to lock the server.
FileWriteLine ( "scriptlog.txt", "Sending lock keys")
Send ("#l")
FileWriteLine ( "scriptlog.txt", "All functions completed, lock keys sendt, closing logfile.")
FileClose ("scriptlog.txt")

Exit
; ______________________ FUNCTIONS ______________________________________

; This is just for renaming the start function to the first step in the entire startup check.

Func start()
logdircheck()
EndFunc

; Function logdircheck() checks that a log dir and a logfile has been created before beginning mouseclick action.

Func logdircheck()
If FileExists("C:\scriptlog\") 
    Then logfilecheck() 
Else DirCreate("C:\scriptlog\") 
    And Assign ("$foldercheck", "1", 0)
        Then logdircheck()
EndIf
EndFunc

; Function logfilecheck() checks that the logfile is created and is also told if the logdir was not created previously.

Func logfilecheck() 
    If Not FileExists ( "C:\scriptlog\log.txt" ) 
        And IsDeclared ( $foldercheck )
            Then FileOpen ( "C:\scriptlog.txt", 2) 
                And FileWriteLine ( "scriptlog.txt", "Directory created and logfile created, moving on with start()")
                    Then startcheck()
    ElseIf FileExists ( "C:\scriptlog\log.txt" ) 
        And Not IsDeclared ( $foldercheck) 
            Then FileOpen ( "C:\scriptlog.txt", 2) 
                And FileWriteLine ( "scriptlog.txt", "Log file and log directory already created, continue to start() ")
                    Then startcheck()
    ElseIf Not FileExists ( "C:\scriptlog\log.txt" ) 
        And Not IsDeclared ( $foldercheck)
            Then FileOpen ( "C:\scriptlog.txt", 2) 
                And FileWriteLine ( "scriptlog.txt", "Directory exists but logfile is not created, creating it now and re-checking")
                    Then FileClose ( "scriptlog.txt" )
                        And logfilecheck()
                            EndIf
    EndFunc

; Function startcheck() begins the start up check script by checking for the IPView SE window and/or waiting for it, before moving on to the mouse click.

Func startcheck()
if WinExists( "IPView SE" )
    Then FileWriteLine ( "scriptlog.txt", "IPView SE started, proceeding to click button.") 
    Else FileWriteLine ( "scriptlog.txt", "IPView not SE started, proceeding to wait for it to start. ") And waitwindow() 
EndFunc

; Function waitwindow() waits for window to become active

Func waitwindow()
    If WinWait ("IPView SE", "", 600) = 1
    Then startcheck() 
Else FileWriteLine ( "scriptlog.txt", "The IPView SE program has not started within 10 minutes or has failed to be detected. Writing out log file.")
    Then FileClose ( "scriptlog.txt" )
        And Exit
EndFunc

However, already at the definition of function logdircheck() I get a error in my syntax, and no matter how I move the then and such around I can at best

make it get to the Else, which I do not get past nomatter what =/

What IS the correct syntax use of the "if then else" ? Is it just the spacing I have done wrong or?

Share this post


Link to post
Share on other sites
nadare

HAH!

Ok you can remove or close this topic now if its become too spammed, I got everything working now and just adding some pauses to make it slow down a bit.

Thanks for the help still. I found that eating something was of tremendous aid to my thinking capabilities since I have only had coffee in my tummy for the last

7 hours. Lesson learned!

Code will be posted if anyone want me to.

-Nadare

Share this post


Link to post
Share on other sites
nadare

Well, here it is. I wont be putting it in code tags, will instead upload it as script.

Thanks for the help herewasplato even if I did not follow your tips :)

--Nadare

Webcam_button_click.au3

Share this post


Link to post
Share on other sites
herewasplato

...Thanks for the help herewasplato even if I did not follow your tips :)

--Nadare

No problem... Enjoy AutoIt.

BTW,

; The beginning of the script starts with a 20 second wait time to let things settle after a startup upon which it begins the start() function.

Sleep (200)

Sleep(20000) is 20 seconds.

Sleep(200) is 20 milliseconds.


[size="1"][font="Arial"].[u].[/u][/font][/size]

Share this post


Link to post
Share on other sites
nadare

No problem... Enjoy AutoIt.

BTW,

; The beginning of the script starts with a 20 second wait time to let things settle after a startup upon which it begins the start() function.

Sleep (200)

Sleep(20000) is 20 seconds.

Sleep(200) is 20 milliseconds.

Heh yea, I fixed that after I moved the script to the target pc, thanks for noticing it even so :)

Im sure I will be more creative with AutoIt after this, I can see many uses for it.

-Nadare

***** EDIT *****

Since there are actually people who like this script and have a use for it already I will briefly explain what it does.

It starts by creating and or checking for the logdirectory and logfile. If neither of these exist it creates it and makes a note about it in the log.

It the moves on to check if the IPView program has started from autostart and gives it a small grace period before moving to the pixel I have specified.

This will change slightly depending on your resolution but the IPView program always defaults back to the same place ( in my experience atleast) everytime

its closed. Thus the pixel is easy to change if needed. After that it clicks the button, waits a few seconds while writing things to the log and then send the lock

keys to lock the computer. As simple as that.

Edited by nadare

Share this post


Link to post
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
Sign in to follow this  

×