Sign in to follow this  
Followers 0
Diana (Cda)

Putting GUI as UDF and putting info in script, how to?

8 posts in this topic

I can't figure out how to get what I'm trying to achieve work. I have a bunch of GUIs that are all of the same configuration. I'd like to change them all so that I have simple scripts that call the _BOXReminder.au3 UDF instead of building GUIs for each script. Besides simplifying everything, if I want to change the configuration, I'd only have to do it to the UDF GUI!

The _BOXReminder.au3 UDF would be something like this:

;
; AutoIt 3x
;
#include <GUIConstants.au3>
#NoTrayIcon     ; AutoIt's icon doesn't show in systray
TraySetIcon("Shell32.dll", 1004)     ; changes the icon displayed in the systray
AutoItSetOption("WinTitleMatchMode", 2)     ; this allows partial window titles to be valid!
;-------------------------------------------------------------------------------------------------------------------------


;=========================================
$ThisPartition = StringLeft(@ScriptDir, 2)
;=========================================


#cs
; ====== Edit this part here below, only:  ============================================================================
;     VARIABLES THAT WERE IN PLACE BEFORE:
$BoxTitle     = "Reminder ..."
$BoxBlurb     = "Do you need to do this task today ...?"
;----------------------------------------------------------------------------------------------------------------------
$Task2Perform = $ThisPartition & "\WORKINGdirs\WorkDir- EXCEL\FIN\zz- OpenFldr- BKP and open file.au3"
;======================================================================================================================
#ce


Sleep(1000)

Opt("GUIOnEventMode", 1)
#Region------------------------------------------------------------------------------------------------------
$Form1_1 = GUICreate($BoxTitle, 420, 155, 195, 125)     ; Width, Height, Left, Top (these measurements are for the entire box itself).
$Label2 = GUICtrlCreateLabel($BoxBlurb, 16, 15, 375, 60)   ; Left, top, width, height (these measurements are for the box of text only.)
GUICtrlSetFont(-1, 12, 400, 0, "MS Sans Serif")     ; this sets the font for the text above

$YES = GUICtrlCreateButton("Yes, Continue ...", 40, 110, 110, 25, 0)     ;  Button co-ordinates:  Left, Top, Width, Height, "0".
GUICtrlSetFont(-1, 11, 400, 0, "MS Sans Serif")    ; this font setting was missing in original script (for the "OK" or "Continue" button).
GUICtrlSetOnEvent(-1, "Click_YES")

$SNOOZE = GUICtrlCreateButton("NO, Snooze", 155, 110, 110, 25, 0)     ;  Button co-ordinates:  Left, Top, Width, Height, "0".
GUICtrlSetFont(-1, 10, 400, 0, "MS Sans Serif")     ; font settings for the button text.
GUICtrlSetOnEvent(-1, "Click_SNOOZE")

$Cancel = GUICtrlCreateButton("NO, Cancel", 270, 110, 110, 25, 0)     ;  Button co-ordinates:  Left, Top, Width, Height, "0".
GUICtrlSetFont(-1, 10, 400, 0, "MS Sans Serif")     ; font settings for the button text.
GUICtrlSetOnEvent(-1, "Click_CANCEL")
GUISetState(@SW_SHOW)
#EndRegion---------------------------------------------------------------------------------------------------

While 1
Sleep(100)
WEnd

;EndIf     ; goes with If statement above

Func Click_CANCEL()
    Exit     ; finished
EndFunc     ;  End "Click_CANCEL".

Func Click_SNOOZE()
    GUISetState(@SW_HIDE)
    $input = InputBox(_BoxTitle(), "Snooze for how many minutes?", 15)
    $time = $input*1000*60
    Sleep($time)
        #NoTrayIcon     ; AutoIt's icon doesn't show in systray
        GUISetState(@SW_SHOW)
        Beep(3500, 25)     ;  Beep(frequency; duration)
        Sleep(5)
        Beep(3500, 25)     ;  Beep(frequency; duration)
        Sleep(5)
        Beep(3500, 25)     ;  Beep(frequency; duration)
EndFunc     ;  End "Click_SNOOZE".

Func Click_YES()
    GUISetState(@SW_HIDE)
        ShellExecute($Task2Perform)
    Exit    ; finished
EndFunc     ;  End "Click_YES".

One of the scripts that can call the UDF (though this currently doesn't work), looks like this:

;
; AutoIt 3x
;
#include <_BOXReminder.au3>
#NoTrayIcon     ; AutoIt's icon doesn't show in systray
TraySetIcon("Shell32.dll", 1004)     ; changes the icon displayed in the systray
AutoItSetOption("WinTitleMatchMode", 2)     ; this allows partial window titles to be valid!
;-------------------------------------------------------------------------------------------------------------------------


;=========================================
$ThisPartition = StringLeft(@ScriptDir, 2)
;=========================================


; ====== Edit this part here below, only:  ============================================================================
;     VARIABLES THE BE USED IN THE SCRIPTS THAT CALL THIS UFD:
$BoxTitle     = "Reminder ..."
$BoxBlurb     = "Do you need to do this task today ...?"
;----------------------------------------------------------------------------------------------------------------------
$Task2Perform = $ThisPartition & "\WORKINGdirs\WorkDir- EXCEL\FIN\zz- OpenFldr- BKP and open file.au3"
;======================================================================================================================

However, I get the "variable used without being declared" error.

Is there a way to get this type fo functionality to work somehow?

Thank you! :)

Share this post


Link to post
Share on other sites



You just need to make a function in the UDF and call it in the main script...

(_BoxReminder.au3)

;
; AutoIt 3x
;
#include-once
#include <GUIConstants.au3>
;-------------------------------------------------------------------------------------------------------------------------

Global $Action
Global Enum $_NONE, $_YES, $_SNOOZE, $_CANCEL

Func _BoxReminder($BoxTitle, $BoxBlurb, $Task2Perform)

    #cs
        ; =====================================================================================================================
        ;     VARIABLES THAT WERE IN PLACE BEFORE:
        $BoxTitle     = "Reminder ..."
        $BoxBlurb     = "Do you need to do this task today ...?"
        ;----------------------------------------------------------------------------------------------------------------------
        $Task2Perform = $ThisPartition & "\WORKINGdirs\WorkDir- EXCEL\FIN\zz- OpenFldr- BKP and open file.au3"
        ;======================================================================================================================
    #ce

    Local $Form1_1, $Label2, $YES, $SNOOZE, $Cancel
    
    Sleep(1000)

    Opt("GUIOnEventMode", 1)
    #Region------------------------------------------------------------------------------------------------------
    $Form1_1 = GUICreate($BoxTitle, 420, 155, 195, 125) ; Width, Height, Left, Top (these measurements are for the entire box itself).
    $Label2 = GUICtrlCreateLabel($BoxBlurb, 16, 15, 375, 60) ; Left, top, width, height (these measurements are for the box of text only.)
    GUICtrlSetFont(-1, 12, 400, 0, "MS Sans Serif") ; this sets the font for the text above

    $YES = GUICtrlCreateButton("Yes, Continue ...", 40, 110, 110, 25, 0) ;  Button co-ordinates:  Left, Top, Width, Height, "0".
    GUICtrlSetFont(-1, 11, 400, 0, "MS Sans Serif") ; this font setting was missing in original script (for the "OK" or "Continue" button).
    GUICtrlSetOnEvent(-1, "Click_YES")

    $SNOOZE = GUICtrlCreateButton("NO, Snooze", 155, 110, 110, 25, 0) ;  Button co-ordinates:  Left, Top, Width, Height, "0".
    GUICtrlSetFont(-1, 10, 400, 0, "MS Sans Serif") ; font settings for the button text.
    GUICtrlSetOnEvent(-1, "Click_SNOOZE")

    $Cancel = GUICtrlCreateButton("NO, Cancel", 270, 110, 110, 25, 0) ;  Button co-ordinates:  Left, Top, Width, Height, "0".
    GUICtrlSetFont(-1, 10, 400, 0, "MS Sans Serif") ; font settings for the button text.
    GUICtrlSetOnEvent(-1, "Click_CANCEL")
    GUISetState(@SW_SHOW)
    #EndRegion---------------------------------------------------------------------------------------------------
    $Action = $_NONE
    
    While 1
        Switch $Action
            Case $_YES
                GUISetState(@SW_HIDE)
                ShellExecute($Task2Perform)
                ExitLoop ; finished

            Case $_SNOOZE
                $Action = $_NONE
                GUISetState(@SW_HIDE)
                $input = InputBox($BoxTitle, "Snooze for how many minutes?", 15)
                $time = $input * 1000 * 60
                Opt("TrayIconHide", 1)
                Sleep($time)
                Beep(3500, 25) ;  Beep(frequency; duration)
                Sleep(5)
                Beep(3500, 25) ;  Beep(frequency; duration)
                Sleep(5)
                Beep(3500, 25) ;  Beep(frequency; duration)
                GUISetState(@SW_SHOW)
                Opt("TrayIconHide", 0)
            Case $_CANCEL
                ExitLoop
        EndSwitch
        Sleep(100)
    WEnd
    GUIDelete()
EndFunc   ;==>_BoxReminder

;EndIf     ; goes with If statement above

Func Click_CANCEL()
    $Action = $_CANCEL ; finished
EndFunc   ;==>Click_CANCEL

Func Click_SNOOZE()
    $Action = $_SNOOZE
EndFunc   ;==>Click_SNOOZE

Func Click_YES()
    $Action = $_YES
EndFunc   ;==>Click_YES

(Main Script)

;
; AutoIt 3x
;
#include "_BOXReminder.au3"
#NoTrayIcon     ; AutoIt's icon doesn't show in systray
TraySetIcon("Shell32.dll", 1004)     ; changes the icon displayed in the systray
AutoItSetOption("WinTitleMatchMode", 2)     ; this allows partial window titles to be valid!
;-------------------------------------------------------------------------------------------------------------------------


;=========================================
$ThisPartition = StringLeft(@ScriptDir, 2)
;=========================================

_BOXReminder("Reminder ...", "Do you need to do this task today ...?", $ThisPartition & "\WORKINGdirs\WorkDir- EXCEL\FIN\zz- OpenFldr- BKP and open file.au3")

[font="Tahoma"]"Tougher than the toughies and smarter than the smarties"[/font]

Share this post


Link to post
Share on other sites

Skruge, thanks! A couple of things there that I didn't know so no wonder I couldn't get it to work <g>.

I changed it a little bit to make it as easy to edit for me as possible. The things we have to do since there are no real portable reminder freewares out there! The ones that say they're portable are actually mostly just standalones since they don't function on relative paths only. But, of course, AutoIt can overcome anything! And with this I now don't even have to bother building a GUI for each message, I just can copy and overwrite that copy with new message needed. Very, very kewl! <g>

"GUI UDF" (minor change, added another UDF reference re relative paths to make life 1-step easier):

;
; AutoIt 3x
;
#include-once
#include<_PartitionLetters.au3>
#include <GUIConstants.au3>
;-------------------------------------------------------------------------------------------------------------------------

Global $Action
Global Enum $_NONE, $_YES, $_SNOOZE, $_CANCEL

Func _BoxReminder($BoxTitle, $BoxBlurb, $Task2Perform)

    #cs
        ; =====================================================================================================================
        ;     VARIABLES THAT WERE IN PLACE BEFORE:
        $BoxTitle     = "Reminder ..."
        $BoxBlurb     = "Do you need to do this task today ...?"
        ;----------------------------------------------------------------------------------------------------------------------
        $Task2Perform = $ThisPartition & "\WORKINGdirs\WorkDir- EXCEL\FIN\zz- OpenFldr- BKP and open file.au3"
        ;======================================================================================================================
    #ce

    Local $Form1_1, $Label2, $YES, $SNOOZE, $Cancel
    
    Sleep(1000)

    Opt("GUIOnEventMode", 1)
    #Region------------------------------------------------------------------------------------------------------
    $Form1_1 = GUICreate($BoxTitle, 420, 155, 195, 125) ; Width, Height, Left, Top (these measurements are for the entire box itself).
    $Label2 = GUICtrlCreateLabel($BoxBlurb, 16, 15, 375, 60) ; Left, top, width, height (these measurements are for the box of text only.)
    GUICtrlSetFont(-1, 12, 400, 0, "MS Sans Serif") ; this sets the font for the text above

    $YES = GUICtrlCreateButton("Yes, Continue ...", 40, 110, 110, 25, 0) ;  Button co-ordinates:  Left, Top, Width, Height, "0".
    GUICtrlSetFont(-1, 11, 400, 0, "MS Sans Serif") ; this font setting was missing in original script (for the "OK" or "Continue" button).
    GUICtrlSetOnEvent(-1, "Click_YES")

    $SNOOZE = GUICtrlCreateButton("NO, Snooze", 155, 110, 110, 25, 0) ;  Button co-ordinates:  Left, Top, Width, Height, "0".
    GUICtrlSetFont(-1, 10, 400, 0, "MS Sans Serif") ; font settings for the button text.
    GUICtrlSetOnEvent(-1, "Click_SNOOZE")

    $Cancel = GUICtrlCreateButton("NO, Cancel", 270, 110, 110, 25, 0) ;  Button co-ordinates:  Left, Top, Width, Height, "0".
    GUICtrlSetFont(-1, 10, 400, 0, "MS Sans Serif") ; font settings for the button text.
    GUICtrlSetOnEvent(-1, "Click_CANCEL")
    GUISetState(@SW_SHOW)
    #EndRegion---------------------------------------------------------------------------------------------------
    $Action = $_NONE
    
    While 1
        Switch $Action
            Case $_YES
                GUISetState(@SW_HIDE)
                ShellExecute($Task2Perform)
                ExitLoop ; finished

            Case $_SNOOZE
                $Action = $_NONE
                GUISetState(@SW_HIDE)
                $input = InputBox($BoxTitle, "Snooze for how many minutes?", 15)
                $time = $input * 1000 * 60
                Opt("TrayIconHide", 1)
                Sleep($time)
                Beep(3500, 25) ;  Beep(frequency; duration)
                Sleep(5)
                Beep(3500, 25) ;  Beep(frequency; duration)
                Sleep(5)
                Beep(3500, 25) ;  Beep(frequency; duration)
                GUISetState(@SW_SHOW)
                Opt("TrayIconHide", 0)
            Case $_CANCEL
                ExitLoop
        EndSwitch
        Sleep(100)
    WEnd
    GUIDelete()
EndFunc   ;==>_BoxReminder

;EndIf     ; goes with If statement above

Func Click_CANCEL()
    $Action = $_CANCEL ; finished
EndFunc   ;==>Click_CANCEL

Func Click_SNOOZE()
    $Action = $_SNOOZE
EndFunc   ;==>Click_SNOOZE

Func Click_YES()
    $Action = $_YES
EndFunc   ;==>Click_YES

And put the variables in usual way I do which I recognize. This pattern I use makes it easy for me to overwrite pertinent parts without much conscious thought as I'm used to this easy format:

;
; AutoIt 3x
;
#include <_BOXReminder.au3>
#NoTrayIcon     ; AutoIt's icon doesn't show in systray
TraySetIcon("Shell32.dll", 1004)     ; changes the icon displayed in the systray
AutoItSetOption("WinTitleMatchMode", 2)     ; this allows partial window titles to be valid!
;-------------------------------------------------------------------------------------------------------------------------




; ====== Edit this part here below, only:  ============================================================================
;     VARIABLES TO BE USED IN THE SCRIPTS THAT CALL THIS UFD:
$BoxTitle     = "Reminder ..."
$BoxBlurb     = "Do you need to do this task today ...?"
;----------------------------------------------------------------------------------------------------------------------
$Task2Perform = $SamePartition & "\WORKINGdirs\WorkDir- EXCEL\FIN\zz- OpenFldr- BKP and open file.au3"
;======================================================================================================================




_BOXReminder($BoxTitle, $BoxBlurb, $Task2Perform)
It works a treat. When I started this message system, I was a real newbie at AutoIt. I've come a long way and things have progressed but now I can go back and make them all in this newer, better message system that now is completely automated with the new UDF system.

Thanks! :)

Share this post


Link to post
Share on other sites

Oops, I found something; I spoke too soon <g>. The YES button is anticipated at all times. AI doesn't wait for the user to press YES; the action is done at the launch of the script each and every time. I thought that removing the ShellExecute bit from the UDF might do the trick, but no go. Since the action is not always a ShellExecute one, I really thought that would do it. (p.s., action might be a ClipPut or a Send, too, etc.)

Taking ShellExecute out didn't fix the problem:

Switch $Action
            Case $_YES
                GUISetState(@SW_HIDE)
                $Task2Perform
                ExitLoop ; finished

How would the script be made so that it waits for user input, no matter what it is (YES, SNOOZE or CANCEL)?

Thank you!! :)

Share this post


Link to post
Share on other sites

Oops, I found something; I spoke too soon <g>. The YES button is anticipated at all times. AI doesn't wait for the user to press YES; the action is done at the launch of the script each and every time. I thought that removing the ShellExecute bit from the UDF might do the trick, but no go. Since the action is not always a ShellExecute one, I really thought that would do it. (p.s., action might be a ClipPut or a Send, too, etc.)

Taking ShellExecute out didn't fix the problem:

Switch $Action
            Case $_YES
                GUISetState(@SW_HIDE)
                $Task2Perform
                ExitLoop ; finished

How would the script be made so that it waits for user input, no matter what it is (YES, SNOOZE or CANCEL)?

Thank you!! :)

It all works fine for me. The ShellExecute portion only runs when I click "Yes, Continue...".

Make sure you don't have an older copy of _BOXReminder.au3 sitting in one of your include directories.


[font="Tahoma"]"Tougher than the toughies and smarter than the smarties"[/font]

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

I got it. It took me a day away from this to catch where the problem was! <g> I knew it wasn't that I had anything extra in the Include folder as I ran into that type of problem some time before and now just put shortcuts to the files to my desktop and edit the files by accessing those shortcuts. That avoids duplicating, etc.

No, the problem lay with the variables. I didn't realize it but you, naturally in hindsight, used my "user" variables in the function where they only really need plain, "machine" variables, or whatever the hell I can call them <g>. So I ended up just changing the UDF ones to simpler $Title, $Blurb and $Task whereas for the user I kept the $BoxTitle, $BoxBlurb and $Task2perform. That did the trick, it seems. Very clear in retrospect <g>.

I'll start converting all the messages boxes and will see if they hold up. I don't see why not, but then one can't always tell ahead of time.

Thanks. I'll report back.

Edited by Diana (Cda)

Share this post


Link to post
Share on other sites

Argghhhhh <pulling hair out>. Like ALWAYS, I wish I had more knowledge or the time and the money to gain more knowledge other than the way I have to do this by trial and error and no programming courses <sigh>.

The key seems to be the variables, but I can't see where I'm going wrong. I need the variables in the box the user will re-use and will edit time and again when making new message boxes.

Your example, Skruge, works. But when I try to customize it the action is peformed on script startup and it doesn't wait for user input. That means this script is limited at this stage until I can figure out how where I'm going wrong.

Thanks.

Share this post


Link to post
Share on other sites

Okay, a glimmer here. ShellExecute is a real bad thing in here. This script cannot accommodate anything that isn't very, very simple. I broke the script down in pieces in terms of the script.

In the script calling the udf, anything other than the simple structure used in the example of this thread, _BOXReminder("Reminder ...", "Do you need to do this task today ...?", $ThisPartition & "\WORKINGdirs\WorkDir- EXCEL\FIN\zz- OpenFldr- BKP and open file.au3"), specifically:

$ThisPartition & "\WORKINGdirs\WorkDir- EXCEL\FIN\zz- OpenFldr- BKP and open file.au3"

doesn't work. In the UDF, shellexecute is used in the action, but as I mentioned, this is only one of the types of actions that might be needed. It isn't always what is required. We can have a ClipPut and Send keys or a message box, etc. Also, a more complex ShellExecute such as this:

ShellExecute(@ScriptDir & "\0-Test.txt", "", "", "Open", @SW_MAXIMIZE)

creates parsing problems, etc. It just won't work.

Any clues, anyone?

How can the function in the UDF: Func _BoxReminder($BoxTitle, $BoxBlurb, $Task2Perform)

work with the script's line _BoxReminder($BoxTitle, $BoxBlurb, $Task2Perform) while using something like this for the action to be done:

$Task2Perform = ShellExecute(@ScriptDir & "\0-Test.txt", "", "", "Open", @SW_MAXIMIZE)

or

$Task2Perform = MsgBox(48,"test","test")

etc.

Initially, when I used one set of variables for the UDF and another for the script, it seemed to work but I think that was a fluke in that the task I was doing was not a shellexecute one, perhaps. I can't remember what I did but now nothing works unless I follow Skruge's format _exactly_, which is too limited to change over to.

Thanks everyone. Hoping to get this to work. I need to streamline this whole process and having a GUI accessed from a UDF rather than each script building the GUI would be a big giant step forward.

:)

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