Jump to content
Sign in to follow this  
Mast3rpyr0

While statement in a Switch/Array Help

Recommended Posts

Mast3rpyr0

Hi this is my first post (been here away but havent registered till now) and i got a little problem with a script that will run a program when I click a button. Basically it wont let me put a While in the Switch function. I could start the while outside of the Switch but i only need it to change the cases. Here is my Current Code:

While 1
    $tmsg = TrayGetMsg()
    $nMsg = GUIGetMsg()
    Switch $nMsg
    Case $Program_[$i]
        Run($Path_[$i])
        Sleep(3000)
    EndSwitch
    WEnd

And here is what i tried before:

While 1
    $tmsg = TrayGetMsg()
    $nMsg = GUIGetMsg()
    Switch $nMsg
                While $Progs >=$i
                 $i = $i+1
    Case $Program_[$i]
        Run($Path_[$i])
        Sleep(3000)
    EndSwitch
    WEnd
WEnd

Any suggestions?

Mast3rpyr0

Edited by Mast3rpyr0

Share this post


Link to post
Share on other sites
Jos

Maybe first tell us what you want to accomplish ?

Anyway.. You will have to Finish the While with a Wend before any Case statement.

Also you cannot have a While after Switch ... it has to be inside a Case .....

Look at the Helpfile for the proper syntax.

:)

Edited by JdeB

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     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
Mast3rpyr0

Ok basically what it will do is $Prog is a constant set from an INI that contains the number of programs that this program will run. Im not sure but i think the Case would have to be in the while statment for it to do what its supposed Which is change $i and cause it to create a new Case so: Case $Program _[$i] = $Program_[1] then loop aruond and make Case $Program_[2]

until it reaches $Prog.

Mast3rpyr0

Share this post


Link to post
Share on other sites
Jos

Ok basically what it will do is $Prog is a constant set from an INI that contains the number of programs that this program will run. Im not sure but i think the Case would have to be in the while statment for it to do what its supposed Which is change $i and cause it to create a new Case so: Case $Program _[$i] = $Program_[1] then loop aruond and make Case $Program_[2]

until it reaches $Prog.

Mast3rpyr0

Something like this ?

$h_Button = GUICtrlCreateButton ("Run Programs",  10, 30, 100)
;
;
;
;

While 1
    $tmsg = TrayGetMsg()
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $h_Button
            While $Progs >= $i
                Run($Path_[$i])
                $i = $i + 1
                Sleep(3000)
            WEnd
    EndSwitch
WEnd

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     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
PsaltyDS

That's kind of silly. A Switch with only one Case is just a glorified IF. So you have GUI IDs in the $Program_ array and matching paths to run in the $Path_ array. Just use a For/Next loop to look for the match:

While 1
    $nMsg = GUIGetMsg()
    For $p = 0 To UBound($Program_) - 1
        If $nMsg = $Program_[$p] Then
            Run($Path_[$p])
            Sleep(3000)
            ExitLoop
        EndIf
    Next
WEnd

:)

Edited by PsaltyDS

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites
Mast3rpyr0

Ok that seemed to work, i dont get an error message now when trying to run it, but my problem now is that it will like start the program in the tray and close instantly, it wont even start a service for it. I may have the flags wrong in my minimize functions...

#include <GUIConstants.au3>
#include <Constants.au3>
Opt("TrayOnEventMode",1)
Opt("TrayMenuMode",1)

TraySetOnEvent($TRAY_EVENT_PRIMARYUP,"SpecialEvent")
;TraySetState(2); hide --> not needed

;Set Vars
Global $ScriptDir = @ScriptDir & "GLaunch\Launcher Files"
Global $ConfigINI = "\Info Files\Config.ini"
$ver = IniRead($ScriptDir, "Version", "ver", "")
Const $Progs = IniRead($ScriptDir & $ConfigINI, "Progs", "num", "")
$i = 1

;Functions
Func SpecialEvent()
    GuiSetState(@SW_Show)
    TraySetState(2); hide
EndFunc



;Grab From INI
If $Progs > 0 Then
    While $Progs >= $i
        $Name_[$i] = IniRead($ScriptDir & $ConfigINI, "Name", $i, "")
        $Dir_[$i] = IniRead($ScriptDir & $ConfigINI, "Dir", $i, "")
        $i = $i=1
    WEnd
    
;Create Window
    $i = 1
    Local $s = 80
    $MainWindow = GUICreate("Game Launcher v1.0", 415, 290)
    $Tab1 = GUICtrlCreateTab(0, 0, 417, 273)
    $Programs = GUICtrlCreateTabItem("Games");===>Games Tab
    While $Progs > $i
    $Program_[$i] = GUICtrlCreateButton($Name_[$1], 237, $s, 120, 17)
    $Path_[$i] = IniRead($ScriptDir & $ConfigINI, "Path", $i, "")
    $i = $i+1
    $s = $s+20
WEnd

GUISetState(@SW_SHOW)

;Launch a Program
While 1
    $nMsg = GUIGetMsg()
    $tmsg = TrayGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $GUI_EVENT_MINIMIZE
            GuiSetState(@SW_HIDE)
            TraySetState(1); show
            TraySetToolTip ("Game Launcher - Click here to restore")
        Case $Exit
            ExitLoop
        EndSwitch
    For $p = 0 To UBound($Program_) - 1
        If $nMsg = $Program_[$p] Then
            Run($Path_[$p])
            Sleep(3000)
            ExitLoop
        EndIf
    Next
WEnd
EndIf

I dont think it would be because i havent wrote the INI yet but maybe...

Edited by Mast3rpyr0

Share this post


Link to post
Share on other sites
PsaltyDS

Line 29 is probably a typo:

; $i = $i = 1
        $i = $i + 1oÝ÷ Øô­ëazǬºh±çm¡·¬¢g­)à¢Ø^®ØZªëk*Þ}êÞǬjëh×6     $Name_[$i] = IniRead($ScriptDir & $ConfigINI, "Name", $i, "")
        $Dir_[$i] = IniRead($ScriptDir & $ConfigINI, "Dir", $i, "")

Same strange array references throughout...

Edited by PsaltyDS

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites
Mast3rpyr0

Ok Basically What those do is store the data from the INI to an array(I think thats what you said...) so $Dir_[1] is the ..... Scratch that part, just realize I could merge that with the next Sectionwhich now makes it this:

;Create Window
    If $Progs > 0 Then
    $i = 1
    Local $s = 80
    $MainWindow = GUICreate("Game Launcher v1.0", 415, 290)
    $Tab1 = GUICtrlCreateTab(0, 0, 417, 273)
    $Programs = GUICtrlCreateTabItem("Games");===>Games Tab
    While $Progs > $i
    $Name_[$i] = IniRead($ScriptDir & $ConfigINI, "Name", $i, "")
    $Program_[$i] = GUICtrlCreateButton($Name_[$1], 237, $s, 120, 17)
    $Path_[$i] = IniRead($ScriptDir & $ConfigINI, "Path", $i, "")
    $i = $i+1
    $s = $s+20
WEnd

Thanks again :)

Still cant seem to fix the program not starting at all tho...

Edited by Mast3rpyr0

Share this post


Link to post
Share on other sites
gsglive

Try a

Do
;stuff
; if condition = satisfied then
$ok = 1
;endif
Until $ok = 1

- gsglive

Share this post


Link to post
Share on other sites
PsaltyDS

Ok Basically What those do is store the data from the INI to an array(I think thats what you said...) so $Dir_[1] is the ..... Scratch that part, just realize I could merge that with the next Sectionwhich now makes it this:

;Create Window
    If $Progs > 0 Then
    $i = 1
    Local $s = 80
    $MainWindow = GUICreate("Game Launcher v1.0", 415, 290)
    $Tab1 = GUICtrlCreateTab(0, 0, 417, 273)
    $Programs = GUICtrlCreateTabItem("Games");===>Games Tab
    While $Progs > $i
    $Name_[$i] = IniRead($ScriptDir & $ConfigINI, "Name", $i, "")
    $Program_[$i] = GUICtrlCreateButton($Name_[$1], 237, $s, 120, 17)
    $Path_[$i] = IniRead($ScriptDir & $ConfigINI, "Path", $i, "")
    $i = $i+1
    $s = $s+20
WEnd

Thanks again :)

Still cant seem to fix the program not starting at all tho...

You're not getting off that easy... :)

Where do the $Name_, $Program_, and $Path_ arrays get created? I'm thinking you are not at all fluent in array-speak.

:)


Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites
PsaltyDS

ya... not really. :)

OK. Let's figure it out.

First step = minimize the script. This is your script from post #6, with Tray stuff removed. You can easily put that back after the core of your script works:

#include <GUIConstants.au3>
#include <Constants.au3>

;Set Vars
Global $ScriptDir = @ScriptDir & "GLaunch\Launcher Files"
Global $ConfigINI = "\Info Files\Config.ini"
$ver = IniRead($ScriptDir, "Version", "ver", "")
Const $Progs = IniRead($ScriptDir & $ConfigINI, "Progs", "num", "")
$i = 1

;Grab From INI
If $Progs > 0 Then
    While $Progs >= $i
        $Name_[$i] = IniRead($ScriptDir & $ConfigINI, "Name", $i, "")
        $Dir_[$i] = IniRead($ScriptDir & $ConfigINI, "Dir", $i, "")
        $i = $i = 1
    WEnd

    ;Create Window
    $i = 1
    Local $s = 80
    $MainWindow = GUICreate("Game Launcher v1.0", 415, 290)
    $Tab1 = GUICtrlCreateTab(0, 0, 417, 273)
    $Programs = GUICtrlCreateTabItem("Games");===>Games Tab
    While $Progs > $i
        $Program_[$i] = GUICtrlCreateButton($Name_[$1], 237, $s, 120, 17)
        $Path_[$i] = IniRead($ScriptDir & $ConfigINI, "Path", $i, "")
        $i = $i + 1
        $s = $s + 20
    WEnd
    GUISetState(@SW_SHOW)

    ;Launch a Program
    While 1
        $nMsg = GUIGetMsg()
        Switch $nMsg
            Case $GUI_EVENT_CLOSE
                Exit
            Case $GUI_EVENT_MINIMIZE
                GUISetState(@SW_HIDE)
            Case $Exit
                ExitLoop
        EndSwitch
        For $p = 0 To UBound($Program_) - 1
            If $nMsg = $Program_[$p] Then
                Run($Path_[$p])
                Sleep(3000)
                ExitLoop
            EndIf
        Next
    WEnd
EndIf

Second, check your "; Set vars" section. You are missing some backslashes.

1. The macro @ScriptDir does not include a trailing backslash so your path is broken.

2. Look closely at the IniRead() for $ver - what is the path to the ini file...?

3. The "Const" keyword is misused - look it up in the help file and fix it.

Take the minimized version above and fix the "; Set vars" part.

Then post it back, along with a sample .ini file for us to work with.

We'll work our way down and eventually get to the array weirdness.

:)


Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites
Mast3rpyr0

ok i did that stuff you said, stil looking up the const thing and arrays. I think i need _ArrayCreat maybe?

EDIT:

Ok here's the script now:

#include <GUIConstants.au3>
#include <Constants.au3>

;Set Vars
Global $ScriptDir = @ScriptDir & "\GLaunch\Launcher Files"
Global $ConfigINI = "\Info Files\Config.ini"
$ver = IniRead($ScriptDir & $ConfigINI, "Version", "ver", "")
Const $Progs = IniRead($ScriptDir & $ConfigINI, "Progs", "num", "")
$i = 1

    ;Create Window
    If $Progs > 0 Then
    $i = 1
    Local $s = 80
    $MainWindow = GUICreate("Game Launcher v1.0", 415, 290)
    $Tab1 = GUICtrlCreateTab(0, 0, 417, 273)
    $Programs = GUICtrlCreateTabItem("Games");===>Games Tab
    While $Progs > $i
    $Name_[$i] = IniRead($ScriptDir & $ConfigINI, "Name", $i, "")
    $Program_[$i] = GUICtrlCreateButton($Name_[$1], 237, $s, 120, 17)
    $Path_[$i] = IniRead($ScriptDir & $ConfigINI, "Path", $i, "")
    $i = $i+1
    $s = $s+20
WEnd
    GUISetState(@SW_SHOW)

    ;Launch a Program
    While 1
        $nMsg = GUIGetMsg()
        Switch $nMsg
            Case $GUI_EVENT_CLOSE
                Exit
            Case $GUI_EVENT_MINIMIZE
                GUISetState(@SW_HIDE)
            Case $Exit
                ExitLoop
        EndSwitch
        For $p = 0 To UBound($Program_) - 1
            If $nMsg = $Program_[$p] Then
                Run($Path_[$p])
                Sleep(3000)
                ExitLoop
            EndIf
        Next
    WEnd
EndIf

Here's the Config.ini:

[Version]
ver=1.0

[Progs]
num=2

[Name]
1=IE
2=WMP

[Path]
1=C:\Program Files\Internet Explorer\iexplore.exe
2=C:\Program Files\Windows Media Player\wmplayer.exe
Edited by Mast3rpyr0

Share this post


Link to post
Share on other sites
PsaltyDS

ok i did that stuff you said, stil looking up the const thing and arrays. I think i need _ArrayCreat maybe?

EDIT:

Ok here's the script now:

Here's the Config.ini:

Cool. So far, so good.

You are not going to ever need Const much. I would drop it and just use Global or Dim to declare $ver and $Progs.

Next, in the same area as those, use IniReadSection() functions to read the [Name] section into $Name and the [Path] section into $Path. The underbar character at the end of those names is not needed, but you can include them if you like.

Now we get into the wonderful world of Arrays. A one-dimensional array (1D) is just a numbered list, with the first item always numbered 0. A two-dimensional array (2D) is just a table, like a spreadsheet, with rows and columns. The first row and first column are still numbered 0. So, to access the first element in a 1D array, it looks like this: $Array[0] and the second element is $Array[1]. A 2D array needs both a row and column reference, so the first element in a 2D array is $Array[0][0] and the second column in the first row is $Array[0][1].

When you use IniReadSection(), it returns a 2D array. Each row is one line from the section. Column 0 is the value name (like "ver"), and Column 1 is the value data (like "1.0"). The first row is used to indicate how many values were read. So $Array[0][0] = count, and the first value is in $Array[1][0] (the name), and $Array[1][1] (the data).

In your script, for example, when you read the [Name] section into the variable $Name with IniReadSection, the data will look like this:

$Name[0][0] = 2 ; count

$Name[1][0] = "1"
$Name[1][1] = "IE"

$Name[2][0] = "2"
$Name[2][1] = "WMP"

Your $Program array seems to hold the control IDs of the buttons you create for each program. You need to declare the array, but don't have any data to put in it yet. So use Global to declare a 1D array called $Program and use the $Progs value you got from the INI to set the number of elements in it.

Read the help file stuff on arrays, update your script for the stuff above, and we'll move on from there.

:)


Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites
Mast3rpyr0

So like this?

EDIT: OK what was here was completely wrong.

I got it now to what its supposed to be but now i get a new error:

Posted Image

I would like to stick with just IniRead() since i know it works and its already there. I just wanted to use a constant for $Progs to make sure it stayed the same.

Edited by Mast3rpyr0

Share this post


Link to post
Share on other sites
PsaltyDS

So like this?

Dim Const $Progs = IniRead($ScriptDir & $ConfigINI, "Progs", "num", "")
Global $Programs_ = $ProgsoÝ÷ Ûú®¢×¢·¥G­+ºÚ"µÍÛØ[  ÌÍÔÙÜ[×ÖÉÌÍÔÙÜ×oÝ÷ Ú«¨µâ0¢é])¶-É0ØcºËH$^iÛ"Ç¢z0Ü(®KØ­±©ky§r¶«{ú®¢×£«v'Â÷öØ·*.j|°êÞßtÚæ°jY(Ø^Â)]²'^Â+a²jºÚÊÍ÷jëh×6;Set Vars
Global $ScriptDir = @ScriptDir & "\GLaunch\Launcher Files"
Global $ConfigINI = "\Info Files\Config.ini"
Global $ver = IniRead($ScriptDir & $ConfigINI, "Version", "ver", "")
Global $Progs = IniRead($ScriptDir & $ConfigINI, "Progs", "num", "")
Global $Program_[$Progs] ; Array to hold button control IDs
Global $Name_ = IniReadSection($ConfigINI, "Name")
Global $Path_ = IniReadSection($ConfigINI, "Path")
Global $i = 1oÝ÷ Ú«¨µâ#ºËpj{^.±æ¢{-j{_¢½7èúèËh©²êÞÛ-k'¶¬jg¿ªê-y8Z·l)Ý¡úZ­©èÒÊ¢}ýµÈZ¢¶+p¢}ýµÈZ·ô*'²ÝýÊÞjYr¢yrºÇºX§P1l¶z§z0û§rبËajܨ¹Ømz÷§Â¸­yúèÈZ§Ø^*§Ø^½ªâi¹^uç%j¶­ìr§)ථ¶²­çÞ­éÜzËhºÇ­íjºÚÊÇè¯Mú5©jwtߣڶ§v·¦¢ø§Ø^"xy§l¶­j··w'hæò¶$^iÔrب̨º­jëh×6#include <GUIConstants.au3>
#include <Constants.au3>
#include <array.au3>

;Set Vars
Global $ScriptDir = @ScriptDir & "GLaunch\Launcher Files"
Global $ConfigINI = $ScriptDir & "\Info Files\Config.ini"
Global $ver = IniRead($ConfigINI, "Version", "ver", "")
Global $Progs = IniRead($ConfigINI, "Progs", "num", 0)
Global $Program_[$Progs + 1] ; Array to hold button control IDs
Global $Name_ = IniReadSection($ConfigINI, "Name")
_ArrayDisplay($Name_, "Name_")
Global $Path_ = IniReadSection($ConfigINI, "Path")
_ArrayDisplay($Path_, "Path_")

;Create Window
If $Progs > 0 Then
    $i = 1 ; program number
    $s = 80 ; top of buttons
    $MainWindow = GUICreate("Game Launcher v1.0", 415, 290)
    $Tab1 = GUICtrlCreateTab(0, 0, 417, 273)
    $Programs = GUICtrlCreateTabItem("Games");===>Games Tab
    Do
        $Program_[$i] = GUICtrlCreateButton($Name_[$i][1], 237, $s, 120, 17)
        $i = $i + 1
        $s = $s + 20
    Until $i > $Progs
    GUISetState(@SW_SHOW)

    ;Launch a Program
    While 1
        $nMsg = GUIGetMsg()
        Switch $nMsg
            Case $GUI_EVENT_CLOSE
                Exit
            Case $GUI_EVENT_MINIMIZE
                GUISetState(@SW_HIDE)
            Case $Exit
                ExitLoop
        EndSwitch
        For $p = 1 To $Progs
            If $nMsg = $Program_[$p] Then
                Run($Path_[$p][1])
                Sleep(3000)
                ExitLoop
            EndIf
        Next
    WEnd
EndIf

I also threw in two _ArrayDisplays() just so you could see what the contents of those array look like.

I can't test it completely, cause I don't have your directory structure. But the GUI comes up.

:)


Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites
Mast3rpyr0

See edited post above. Heres what i got now:

#include <GUIConstants.au3>
#include <Constants.au3>

;Set Vars
Global $ScriptDir = @ScriptDir & "\GLaunch\Launcher Files"
Global $ConfigINI = "\Info Files\Config.ini"
Global $ver = IniRead($ScriptDir & $ConfigINI, "Version", "ver", "")
Dim Const $Progs = IniRead($ScriptDir & $ConfigINI, "Progs", "num", "")

    ;Create Window
    If $Progs > 0 Then
    Local $i = 1
    Local $s = 80
    $MainWindow = GUICreate("Game Launcher v1.0", 415, 290)
    $Tab1 = GUICtrlCreateTab(0, 0, 417, 273)
    $Programs = GUICtrlCreateTabItem("Games");===>Games Tab
    While $Progs >= $i
    Global $Name_[$i] = [IniRead($ScriptDir & $ConfigINI, "Name", $i, "")]
    Global $Path_[$i] = [IniRead($ScriptDir & $ConfigINI, "Path", $i, "")]
    Global $Program_[$i] = [GUICtrlCreateButton($Name_[$i], 237, $s, 120, 17)]
    $i = $i+1
    $s = $s+20
WEnd
    GUISetState(@SW_SHOW)

    ;Launch a Program
    While 1
        $nMsg = GUIGetMsg()
        Switch $nMsg
            Case $GUI_EVENT_CLOSE
                Exit
            Case $GUI_EVENT_MINIMIZE
                GUISetState(@SW_HIDE)
            Case $Exit
                ExitLoop
        EndSwitch
        For $p = 0 To UBound($Program_) - 1
            If $nMsg = $Program_[$p] Then
                Run($Path_[$p])
                Sleep(3000)
                ExitLoop
            EndIf
        Next
    WEnd
EndIf

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  

×