Jump to content

Create tray menu from INI


kpu
 Share

Recommended Posts

First, thanks for taking the time to read my post. I appreciate any help anyone can provide me.

My Goal:

I'm trying to read an ini section name, then a section called Name and create a tray menu item from the "Name" with a defined variable that can be called be the "TrayGetMsg()". My problem is that I don't know how to correctly create the variable for the menu item.

Here's what the ini file looks like. There are multiple names...client1, client2, client3...etc.

[Client1]
Name=My Client
Address= 10.10.10.10

Heres what I have for code so far:

#Include <Constants.au3>
#NoTrayIcon
Dim $strSettings = "settings.ini"
Dim $strClient, $x, $client,$strClientMenu,$StrClientName

Opt("TrayMenuMode",1) 
_GetRDPSessions()
TrayCreateItem("")
$settingsitem = TrayCreateMenu("Settings")
TrayCreateItem("")
$aboutitem = TrayCreateItem("About")
TrayCreateItem("")
$exititem = TrayCreateItem("Exit")

TraySetState()

While 1
    $msg = TrayGetMsg()
    Select
        Case $msg = 0
            ContinueLoop
        Case $msg = $aboutitem
           Exitloop
        Case $msg = $strClientMenu[$x]
           MsgBox(64,"Selection","You've selected " & $strClientMenu[$x])
        Case $msg = $exititem
            ExitLoop
    EndSelect
WEnd

Exit

Func _GetRDPSessions()
    $var = IniReadSectionNames($strSettings)
    If @error Then 
        MsgBox(4096, "", "Error occurred, probably no INI file.")
    Else
        For $i = 1 To $var[0]
        ;Read Name in section
            $strClient = IniRead($strSettings,$var[$i],"Name","")
            If $var[$i] = "Configuration" then 
                Else
            ;Create menu item
            ;MsgBox(4096, "Client", "File:" & $strSettings & @CRLF & "Clientname: " & $var[$i] & @CRLF & "Name:" & $strClient)  
                _CreateTrayMenu($strClient)
            EndIf
        Next
    EndIf   
EndFunc

Func _CreateTrayMenu($StrClient)
    For $x = 1 To $x[0][0]
        $strClientMenu[$x] = TrayCreateItem($StrClient)                 
    Next
EndFunc

Is there any good documentation somewhere that explains what the heck For $i = 1 To $var[0]. I have a basic idea, but I'm apparently missing some basics here.

Again,

Thanks for any help you can provide me!

Link to comment
Share on other sites

This works ;)

#Include <Constants.au3>
#NoTrayIcon
Global $strSettings = @ScriptDir & "\settings.ini"
Global $NumberClients = 0
Global $clientsInfo[$NumberClients + 1][2]
Global $clientsMenu[$NumberClients + 1]

_GetClientsInfo()

Opt("TrayMenuMode",1)

For $i = 1 To $clientsInfo[0][0]
    $clientsMenu[$i] = TrayCreateItem($clientsInfo[$i][0])
Next    
TrayCreateItem("")
$settingsitem = TrayCreateMenu("Settings")
TrayCreateItem("")
$aboutitem = TrayCreateItem("About")
TrayCreateItem("")
$exititem = TrayCreateItem("Exit")

TraySetState()

While 1
    $msg = TrayGetMsg()
    Select
        Case $msg = 0
            ContinueLoop
        Case $msg = $aboutitem
           Exitloop
        Case $msg = $exititem
            ExitLoop
        Case Else
            For $i = 1 To $clientsInfo[0][0]
                If $msg = $clientsMenu[$i] Then
                    MsgBox(64, "Client Nr. " & $i ,'Name: ' & $clientsInfo[$i][0] & @LF _
                    & 'Address: ' & $clientsInfo[$i][1])
                EndIf
            Next
    EndSelect
WEnd

Func _GetClientsInfo()
    Local $sectionNames = IniReadSectionNames($strSettings)
    
    ; If error...
    If @error Then
        Return MsgBox(4096, "", "Error occurred, probably no INI file.")
    EndIf
    
    For $i = 1 To $sectionNames[0]
        If StringInStr($sectionNames[$i], 'Client', 1) Then
            
            ; Read Client Info
            Local $clientNumber, $clientName, $clientAddress
            $clientNumber = StringTrimLeft($sectionNames[$i], 6)
            $clientName = IniRead($strSettings, $sectionNames[$i], 'Name', '')
            $clientAddress = IniRead($strSettings, $sectionNames[$i], 'Address', '')
            
            ; Fill Client Info Into "$clientsInfo" Array
            $NumberClients += 1
            ReDim $clientsInfo[$NumberClients + 1][2]
            ReDim $clientsMenu[$NumberClients + 1]
            $clientsInfo[0][0] = $NumberClients
            $clientsInfo[$clientNumber][0] = $clientName
            $clientsInfo[$clientNumber][1] = $clientAddress
            
        EndIf
    Next

EndFunc
Edited by tittoproject
Link to comment
Share on other sites

Is there any good documentation somewhere that explains what the heck For $i = 1 To $var[0]. I have a basic idea, but I'm apparently missing some basics here.

Its a loop, example:

For $i = 1 To 10
   Msgbox(0,"",$i)
NextoÝ÷ Ù8b³¥ÊÞj×ë,j£ajÜ"Wb²ZË]·¶)b¶­³]¢ç©l¢f¤z+]yÊÞjÇ¥G­+ºÚ"µÍÜ   ÌÍÚHHLÈHÝLBÙØÞ
    ][ÝÉ][ÝË    ÌÍÚJB^

The Step is the method used, so instead of +1 (default, doesnt need to be included) its -1

The $i is more or less just a placeholder... you can use it too

For more help look at your Autoit Help document (Start > Autoit > Autoit Help File), then select "Index" and type in For, it should give For...Next (You can also use the Search feature if your not sure what the function is)

Edited by Rad
Link to comment
Share on other sites

Little optimization:

Case Else
            For $i = 1 To $clientsInfo[0][0]
                If $msg = $clientsMenu[$i] Then
                    MsgBox(64, "Client Nr. " & $i ,'Name: ' & $clientsInfo[$i][0] & @LF _
                    & 'Address: ' & $clientsInfo[$i][1])
                    ExitLoop
                EndIf
            Next
    EndSelect

There was added ExitLoop - when you find it you needn't do rest of FOR .. NEXT

Link to comment
Share on other sites

Thanks! What you've done is way passed my comprehension...but I like it! Now I just have to dissect it to understand what you've done.

Don't worry, we're here to help you, and to learn ;)

Maybe, you'll like to be able to add or remove clients in "setting.ini". In this case you're really lucky: actually I'm writing a script really really close to your.

My script is a launcher that starts Mozilla's apps using different profiles. It reads/writes from/to "prifiles.ini", the Mozilla's configuration file, that's exactly formatted as your "settings.ini"!

Edited by tittoproject
Link to comment
Share on other sites

kpu, I'm sure to understand what you want to do...

What is "refreash.exe"? A script you wrote?

I don't like too much how you use the "If...Then" statement. Your code:

If FileExists("settings.ini") Then
    Else
        MsgBox(16, "Warning!", "Couldn't find settings.ini. File must be placed at " & @ScriptDir)
        Exit
    EndIf
Edited by tittoproject
Link to comment
Share on other sites

Hey Tittoproject

kpu, I'm sure to understand what you want to do...

What is "refreash.exe"? A script you wrote?

I've updated my code to refresh the list based upon the settings.ini file under the configurations section. (I know, I've been spelling refresh wrong... ) Maybe there is a better way to do this, but I'm closing the app and reopening it. Basically, if this works for the person I'm writing it for, I will most likely go through the code again and clean everything up. Lets just say it's a beta for now.

I coded the "IF" statements that way so if I chose to add something to it, I can easily do so. I don't see the performance difference in coded it the way you have shown.

However, thank you for your help with the ini file. If I had not gotten that from you, it may have taken me a lot longer or possibly not at all to figure out the best solution.

Thanks again for your help!

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...