Sign in to follow this  
Followers 0
Iffie

Learning AutoIt, help needed.

12 posts in this topic

So, I'm stuck with my script.. Basically you enter name for example banana press "+" to add it to the list which will be stored in settings.ini after that when you press start it will open notepad and print out your list. And delete simply clears settings.ini. I haven't been able to figure out how to get multiple items in the list, the old one is always replaced. And also how to print the settings.ini into the list box when I run the script first time.

#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <GUIListBox.au3>
#include <WindowsConstants.au3>

$IniFile = "Settings.ini"
$Form1 = GUICreate("Test", 203, 363, 239, 175)
$Input = GUICtrlCreateInput("Enter Name Here", 16, 16, 129, 24)
GUICtrlSetFont(-1, 11, 400, 0, "Arial")
$Add = GUICtrlCreateButton("+", 160, 16, 25, 25, $WS_GROUP)
$List = GUICtrlCreateList("", 16, 56, 169, 253)
$Delete = GUICtrlCreateButton("Delete", 16, 328, 81, 25, $WS_GROUP)
$Start = GUICtrlCreateButton("Start", 112, 328, 73, 25, $WS_GROUP)
GUISetState(@SW_SHOW)

while 1
$msg = GUIGetMsg()
Switch $msg
Case -3
Exit
case $Add
IniWrite($IniFile, "Names", "item", GUICtrlRead($Input))
$List = GUICtrlCreateList("", 16, 56, 169, 253)
GUICtrlSetData(-1, GUICtrlRead($Input), "")
Case $Delete
IniDelete ("Settings.ini", "Names", "item")
Case $Start
$ReadList = IniRead($IniFile, "Names", "item", "ERROR")
Run("notepad.exe")
WinWaitActive("Untitled - Notepad")
Send ("This is the list: ")
Send ($ReadList)
EndSwitch
WEnd

settings.ini

[Names]
item=Banana

I was thinking of being able to click and delete one entry from the list instead of clearing it all.. once I get multiple entries on the list even working..

This has been evolving while I've learned new stuff so I'm pretty sure things could be done differently.. in a better way. Help and tips appreciated!

Share this post


Link to post
Share on other sites



This is how I like doing these.

Grab what you need :)

#include <WindowsConstants.au3>
#include <MenuConstants.au3>
#include <GuiListView.au3>
#include <Array.au3>

Global $IniFile = @ScriptDir & "\Settings.ini"

#Region - UI -

Global $Form1 = GUICreate("Test", 203, 363, 239, 175)
Global $Input = GUICtrlCreateInput("Enter Name Here", 16, 16, 129, 24)

Global $List = GUICtrlCreateListView("Name", 16, 56, 169, 253)

Global $Add = GUICtrlCreateButton("+", 160, 16, 25, 25)
Global $Delete = GUICtrlCreateButton("Delete", 16, 328, 81, 25)
Global $Start = GUICtrlCreateButton("Start", 112, 328, 73, 25)

Global $cMenu = GUICtrlCreateContextMenu($List)
Global $cDelete = GUICtrlCreateMenuItem("Delete", $cMenu)

Global $hENTER = GUICtrlCreateDummy()
Global $hDELETE = GUICtrlCreateDummy()

Global $aAcceleratorKeys[2][2] = [["{ENTER}", $hENTER], ["{DEL}", $hDELETE]]
GUISetAccelerators($aAcceleratorKeys)

GUISetState(@SW_SHOW)

DllCall("USER32.DLL", "lresult", "SendMessageW", "hwnd", GUICtrlGetHandle($List), "uint", 0x1000 + 30, "wparam", 0, "lparam", 165)

GUIRegisterMsg($WM_COMMAND, "WM_COMMAND")
GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")
GUIRegisterMsg($WM_SYSCOMMAND, "WM_SYSCOMMAND")

#EndRegion - UI -

SessionRestor()
Sleep(99999999)

#region - WM_MESSAGES -

Func WM_COMMAND($hWnd, $iMsg, $iwParam, $ilParam)
    #forceref $hwnd, $iMsg, $ilParam

    Local $Temp
    Local $LowWord = BitAND($iwParam, 0xFFFF)
    Switch $hWnd
        Case $Form1
            Switch $LowWord
                Case $Add, $hENTER
                    $Temp = GUICtrlRead($Input)
                    If $Temp Then
                        GUICtrlCreateListViewItem($Temp , $List)
                    EndIf
                    GUICtrlSetData($Input, "")
                    SaveList()

                Case $cDelete, $Delete, $hDELETE
                    GUICtrlDelete(GUICtrlRead($List))
                    SaveList()

            Case $Start
                ShowEntries()
            EndSwitch
    EndSwitch


    Return 'GUI_RUNDEFMSG'
EndFunc   ;==>WM_COMMAND

Func WM_NOTIFY($hWnd, $iMsg, $iwParam, $ilParam)
    #forceref $hWnd, $iMsg, $iwParam, $ilParam

    Switch $hWnd
        Case $Form1

    EndSwitch
    ;ConsoleWrite($hwnd & @CR)

    Return 'GUI_RUNDEFMSG'
EndFunc   ;==>WM_NOTIFY

Func WM_SYSCOMMAND($hWnd, $iMsg, $iwParam, $ilParam)
    #forceref $hWnd, $iMsg, $iwParam, $ilParam

    Switch $hWnd
        Case $Form1
            Switch BitAND($iwParam, 0xFFF0)
                Case $SC_CLOSE
                    SaveList()
                    _UI_Exit()
            EndSwitch
    EndSwitch

    Return 'GUI_RUNDEFMSG'
EndFunc   ;==>WM_SYSCOMMAND

#endregion - WM_MESSAGES -

#Region - USER -

Func SessionRestor()
    Local $Name
    For $I = 0 To 10e+100
        $Name = IniRead($IniFile, "NAMES", "Item" & $I, 0)
        If ($Name == 0) Then ExitLoop
        GUICtrlCreateListViewItem($Name , $List)
    Next
    FileDelete($IniFile)
    Return
EndFunc

Func ShowEntries()
    SaveList()
    Local $Name, $aNames[1] = [0]
    For $I = 1 To 10e+100
        $Name = IniRead($IniFile, "NAMES", "Item" & ($I-1), 0)
        If ($Name == 0) Then ExitLoop
        If (UBound($aNames)) = $I Then
            ReDim $aNames[$I+1]
        EndIf
        $aNames[0] += 1
        $aNames[$I] = $Name
    Next
    _ArrayDisplay($aNames)
    Return
EndFunc

Func SaveList()
    FileDelete($IniFile)
    Local $ItemCount = _GUICtrlListView_GetItemCount($List)
    Local $Name
    For $I = 0 To $ItemCount
        $Name = _GUICtrlListView_GetItemText($List, $I, 0)
        If Not $Name Then ContinueLoop
        IniWrite($IniFile, "NAMES", "Item" & $I, $Name)
    Next
    Return
EndFunc

#EndRegion - USER -

Func _UI_Exit()
    Exit
EndFunc

Share this post


Link to post
Share on other sites

I modified your script a bit:

#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <GUIListBox.au3>
#include <WindowsConstants.au3>

Global $IniFile = "Settings.ini"
Global $ReadList = IniRead($IniFile, "Names", "item", "")
$Form1 = GUICreate("Test", 203, 363, 239, 175)
$Input = GUICtrlCreateInput("Enter Name Here", 16, 16, 129, 24)
GUICtrlSetFont(-1, 11, 400, 0, "Arial")
$Add = GUICtrlCreateButton("+", 160, 16, 25, 25, $WS_GROUP)
$List = GUICtrlCreateList("", 16, 56, 169, 253)
GUICtrlSetData($List, $ReadList)
$Delete = GUICtrlCreateButton("Delete", 16, 328, 81, 25, $WS_GROUP)
$Start = GUICtrlCreateButton("Start", 112, 328, 73, 25, $WS_GROUP)
GUISetState(@SW_SHOW)

While 1
    $msg = GUIGetMsg()
    Switch $msg
        Case -3
            Exit
        Case $Add
            $ReadList = $ReadList & "|" & GUICtrlRead($Input)
            GUICtrlSetData($List, $ReadList)
            IniWrite($IniFile, "Names", "item", $ReadList)
        Case $Delete
            IniDelete("Settings.ini", "Names", "item")
            $ReadList = ""
            GUICtrlSetData($List, $ReadList)
        Case $Start
            $ReadList = IniRead($IniFile, "Names", "item", "ERROR")
            Run("notepad.exe")
            WinWaitActive("Untitled - Notepad")
            Send("This is the list: ")
            Send(StringReplace($ReadList, "|", @LF))
    EndSwitch
WEnd

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

This is how I like doing these.

Grab what you need :)

That's probably how I would like to be doing things as well :P

It has the small things done that I was thinking of but had no idea how to. Like highlighting the text so you can just straight away start to type instead of deleting the "Enter Name Here" and so on.

I modified your script a bit:

This one makes a bit more sense to me code wise.

Thanks for the replies. I'll be studying them.

Share this post


Link to post
Share on other sites

It has the small things done that I was thinking of but had no idea how to. Like highlighting the text so you can just straight away start to type instead of deleting the "Enter Name Here" and so on.

No, I didn't make any changes to that, that also happens with your version on my PC.

All I did was make it work with pressing keys to do stuff, like click a name and press the [DEL] key, or enter a name into the name field and pres enter to add it, and right click on a name in the listview and choose "delete" etc.

Also, I personally think it's better to use a ListView other than a list control.

Share this post


Link to post
Share on other sites

No, I didn't make any changes to that, that also happens with your version on my PC.

All I did was make it work with pressing keys to do stuff, like click a name and press the [DEL] key, or enter a name into the name field and pres enter to add it, and right click on a name in the listview and choose "delete" etc.

Also, I personally think it's better to use a ListView other than a list control.

Oh, silly me. Indeed it works with that. I should probably take a break, I've been learning new & modifying that quite a lot, it wasn't supposed to even be any kind list maker at first. I don't even know what it will be in the end. :D

Share this post


Link to post
Share on other sites

FlutterShy I'd like to see how you would do the pulling .ini entrys out of the file to notepad for example instead of the array display thingy, I tried to remake the ShowEntries function but didin't quite get it to work. Then I gave up and worked with what I had earlier & Waters improvements since it seemed easier.

I tried to get it write on notepad something like this

This is 1 line of my list: Banana

This is 2 line of my list: Cookie

This is 3 line of my list: Milk

And so on.. Soon I need to give up for today since I can just make 5 posts / day as new member it seems :P

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

Kinda like the way water did it, just send the text from a string variable instead of adding the names to an array variable.

#include <WindowsConstants.au3>
#include <MenuConstants.au3>
#include <GuiListView.au3>
#include <Array.au3>

Global $IniFile = @ScriptDir & "Settings.ini"

#Region - UI -

Global $Form1 = GUICreate("Test", 203, 363, 239, 175)
Global $Input = GUICtrlCreateInput("Enter Name Here", 16, 16, 129, 24)

Global $List = GUICtrlCreateListView("Name", 16, 56, 169, 253)

Global $Add = GUICtrlCreateButton("+", 160, 16, 25, 25)
Global $Delete = GUICtrlCreateButton("Delete", 16, 328, 81, 25)
Global $Start = GUICtrlCreateButton("Start", 112, 328, 73, 25)

Global $cMenu = GUICtrlCreateContextMenu($List)
Global $cDelete = GUICtrlCreateMenuItem("Delete", $cMenu)

Global $hENTER = GUICtrlCreateDummy()
Global $hDELETE = GUICtrlCreateDummy()

Global $aAcceleratorKeys[2][2] = [["{ENTER}", $hENTER], ["{DEL}", $hDELETE]]
GUISetAccelerators($aAcceleratorKeys)

GUISetState(@SW_SHOW)

DllCall("USER32.DLL", "lresult", "SendMessageW", "hwnd", GUICtrlGetHandle($List), "uint", 0x1000 + 30, "wparam", 0, "lparam", 165)

GUIRegisterMsg($WM_COMMAND, "WM_COMMAND")
GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")
GUIRegisterMsg($WM_SYSCOMMAND, "WM_SYSCOMMAND")

#EndRegion - UI -

SessionRestor()
Sleep(99999999)

#region - WM_MESSAGES -

Func WM_COMMAND($hWnd, $iMsg, $iwParam, $ilParam)
    #forceref $hwnd, $iMsg, $ilParam

    Local $Temp
    Local $LowWord = BitAND($iwParam, 0xFFFF)
    Switch $hWnd
        Case $Form1
            Switch $LowWord
                Case $Add, $hENTER
                    $Temp = GUICtrlRead($Input)
                    If $Temp Then
                        GUICtrlCreateListViewItem($Temp , $List)
                    EndIf
                    GUICtrlSetData($Input, "")
                    SaveList()

                Case $cDelete, $Delete, $hDELETE
                    GUICtrlDelete(GUICtrlRead($List))
                    SaveList()

            Case $Start
                ShowEntries()
            EndSwitch
    EndSwitch


    Return 'GUI_RUNDEFMSG'
EndFunc   ;==>WM_COMMAND

Func WM_NOTIFY($hWnd, $iMsg, $iwParam, $ilParam)
    #forceref $hWnd, $iMsg, $iwParam, $ilParam

    Switch $hWnd
        Case $Form1

    EndSwitch
    ;ConsoleWrite($hwnd & @CR)

    Return 'GUI_RUNDEFMSG'
EndFunc   ;==>WM_NOTIFY

Func WM_SYSCOMMAND($hWnd, $iMsg, $iwParam, $ilParam)
    #forceref $hWnd, $iMsg, $iwParam, $ilParam

    Switch $hWnd
        Case $Form1
            Switch BitAND($iwParam, 0xFFF0)
                Case $SC_CLOSE
                    SaveList()
                    _UI_Exit()
            EndSwitch
    EndSwitch

    Return 'GUI_RUNDEFMSG'
EndFunc   ;==>WM_SYSCOMMAND

#endregion - WM_MESSAGES -

#Region - USER -

Func SessionRestor()
    Local $Name
    For $I = 0 To 10e+100
        $Name = IniRead($IniFile, "NAMES", "Item" & $I, 0)
        If ($Name == 0) Then ExitLoop
        GUICtrlCreateListViewItem($Name , $List)
    Next
    FileDelete($IniFile)
    Return
EndFunc

Func ShowEntries()
    SaveList()
    Local $Name, $Names
    For $I = 1 To 10e+100
        $Name = IniRead($IniFile, "NAMES", "Item" & ($I-1), 0)
        If ($Name == 0) Then ExitLoop
        $Names &= "This is " & $I & " line of my list: " & $Name & @LF
    Next

    Run("notepad.exe")
    WinWaitActive("[CLASS:Notepad]")
    Send("This is the list:" & @CRLF)
    Send($Names, 1)

    Return
EndFunc

Func SaveList()
    FileDelete($IniFile)
    Local $ItemCount = _GUICtrlListView_GetItemCount($List)
    Local $Name
    For $I = 0 To $ItemCount
        $Name = _GUICtrlListView_GetItemText($List, $I, 0)
        If Not $Name Then ContinueLoop
        IniWrite($IniFile, "NAMES", "Item" & $I, $Name)
    Next
    Return
EndFunc

#EndRegion - USER -

Func _UI_Exit()
    Exit
EndFunc
Edited by FlutterShy

Share this post


Link to post
Share on other sites

Kinda like the way water did it, just send the text from a string variable instead of adding the names to an array variable.

That works, I did add more hotkeys like start the listing and closing it completely and other small stuff.

Func ShowEntries()
    SaveList()
    Local $Name, $Names
    For $I = 1 To 10e+100
        $Name = IniRead($IniFile, "NAMES", "Item" & ($I-1), 0)
        If ($Name == 0) Then ExitLoop
        $Names &= "This is " & $I & " line of my list: " & $Name & @LF
    Next

    Run("notepad.exe")
    WinWaitActive("[CLASS:Notepad]")
    Send("This is the list:" & @CRLF)
    Send($Names, 1)

    Return
EndFunc

I was trying to toy with that, to get some delays in it but.. without success..

I could get delay between "This is the list:" and then the listing and I could delay the characters with Opt("SendKeyDelay", 25)

But I had issues when I was trying to modify this to work like I wanted

$Names &= "This is " & $I & " (DELAY HERE) " line of my list: " & $Name & @LF

For example like that, 2s delay after when it tells which line it was

Do you need to break that into 2 parts like

$Names1 &= "This is " & $I & "

$Names2 &= " line of my list: " & $Name & @LF

And then

Run("notepad.exe")
WinWaitActive("[CLASS:Notepad]")
Send("This is the list:" & @CRLF)
Send($Names1, 1)
Sleep (2000)
Send($Names2, 1)

And then change the loop or is there simpler way which I've missed? This was last post until evening. :(

Share this post


Link to post
Share on other sites

Use the first example I posted, you should break the strings into an array and add sleep calls as needed, very simple.

Share this post


Link to post
Share on other sites

I got it to work, I guess I was just over thinking it at first. I can't think of improvements so it's time to create new more or less useless thingy :P

Thanks for your help.

Share this post


Link to post
Share on other sites

That's my favorite way to learn things :P

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