Sign in to follow this  
Followers 0
gordo

Any way to auto kickstart AutoIT scripts?

11 posts in this topic

I'm wondering if there's a way to have AutoIT constantly listen in the background for certain triggers like key combinations or window pop-ups, and automatically perform certain scripted actions and then go back to a listening state for the next trigger... until the process is killed.

Is there a way to make such a listener as some sort of uber-script with all actions included as subroutines or is there another external facility that provides for this capability?

Share this post


Link to post
Share on other sites



I'm wondering if there's a way to have AutoIT constantly listen in the background for certain triggers like key combinations or window pop-ups, and automatically perform  certain scripted actions and then go back to a listening state for the next trigger... until the process is killed.

Is there a way to make such a listener as some sort of uber-script with all actions included as subroutines or is there another external facility that provides for this capability?

<{POST_SNAPBACK}>

You have to use WinWaitActive in a loop and HotKeySet. Even you can use AdLibEnable to poll :)

Share this post


Link to post
Share on other sites

Thanks, I'll give it a shot.

Share this post


Link to post
Share on other sites

Ok, so here's a test script I created that seems to do the trick, although I'm not exactly sure whether this is an efficient way to accomplish my task. I assume that with this structure, I can just keep adding functions and the main program will loop through each one. Is this the right way to go about this? Will this scale to let's say 100 functions without killing the CPU even if I speed up the sleep periods?

While 1

AdlibEnable ("notepad_hello", 1000)

Sleep (1000)

AdlibEnable ("wordpad_goodbye", 1000)

Sleep (1000)

WEnd

Exit

Func notepad_hello()

If WinActive("Untitled - Notepad") Then

ControlClick("Untitled - Notepad", "", 15)

Send("hello")

Sleep(2000)

WinKill("Untitled - Notepad")

EndIf

EndFunc

Func wordpad_goodbye()

If WinActive("Document - WordPad") Then

ControlClick("Document - WordPad", "", 59648)

Send("goodbye")

Sleep(2000)

WinKill("Document - WordPad")

EndIf

EndFunc

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

I would approach this way (not saying its correct, but it works for me)

;set a hot key to exit

While 1

If WinActive("Untitled - Notepad") Then

ControlClick("Untitled - Notepad", "", 15)

Send("hello")

Sleep(2000)

WinKill("Untitled - Notepad")

EndIf

If WinActive("Document - WordPad") Then

ControlClick("Document - WordPad", "", 59648)

Send("goodbye")

Sleep(2000)

WinKill("Document - WordPad")

EndIf

Sleep(100)

WEnd

just a thought

8)

Edited by Valuater

NEWHeader1.png

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

Read the help on AdlibEnable, you only need one function, have that function

contain all your if statements

#NoTrayIcon

Opt("TrayOnEventMode",1)
Opt("TrayMenuMode",1)  ; Default tray menu items (Script Paused/Exit) will not be shown.

$exit = TrayCreateItem("Exit")
TraySetItemOnEvent(-1,"ExitEvent")
TraySetState()

AdlibEnable("_MyAdlib")
While 1
    Sleep(10)
WEnd 

Func _MyAdlib()
    If WinActive("Untitled - Notepad") Then
        ControlClick("Untitled - Notepad", "", 15)
        Send("hello")
        Sleep(2000)
        WinKill("Untitled - Notepad")
    EndIf
    If WinActive("Document - WordPad") Then
        ControlClick("Document - WordPad", "", 59648)
        Send("goodbye")
        Sleep(2000)
        WinKill("Document - WordPad")
    EndIf
EndFunc

Func ExitEvent()
    Exit
EndFunc

Edit: added TrayMenu

Edit: this is for the Release version of AutoIt, for the Beta change the

TraySetItemOnEvent(-1,"ExitEvent")

to

TrayItemSetOnEvent(-1,"ExitEvent")

Edited by gafrost

SciTE for AutoItDirections for Submitting Standard UDFs

 

Don't argue with an idiot; people watching may not be able to tell the difference.

 

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

Read the help on AdlibEnable, you only need one function, have that function

contain all your if statements

;set a hot key to exit
HotKeySet("{ESC}", "Terminate")

AdlibEnable("_MyAdlib")
While 1
    Sleep(100)
WEnd 

Func _MyAdlib()
    If WinActive("Untitled - Notepad") Then
        ControlClick("Untitled - Notepad", "", 15)
        Send("hello")
        Sleep(2000)
        WinKill("Untitled - Notepad")
    EndIf
    If WinActive("Document - WordPad") Then
        ControlClick("Document - WordPad", "", 59648)
        Send("goodbye")
        Sleep(2000)
        WinKill("Document - WordPad")
    EndIf
EndFunc

Func Terminate()
    Exit 0
EndFunc

Edit: added hot key exit

<{POST_SNAPBACK}>

doesn't the adlib need to be inside the loop?

8)

Edited by Valuater

NEWHeader1.png

Share this post


Link to post
Share on other sites

From the help file, I've used AdlibEnable many times, it runs on it's own loop

Enables Adlib functionality.

AdlibEnable ( "function" [, time])

Parameters

function The name of the adlib function to call.

time [optional] how often in milliseconds to call the function. Default is 250 ms.

Return Value

None.

Remarks

Every 250 ms (or time ms) the specified "function" is called--typically to check for unforeseen errors. For example, you could use adlib in a script which causes an error window to pop up unpredictably.

The adlib function should be kept simple as it is executed often and during this time the main script is paused. Also, the time parameter should be used carefully to avoid CPU load.

AdlibEnable("myadlib")

;...

Exit

Func myadlib()

    If WinActive("Error") Then

        ;...

    EndIf

EndFunc


SciTE for AutoItDirections for Submitting Standard UDFs

 

Don't argue with an idiot; people watching may not be able to tell the difference.

 

Share this post


Link to post
Share on other sites

#10 ·  Posted (edited)

AdLibEnable is such a strange function. It does run in its own internal loop and will max out CPU unless the next command is a sleep(x). But when exactly is the sleep function evaluated? I just don't get it. It doesn't seem to matter what length of time you sleep for either, as the sleep function is not fully evaluated... Can anyone explain what exactly is going on here?

Oh, also, each of these lines error out on my computer. How come and what do they do?

Opt("TrayOnEventMode",1)

Opt("TrayMenuMode",1) ; Default tray menu items (Script Paused/Exit) will not be shown.

$exit = TrayCreateItem("Exit")

TrayItemSetOnEvent(-1,"ExitEvent")

TraySetState()

Edited by gordo

Share this post


Link to post
Share on other sites

Oh, also, each of these lines error out on my computer. How come and what do they do?

Opt("TrayOnEventMode",1)

Opt("TrayMenuMode",1)  ; Default tray menu items (Script Paused/Exit) will not be shown.

$exit = TrayCreateItem("Exit")

TrayItemSetOnEvent(-1,"ExitEvent")

TraySetState()

<{POST_SNAPBACK}>

I dont care much for adlib, thus i showed you how I would approach the script

you are recieving these errors because these function are designed for the BETA Autoit release

you can down load it AutoitForums>Autoit v3>Developers>Autoit 3.1.1+++

8)


NEWHeader1.png

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  
Followers 0