Jump to content

Making a script that presses Enter a set number of times


Recommended Posts

Okay, im not really sure how to get all of this into the title, so ill just explain it here.

At my company we have a database for all our prducts.

After testing a batch of say 1000, we have to register that they are good in the database.

This is done manually ( they claim that letting us ad say 100 cards at a time is to much of a strain on the database) so you have to press enter 3 times in a row to get throught the menus and register 1 card.

I have used other macro programs to time how long there has to be between each enter for the page to load before the nex enter.

I do not have much experience with making scripts, and have only made this of whatever information i have found on different forums and changes to them that i made myself.

HotKeySet("a", "Regi")
HotKeySet("{Esc}", "On_Exit")
While 1
   Sleep(100)
   WEnd
 
Global $Counter = 0
 
Func Regi()
   While 1
   Send("{Enter}")
   Sleep(848)
   Send("{Enter}")
   Sleep(400)
   Send("{Enter}")
   Sleep(736)
   Send("{Enter}")
   Sleep(672)
   Send("{Enter}")
   Sleep(448)
   Send("{Enter}")
   Sleep(500)
   $Counter = $Counter + 1
   If Counter = 480 Then Exit ;Edit this for how many cards should be registered before closing the script
   WEnd
 
EndFunc
 
 
 
Func On_Exit()
   Exit
EndFunc
 
As you can see it registers 2 cards each time it runs the function.
I use 2 cards because i know the timing works.
I wanted to make it so that i enter the page for the database, edit the script for how many cards i want to register/2.
Then start the script, higlight the button for "next card", and just press a.
The script should then run for as many cards that i set beforehand, then close itself.
 
If you could point out the mistakes i have made and show me/teach me how to fix them that would be great! 
 
Many thanks in advance
Link to comment
Share on other sites

  • Moderators

Your delay seems to be based on the speed of your processor and the ability for the app to process the data you just put in.

Would it not be more efficient to:

1.  If this app has an API or direct access to the sql/mysql/sqlite database, to access from that... or

2.  Access the "buttons" as they are visible or enabled with Control functions?

What application are you interacting with anyway?  Maybe someone has or could come up with a much more proficient and safe way about it.

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

I assume the in your video you're using google web browser.

If you use IE, there are many functions you can employ to do your task without sending keystrokes, and setting properties and pushing buttons directly, even if the window is in the background.

Take a look in the help file at _IE in the index tab, there are many examples.

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Link to comment
Share on other sites

  • Moderators

I think what JohnOne's assumptive post was, he assumes you know how to use the IE.au3 functions for internet explorer, or that was his attempt to have you search on how to use them.

I don't believe you know they exist.

First, you should know that AutoIt is so much more than just a "macro" service language (click this coord, wait, do it again).  You can delve pretty deep into pretty much any application and its workings.

Here's an example of using IE.au3 (_IE* functions in the help file) for just logging into your website (be sure to add your username/password):

#include <IE.au3>

Global $gsLoginUsername = "<username here>"
Global $gsLoginPassword = "<password here>"

; create a browser instance
Global $goIE = _IECreate()
If Not IsObj($goIE) Then
    MsgBox(16 + 262144, "Error", "Failed to create browser object.")
    Exit 101
EndIf

_westcontrolLogin($goIE, $gsLoginUsername, $gsLoginPassword)
Switch @error ; track error for your testing purposes only
    Case 1
        MsgBox(16 + 262144, "Error", "$goIE was not a browser object!")
        Exit 102
    Case 2
        MsgBox(16 + 262144, "Error", "$oForm was not a form object!")
        Exit 103
    Case 3
        MsgBox(16 + 262144, "Error", "$oUser was not an input object!")
        Exit 104
    Case 4
        MsgBox(16 + 262144, "Error", "$oPass was not an input object!")
        Exit 105
    Case Else
        MsgBox(262144, "Success", "You should be logged in!")
        Exit 0
EndSwitch

Func _westcontrolLogin(ByRef $oBrowser, $sUsername, $sPassword)

    If Not IsObj($oBrowser) Then
        Return SetError(1, 0, 0)
    EndIf

    Local $sLoginURL = "http://db.westcontrol.no/new/login.php"

    ; navigate to login page
    _IENavigate($oBrowser, $sLoginURL)
    _IELoadWait($oBrowser)

    ; get form object
    Local $oForm = _IEGetObjById($oBrowser, "login_form")
    If Not IsObj($oForm) Then
        Return SetError(2, 0, 0)
    EndIf

    ; get username input object
    Local $oUser = _IEGetObjById($oForm, "username")
    If Not IsObj($oUser) Then
        Return SetError(3, 0, 0)
    EndIf

    ; get password input object
    Local $oPass = _IEGetObjById($oForm, "password")
    If Not IsObj($oPass) Then
        Return SetError(4, 0, 0)
    EndIf

    ; now submit the login form
    _IEFormSubmit($oForm)
    _IELoadWait($oBrowser)

    Return 1
EndFunc

That should log you in, that's so you can see that you can manipulate IE objects fairly easily without the need for "timely" mouseclicks/send operations that often fail when a site is locked up or your system is operating slowly.

To learn more, study the _IE* functions in the help file.

To get the objects id's / name / etc values, download and use debugbar (drag the green circle icon over the object you want to interact with once it's installed).

Once you dive into a little bit, if you get stumped, post the code your stumped on, and many here can help.

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

On the login page it says that the page is specifically designed for chrome, firefox and opera, and that any use of IE is not allowed.

When login in and doing anything on the page i get these errors(translated them as they were partly in norwegian):

Line: 89
Character: 5
Code: 0
Errormessage: Expected identifier, string or number
 
Line: 60
Character: 6
Code: 0
Errormessage: Expected identifier, string or number
 
 
Link to comment
Share on other sites

  • Moderators

Excellent... lol.  I didn't understand, guess I should have ran that through a translator.

There are also ">Chrome and >Firefox" udf's available, there's also: >IUIAutomation if you just need to narrow down individual buttons maybe.

Just a way to make life much simpler on you.  If you program it right, you won't run into issues the way you would with a macro, and easier to fix when needed to.

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

When i said i dont have much experience, i should have said i dont have any experience...

This is litterally the first script i have made.

I could take the time to first learn all the basics of autoit and then add the chrome udf, learn that, and make a much better and faster script.

but.

they are allready updating the database so that we can add 100's of cards at a time, and this is just a temporary solution while they are updating it.

What i need is some help with is getting the script to count how many cards it has entered and auto-kill itself when done.

Sorry for wasting your time by not not clarifying this

Link to comment
Share on other sites

If that is the case I don't see what is wrong with your program other than editing the 480 loops to something else.

Maybe just change all spleep times to Sleep(1000).

Maybe see if the title of your browser window changes after each ENTER and wait for that change.

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Link to comment
Share on other sites

The address bar changes.

Fixed the two errors i had:

Moved The Global variable up to right under the Hotkeys as i was complaining that i hadent declared it.

Changed Counter in the If statement to $Counter.

Seems to be working now.

Will come back if i decide to write a more complicated version of it

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...