Jump to content

Basic script help - (Moved)


Go to solution Solved by Skeletor,

Recommended Posts

Can someone help me with some gentle pointers here. This is not for work or school, but is my own little exercise to understand how this works. I can't really find an example that helps me 'get' this and was wondering if someone would be willing to help with a simple scenario to try and teach myself the early concepts to use in similar situations in the future. I'm sure others on here could benefit too...

If I have a text file with the name test.txt and it has the following contents....

Yellow line "Round"
Blue line "Square"
Green line "Oblong"
Orange line "Triangle"

I also have a GUI with a drop down for Yellow, blue, green or orange and then a text input line and a button to submit.
I'm trying to write a script which simply looks at the text file and if I select the 'yellow' line in the drop down on the GUI it would replace the text in-between the quotes with whatever was specified in the GUI while leaving the rest untouched.
I've got the GUI part down, but I'm really struggling with the interaction with the text file to change the data between the quotes. I keep wiping whatever is there rather than replacing certain bits.
 

Link to comment
Share on other sites

  • Moderators

Moved to the appropriate AutoIt General Help and Support forum, as the Developer General Discussion forum very clearly states:

Quote

General development and scripting discussions.


Do not create AutoIt-related topics here, use the AutoIt General Help and Support or AutoIt Technical Discussion forums.

Moderation Team

"Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball

How to get your question answered on this forum!

Link to comment
Share on other sites

  • Solution

Creating the GUI can be done with a GuiBuilder or Koda (Koda is already bundled with AutoIt) or you can manually do this if it's a small program like this.
Basic coding is reading contents  (GuiCtrlRead) from the predefined combo box using GUICtrlSetData to add the data to the list.

Below example it from the Help File (F1)

; Create a combobox control.
Local $idComboBox = GUICtrlCreateCombo("Item 1", 10, 10, 185, 20)

; Add additional items to the combobox.
 GUICtrlSetData($idComboBox, "Item 2|Item 3", "Item 2")


The hard part is the StringRegExpReplace. You can check the help file which contains examples to understand this.
I still don't understand regular expressions that much but you can get some help here.

 

#include <GUIConstantsEx.au3>
#include <FileConstants.au3>
#include <MsgBoxConstants.au3>

Global $sFilePath = @ScriptDir & "\test.txt"

Local $hGUI = GUICreate("Text File Editor", 300, 200)
Local $idDropdown = GUICtrlCreateCombo("", 10, 10, 120, 25, BitOR(0x0003, 0x0040))
GUICtrlSetData($idDropdown, "Yellow|Blue|Green|Orange")
Local $idInput = GUICtrlCreateInput("", 10, 40, 200, 25)
Local $idButton = GUICtrlCreateButton("Submit", 220, 40, 70, 25)
GUISetState(@SW_SHOW)

While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            ExitLoop
        Case $idButton
            _EditFileContent()
    EndSwitch
WEnd

Func _EditFileContent()
    Local $sSelectedColor = GUICtrlRead($idDropdown)
    Local $sNewValue = GUICtrlRead($idInput)

    Local $sContent = FileRead($sFilePath)
    If @error Then
        MsgBox($MB_OK + $MB_ICONERROR, "Error", "Failed to read the file content.")
        Return
    EndIf

    Switch $sSelectedColor
        Case "Yellow"
            $sContent = StringRegExpReplace($sContent, 'Yellow line "(.*?)"', 'Yellow line "' & $sNewValue & '"')
        Case "Blue"
            $sContent = StringRegExpReplace($sContent, 'Blue line "(.*?)"', 'Blue line "' & $sNewValue & '"')
        Case "Green"
            $sContent = StringRegExpReplace($sContent, 'Green line "(.*?)"', 'Green line "' & $sNewValue & '"')
        Case "Orange"
            $sContent = StringRegExpReplace($sContent, 'Orange line "(.*?)"', 'Orange line "' & $sNewValue & '"')
    EndSwitch

    FileDelete($sFilePath)
    If Not FileWrite($sFilePath, $sContent) Then
        MsgBox($MB_OK + $MB_ICONERROR, "Error", "Failed to write the updated content to the file.")
    Else
        MsgBox($MB_OK, "Success", "File content updated successfully.")
    EndIf
EndFunc

 

Kind Regards
Skeletor

"Coffee: my defense against going postal."

Microsoft Office Splash Screen | Basic Notepad Program (Beginner) | Transparent Splash Screen | Full Screen UI

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