Sign in to follow this  
Followers 0
nf67

Functions that keep repeating themselves...

8 posts in this topic

#1 ·  Posted (edited)

Hi there,

I was trying to create a simple launcher just to practice a bit, and I encountered some rather annoying problems (including system crashes :) ).

My code is pretty simple to read I think, I've commented the things that are causing trouble, here's a summary:

1- My script has functions that are triggered by hotkeys and buttons, if I press a hotkey once then it should execute that function once. However, it seems more like it's repeating at the moment.

For example, if I press a hotkey/button to launch a program it launches that program again and again and again (eventually causing my system to crash). I have no idea what line of code causes this

"repeat", so I put the comment at the top of my script.

2- I couldn't get a .png to load into my GUI, probably caused by a small typo or something, I just can't find out what. (See ; #2)

3- The functions in my script that launch programs also hide the GUI again, so you can click a program in the GUI, the program launches, and the GUI hides again. But since my functions keep repeating (See ;

#1) The GUI also keeps hiding, if I press the hotkey to show the GUI, it just flickers because it is instantly hidden again by the repeating function. (See ; #3)

4- My script has 2 GUIs, but if I close one of them(using the Exit command) the entire program closes, and not just 1 GUI. I tried WinClose but failed. (See ; #4)

5- I don't know how to empty a .txt file that is used as a resource by my script, I need to overwrite (or Empty -> Rewrite) the lines of the .txt instead of adding new ones at the bottom. (See ; #5)

Here's my script, including comments describing my problems:

#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>

;#1 - If there is a filepath defined in Options.txt, then the program will spamlaunch that program, regardless of pressing a button/hotkey.
; I think this problem is caused by a func that keeps repeating, in that case it&apos;s also the reason for problem #3.

$icanhasrun = GUICreate("icanhasrun", 660, 128, "500", "500", $WS_POPUPWINDOW, $WS_EX_TOPMOST )
$Pic1 = GUICtrlCreatePic(@ScriptDir & "\Icon1.png", 0, 0, 128, 128) ; #2 - Doesn&apos;t seem to load the picture
$Pic2 = GUICtrlCreatePic(@ScriptDir & "\Icon2.png", 133, 0, 128, 128); #2 - Doesn&apos;t seem to load the picture
$Pic3 = GUICtrlCreatePic(@ScriptDir & "\Icon3.png", 266, 0, 128, 128); #2 - Doesn&apos;t seem to load the picture
$Pic4 = GUICtrlCreatePic(@ScriptDir & "\Icon4.png", 399, 0, 128, 128); #2 - Doesn&apos;t seem to load the picture
$Pic5 = GUICtrlCreatePic(@ScriptDir & "\Icon5.png", 532, 0, 128, 128); #2 - Doesn&apos;t seem to load the picture
$ShowHideStatus = 0
GUISetState(@SW_HIDE)
HotKeySet ("{NumPadDot}", "Show")
HotKeySet ("{NumPad1}", "One")
HotKeySet ("{NumPad2}", "Two")
HotKeySet ("{NumPad3}", "Three")
HotKeySet ("{NumPad4}", "Four")
HotKeySet ("{NumPad5}", "Five")
HotKeySet ("{NumPadAdd}", "Options")

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
    Case $GUI_EVENT_CLOSE
            Exit
        Case $Pic1 
            One()
        Case $Pic2 
            Two()
        Case $Pic3 
            Three()
        Case $Pic4 
            Four()
        Case $Pic5 
            Five()
    EndSwitch
WEnd

Func Show()
    If $ShowHideStatus = 0 Then
    GUISetState(@SW_SHOW)
    $ShowHideStatus = 1
    Else
    GUISetState(@SW_HIDE)
    $ShowHideStatus = 0
    EndIf
EndFunc

Func One()
    $Prog1 = FileReadLine( @ScriptDir & "\Options.txt", 1)
    Run($Prog1)
    ;GUISetState(@SW_HIDE)  ;#3 - If this is enabled, then the GUI will instantly hide again after pressing {NumPadDot}(The hotkey to show the GUI).
    ;$ShowHideStatus = 0    
EndFunc

Func Two()
    $Prog2 = FileReadLine( @ScriptDir & "\Options.txt", 2)
    Run($Prog2)
    ;GUISetState(@SW_HIDE) ;#3 - If this is enabled, then the GUI will instantly hide again after pressing {NumPadDot}(The hotkey to show the GUI).
    ;$ShowHideStatus = 0    
EndFunc

Func Three()
    $Prog3 = FileReadLine( @ScriptDir & "\Options.txt", 3)
    Run($Prog3)
    ;GUISetState(@SW_HIDE) ;#3 - If this is enabled, then the GUI will instantly hide again after pressing {NumPadDot}(The hotkey to show the GUI).
    ;$ShowHideStatus = 0    
EndFunc

Func Four()
    $Prog4 = FileReadLine( @ScriptDir & "\Options.txt", 4)
    Run($Prog4)
    ;GUISetState(@SW_HIDE) ;#3 - If this is enabled, then the GUI will instantly hide again after pressing {NumPadDot}(The hotkey to show the GUI).
    ;$ShowHideStatus = 0    
EndFunc

Func Five()
    $Prog5 = FileReadLine( @ScriptDir & "\Options.txt", 5)
    Run($Prog5)
    ;GUISetState(@SW_HIDE) ;#3 - If this is enabled, then the GUI will instantly hide again after pressing {NumPadDot}(The hotkey to show the GUI).
    ;$ShowHideStatus = 0    
EndFunc

Func Options()

    $Options = GUICreate("Options", 129, 141, 600, 600, $WS_POPUPWINDOW, $WS_EX_TOPMOST )
    $Input1 = GUICtrlCreateInput(FileReadLine( @ScriptDir & "\Options.txt", 1), 0, 0, 128, 21)
    $Input2 = GUICtrlCreateInput(FileReadLine( @ScriptDir & "\Options.txt", 2), 0, 24, 128, 21)
    $Input3 = GUICtrlCreateInput(FileReadLine( @ScriptDir & "\Options.txt", 3), 0, 48, 128, 21)
    $Input4 = GUICtrlCreateInput(FileReadLine( @ScriptDir & "\Options.txt", 4), 0, 72, 128, 21)
    $Input5 = GUICtrlCreateInput(FileReadLine( @ScriptDir & "\Options.txt", 5), 0, 96, 128, 21)
    $SaveButton = GUICtrlCreateButton("Save", 0, 120, 128, 20)
    GUISetState(@SW_SHOW)


    While 1
        $nMsg = GUIGetMsg()
        Switch $nMsg
            If _IsPressed("6B") then  
            Exit ; #4 - Needs something that only closes the Options window, not the entire program.
            EndIf
            Case $GUI_EVENT_CLOSE
            Exit ; #4 - Needs something that only closes the Options window, not the entire program.
            Case $SaveButton
            FileDelete( @ScriptDir & "\Options.txt") ; #5 - Any other way to overwrite previous lines than to delete the whole file?
            FileWrite( @ScriptDir & "\Options.txt", GUICtrlRead($Input1) & @CRLF) 
            FileWrite( @ScriptDir & "\Options.txt", GUICtrlRead($Input2) & @CRLF) 
            FileWrite( @ScriptDir & "\Options.txt", GUICtrlRead($Input3) & @CRLF) 
            FileWrite( @ScriptDir & "\Options.txt", GUICtrlRead($Input4) & @CRLF) 
            FileWrite( @ScriptDir & "\Options.txt", GUICtrlRead($Input5) & @CRLF) 
            Exit ; #4 - Needs something that only closes the Options window, not the entire program.
        EndSwitch
    WEnd
    
EndFunc

Thanks a lot :party: .

Edit: Added a .zip including the icons (and the script ofcourse).

AutoIt_Launcher.zip

Edited by nf67

Share this post


Link to post
Share on other sites



1. I assume the GUICtrlCreatePic fails thus sets $pic1 to 0 which makes your Select Case be true for $pic1

2. supported types BMP, JPG, GIF

3. I assume the GUICtrlCreatePic fails thus sets $pic1 to 0 which makes your Select Case be true for $pic1

4. use return in stead of Exit

5. Use $fh=FileOpen("filename",1) and write the lines to the returned Filehandle.

Jos


Visit the SciTE4AutoIt3 Download page for the latest versions  - Beta files                                How to post scriptsource        Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites

Hi there,

I was trying to create a simple launcher just to practice a bit, and I encountered some rather annoying problems (including system crashes :) ).

My code is pretty simple to read I think, I've commented the things that are causing trouble, here's a summary:

1- My script has functions that are triggered by hotkeys and buttons, if I press a hotkey once then it should execute that function once. However, it seems more like it's repeating at the moment.

For example, if I press a hotkey/button to launch a program it launches that program again and again and again (eventually causing my system to crash). I have no idea what line of code causes this

"repeat", so I put the comment at the top of my script.

2- I couldn't get a .png to load into my GUI, probably caused by a small typo or something, I just can't find out what. (See ; #2)

RTFM for GUICtrlCreatePic. It says: "filename of the picture to be loaded : supported types BMP, JPG, GIF(but not animated)." See the second example in the help file for information on how to display it.

3- The functions in my script that launch programs also hide the GUI again, so you can click a program in the GUI, the program launches, and the GUI hides again. But since my functions keep repeating (See ;

#1) The GUI also keeps hiding, if I press the hotkey to show the GUI, it just flickers because it is instantly hidden again by the repeating function. (See ; #3)

If we fix the repeat it should work

4- My script has 2 GUIs, but if I close one of them(using the Exit command) the entire program closes, and not just 1 GUI. I tried WinClose but failed. (See ; #4)

GUIDelete() + GUISwitch() + ExitLoop

5- I don't know how to empty a .txt file that is used as a resource by my script, I need to overwrite (or Empty -> Rewrite) the lines of the .txt instead of adding new ones at the bottom. (See ; #5)

Check FileOpen() and take a closer look at the flags

Here's my script, including comments describing my problems:

-SNIP-

Thanks a lot :party: .

Edit: Added a .zip including the icons (and the script ofcourse).

Hi there.

Please remember to try use Code over AutoIt until they are fixed. A lot of the time they will screw up.

Do you use the full version of SciTE? If not you should download it. Link is in my sig.

Please see my responses in blue in the quote.

Also I did the following things:

-Added the following include that was missing:

#Include <Misc.au3>

- Fixed the switch statement in Options function. If statement can not reside in the switch, but rather in can in a case statement. I just moved it into the main loop.

While 1
    $nMsg = GUIGetMsg()
;####################### I CAN'T SEE HOW THIS IS MEANT TO WORK INSIDE A SWITCH STATEMENT... MOVED IT HERE.
    If _IsPressed("6B") Then
        Exit; #4 - Needs something that only closes the Options window, not the entire program.
    EndIf
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit; #4 - Needs something that only closes the Options window, not the entire program.
        Case $SaveButton
            FileDelete(@ScriptDir & "\Options.txt"); #5 - Any other way to overwrite previous lines than to delete the whole file?
            FileWrite(@ScriptDir & "\Options.txt", GUICtrlRead($Input1) & @CRLF)
            FileWrite(@ScriptDir & "\Options.txt", GUICtrlRead($Input2) & @CRLF)
            FileWrite(@ScriptDir & "\Options.txt", GUICtrlRead($Input3) & @CRLF)
            FileWrite(@ScriptDir & "\Options.txt", GUICtrlRead($Input4) & @CRLF)
            FileWrite(@ScriptDir & "\Options.txt", GUICtrlRead($Input5) & @CRLF)
            Exit; #4 - Needs something that only closes the Options window, not the entire program.
    EndSwitch
WEnd

- Changed your logic. Why have 5 functions when you can have one!

Func LaunchProg($line)
    $Prog1 = FileReadLine(@ScriptDir & "\Options.txt", $line)
    Run($Prog1)
;GUISetState(@SW_HIDE) ;#3 - If this is enabled, then the GUI will instantly hide again after pressing {NumPadDot}(The hotkey to show the GUI).
;$ShowHideStatus = 0
EndFunc  ;==>LaunchProg

See the full edited code here. Give it a go, and if it doesn't work, then take a closer look at what was suggested.

*UNTESTED*

#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <Misc.au3>

;#1 - If there is a filepath defined in Options.txt, then the program will spamlaunch that program, regardless of pressing a button/hotkey.
; I think this problem is caused by a func that keeps repeating, in that case it&apos;s also the reason for problem #3.

$icanhasrun = GUICreate("icanhasrun", 660, 128, "500", "500", $WS_POPUPWINDOW, $WS_EX_TOPMOST)
$Pic1 = GUICtrlCreatePic(@ScriptDir & "\Icon1.png", 0, 0, 128, 128); #2 - Doesn&apos;t seem to load the picture
$Pic2 = GUICtrlCreatePic(@ScriptDir & "\Icon2.png", 133, 0, 128, 128); #2 - Doesn&apos;t seem to load the picture
$Pic3 = GUICtrlCreatePic(@ScriptDir & "\Icon3.png", 266, 0, 128, 128); #2 - Doesn&apos;t seem to load the picture
$Pic4 = GUICtrlCreatePic(@ScriptDir & "\Icon4.png", 399, 0, 128, 128); #2 - Doesn&apos;t seem to load the picture
$Pic5 = GUICtrlCreatePic(@ScriptDir & "\Icon5.png", 532, 0, 128, 128); #2 - Doesn&apos;t seem to load the picture
$ShowHideStatus = 0
GUISetState(@SW_HIDE)
HotKeySet("{NumPadDot}", "Show")
HotKeySet("{NumPad1}", "LaunchProg")
HotKeySet("{NumPad2}", "LaunchProg")
HotKeySet("{NumPad3}", "LaunchProg")
HotKeySet("{NumPad4}", "LaunchProg")
HotKeySet("{NumPad5}", "LaunchProg")
HotKeySet("{NumPadAdd}", "Options")

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Pic1
            LaunchProg(1)
        Case $Pic2
            LaunchProg(2)
        Case $Pic3
            LaunchProg(3)
        Case $Pic4
            LaunchProg(4)
        Case $Pic5
            LaunchProg(5)
    EndSwitch
WEnd

Func Show()
    If $ShowHideStatus = 0 Then
        GUISetState(@SW_SHOW)
        $ShowHideStatus = 1
    Else
        GUISetState(@SW_HIDE)
        $ShowHideStatus = 0
    EndIf
EndFunc  ;==>Show

Func LaunchProg($line = 0)
    Switch @HotKeyPressed
        Case "{NumPad1}"
            $line = 1
        Case "{NumPad2}"
            $line = 2
        Case "{NumPad3}"
            $line = 3
        Case "{NumPad4}"
            $line = 4
        Case "{NumPad5}"
            $line = 5
    EndSwitch
    
    $Prog = FileReadLine(@ScriptDir & "\Options.txt", $line)
    MsgBox (0, "", $Prog)
    
;Run($Prog)
;GUISetState(@SW_HIDE) ;#3 - If this is enabled, then the GUI will instantly hide again after pressing {NumPadDot}(The hotkey to show the GUI).
;$ShowHideStatus = 0
EndFunc  ;==>LaunchProg

Func Options()

    $Options = GUICreate("Options", 129, 141, 600, 600, $WS_POPUPWINDOW, $WS_EX_TOPMOST)
    $Input1 = GUICtrlCreateInput(FileReadLine(@ScriptDir & "\Options.txt", 1), 0, 0, 128, 21)
    $Input2 = GUICtrlCreateInput(FileReadLine(@ScriptDir & "\Options.txt", 2), 0, 24, 128, 21)
    $Input3 = GUICtrlCreateInput(FileReadLine(@ScriptDir & "\Options.txt", 3), 0, 48, 128, 21)
    $Input4 = GUICtrlCreateInput(FileReadLine(@ScriptDir & "\Options.txt", 4), 0, 72, 128, 21)
    $Input5 = GUICtrlCreateInput(FileReadLine(@ScriptDir & "\Options.txt", 5), 0, 96, 128, 21)
    $SaveButton = GUICtrlCreateButton("Save", 0, 120, 128, 20)
    GUISetState(@SW_SHOW)


    While 1
        $nMsg = GUIGetMsg()
    ;####################### I CAN'T SEE HOW THIS IS MEANT TO WORK INSIDE A SWITCH STATEMENT... MOVED IT HERE.
        If _IsPressed("6B") Then
        ;ExitLoop
        EndIf
        
        Switch $nMsg
            Case $GUI_EVENT_CLOSE
                ExitLoop
            Case $SaveButton
                $filehndl = FileOpen (@ScriptDir & "\Options.txt", 2); #5 - Any other way to overwrite previous lines than to delete the whole file?
                FileWrite($filehndl, GUICtrlRead($Input1) & @CRLF & _
                                    GUICtrlRead($Input2) & @CRLF & _ 
                                    GUICtrlRead($Input3) & @CRLF & _ 
                                    GUICtrlRead($Input4) & @CRLF & _ 
                                    GUICtrlRead($Input5))
                FileClose ($filehndl)
                ExitLoop; #4 - Needs something that only closes the Options window, not the entire program.
        EndSwitch
    WEnd
    
    GUISwitch ($icanhasrun)
    GUIDelete ($Options)
EndFunc  ;==>Options

Cheers,

Brett

Share this post


Link to post
Share on other sites

Thank you, testing atm :)

Share this post


Link to post
Share on other sites

Thank you Jos and Brett, I've implented the things you mentioned, I already thought of 1 func instead of 5, but I didn't know about this part

Switch @HotKeyPressed
        Case "{NumPad1}"
            $line = 1
        Case "{NumPad2}"
            $line = 2
        Case "{NumPad3}"
            $line = 3
        Case "{NumPad4}"
            $line = 4
        Case "{NumPad5}"
            $line = 5
    EndSwitchoÝ÷ ØiZw*.¦-yØ­~æy:&jG¢·
+­ÉbrH§§Øu×jëh×6        Case $Pic1 
            $ProgNumber = 1
            Launch()

But I figured using Variables when calling a func (Like LaunchProg(1)) as you showed would be better, first time I'm using it, thanks.

The reason I thought .png was supported was because someone mentioned using a .png in someone else's topic, but I later saw that it involved a UDF :) .

I didn't understand the FileHandel thing jos suggested, even though Brett posted an example, and since this is mainly for practicing I think I won't use it and look it up later so I can understand what it does.

Going to convert the PNGs now ;-)

Share this post


Link to post
Share on other sites

Had a play with your code nf67, sorry but I got carried away..lol

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