Jump to content

How would i write this script?


ins0m
 Share

Recommended Posts

Hello, i'm fairly new (several hours new) to this whole scripting language. I'm trying to write a fairly simple script, just to test some of the stuff this script tool can do.

The first one i wanted help with was something that did the following:

<run script>

when you press a key, let's say "8", it starts the script running. (so i suppose you'd start the script as paused, and 8 would trigger an unpause?)

then i want the script to write a sentence (let's say i already have notepad running, and it has focus)

so,

Send("Hello this is a sentence")

then i want it to hit enter, so

Send("{ENTER}")

then i want it to wait a while.. say.. i dunno, 7 seconds

then i want it to repeat, so it starts a new line (using the enter key) and every 7 seconds write the same phrase, until i hit 8 again, which will pause it. so in essence i could end up with 1000 lines of "hello this is a sentence".

I suppose i just want to learn how to pause/unpause, and use trigger buttons to get a script to run. And also how to use time.. is it a wait() function, or.. ? anyway..

Can anybody help me with writing this? :P It would be muchly appreciated.

Link to comment
Share on other sites

  • Moderators

Look in the help file for HotKeySet(), it has an example of a pause/unpause otpion in it. Everything else you have other than a loop:

Opt('SendKeyDelay', 1)
HotKeySet("{Pause}", 'TogglePause'); Find hotkeyset in help for rest of code
While 1
    Send('Hello This Is A Sentence')
    Send('{ENTER}')
    Sleep(7000); 1 second is 1000 milleseconds / Sleep works in Milleseconds
    Send('{ENTER}')
Wend

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

Ok, thanks, i'm not entirely certain though, is it something like this?

Opt('SendKeyDelay', 1)
Global $Paused
HotKeySet("{PAUSE}", "TogglePause")
HotKeySet("{ESC}", "Terminate")


While 1
    Send('Hello This Is A Sentence')
    Send('{ENTER}')
    Sleep(7000); 1 second is 1000 milleseconds / Sleep works in Milleseconds
    Send('{ENTER}')
Wend

Func TogglePause()
    $Paused = NOT $Paused
    While $Paused
        sleep(100)
        ToolTip('Script is "Paused"',0,0)
    WEnd
    ToolTip("")
EndFunc

Func Terminate()
    Exit 0
EndFunc
Link to comment
Share on other sites

  • Moderators

That looks right...

Now to take it a step further, if it is a specific application you are going to send to:

Opt('SendKeyDelay', 1)
Opt('WinTitleMatchMode', 4)
Global $Paused
Global $Window = 'WindowTitle Between the quotes'
HotKeySet("{PAUSE}", "TogglePause")
HotKeySet("{ESC}", "Terminate")


While 1
    If Not WinActive($Window) Then WinActivate($Window)
    Send('Hello This Is A Sentence' & '{ENTER}')
    Sleep(7000); 1 second is 1000 milleseconds / Sleep works in Milleseconds
    If Not WinActive($Window) Then WinActivate($Window)
    Send('{ENTER}')
Wend

Func TogglePause()
    $Paused = NOT $Paused
    While $Paused
        sleep(100)
        ToolTip('Script is "Paused"',0,0)
    WEnd
    ToolTip("")
EndFunc

Func Terminate()
    Exit 0
EndFunc

Now mind you, you won't have much control over your desktop because it will make sure the window is active that you want to send too.

If it's a window that you can send Control Commands too, and the 'area' that you want to send the text too has a ControlID (Use AutoInfo.exe included with autoit over the area you want to send the text too to see if it has a control id) then you may choose to use ControlSend() rather than Send:

Opt('SendKeyDelay', 1)
Opt('WinTitleMatchMode', 4)
Global $Paused
Global $Window = 'WindowTitle Between the quotes'
HotKeySet("{PAUSE}", "TogglePause")
HotKeySet("{ESC}", "Terminate")


While 1
    ControlSend($Window, '', 'ClassNameNN', 'Hello This Is A Sentence' & '{ENTER}')
    Sleep(7000); 1 second is 1000 milleseconds / Sleep works in Milleseconds
    ControlSend($Window, '', 'ClassNameNN', '{ENTER}')
Wend

Func TogglePause()
    $Paused = NOT $Paused
    While $Paused
        sleep(100)
        ToolTip('Script is "Paused"',0,0)
    WEnd
    ToolTip("")
EndFunc

Func Terminate()
    Exit 0
EndFunc

You might want to try both options to see what works best for you.

Edit:

Combined the send text / send enter and same with controlsend text/enter

Edited by SmOke_N

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

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