Jump to content

Button to run script


iMacg3
 Share

Recommended Posts

Hi,

 

New to the forum and AutoIt. Still learning the basics of how to code in AutoIt.

Just a question that should be easy for all of you experts to answer. After creating a button in a GUI, what would be the best way to make the action of the user clicking the button run another AutoIt script?

 

Thanks. 

Link to comment
Share on other sites

Hi @iMacg3, and welcome to the AutoIt Forum :)

Mainly, there are two ways to make an action when pressing a button.

The first, is by using an infinite While loop, in which there's a function called GUIGetMsg(), which continuously sees if there are new "messages", like a button press, the close event of the GUI, and so on.

The structure what I've described has this form:

Local $idMsg = 0

While 1
    $idMsg = GUIGetMsg()
    
    Switch $idMsg
        Case $idButton
            ; Make actions when $idButton has been pressed
        Case $GUI_EVENT_CLOSE
            ; Exit the script, since the "X" button of the GUI has been pressed
            Exit
    EndSwitch
WEnd

The second one, is by using the option "OnEventMode = 1", which means that, the script, will not continuously sees if there are new messages, but will process them and take actions only when the event(s) effectively occur(s).

The structure of what I've described has this form:

Global $GUI = GUICreate()
GUISetOnEvent($GUI_EVENT_CLOSE, "ExitApplication")
Global $idButton = GUICtrlCreateButton(...)
GUICtrlSetOnEvent(-1, "idButton_Click")
GUISetState($hGUI, @SW_SHOW)

While 1
    Sleep(100)
WEnd


Func ExitApplication()
    Exit
EndFunc

Func idButton_Click()
    ; The button has been pressed
EndFunc

You can find more about GUIGetMsg() and the Option OnEventMode in the Help file :)

 

P.S.: If you want to make a GUI without manually calculating the position of the controls, you can use Koda Form Designer ;)

Edited by FrancescoDiMuro

Click here to see my signature:

Spoiler

ALWAYS GOOD TO READ:

 

Link to comment
Share on other sites

You could #Include "YourOtherScript.au3" (this assumes that it resides in the main script directory. You can also add a path)

And just call specific functions from that script from your main script when button is pressed. 

Edited by markyrocks
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...