Jump to content

Reading strings from an Ini-file and listing them


Recommended Posts

Hi there,

I made a script together with some great help from this community. What is does is, sit in the tray until ctrl+1 is pressed, it will ask for input (in this case a short word that contains a longer answer in the ini file it will paste, for example when answering outlook mails with default answers) now the list will become longer and longer, and learning the strings by head will get harder so I was looking into listing the search terms somewhere in the program but can't really find code to borrow for it on these forums. I was wondering if anyone here would be able to help me with it. What I'm looking for is some "bar" attached to the search bar, or some pulldown menu that lists the options that would be the most convenient view. I've attached my script here.

#include <Misc.au3>

HotKeySet("^1", "search")
$zoek = "" ; if its going to be a string, its good practice to declare it as a string

While 1
    Sleep(1)
WEnd

Func _test()
    Local $sIni = @ScriptDir & "\antwoorden.ini"
    Local $sAnswer = ""

    $sAnswer = IniRead($sIni, "answers", $zoek, "Sorry No answer available")
   
 If StringInStr($sAnswer,"<enter>") Then
        $aAnswer = StringSplit($sAnswer,"<enter>",1) ; 1 = entire delimiter string is needed to mark the split
        $sAnswer = ""
        For $i = 1 To $aAnswer[0]
            $sAnswer &= $aAnswer[$i] & @CR 
        Next
    EndIf
            _Sendex ($sAnswer)
        
    $zoek = "" ;Reset string
EndFunc   ;==>_test

Func search()
    $zoek &= InputBox("Zoeken", "Zoekterm:")
    _test()
EndFunc   ;==>search

Func _SendEx($ss)
    Local $iT = TimerInit()
    While _IsPressed("10") Or _IsPressed("11") Or _IsPressed("12")
        If TimerDiff($iT) > 2000 Then
            MsgBox(262144, "Warning", "Shift, Ctrl and Alt keys need to be released to proceed!")
            $iT = TimerInit() ; reset the timer
        EndIf
    WEnd
    Send($ss)
    $zoek = "" ;Reset string
EndFunc   ;==>_SendEx
[answers]
password=answer rule 1<enter>rule 2
abuse=answer rule 1<enter>rule 2
VPN=answer rule 1<enter>rule 2<enter>rule 3

 

antwoorden.au3 antwoorden.ini

Link to comment
Share on other sites

something like this ? :

#include <WindowsConstants.au3>
#include <guiconstantsEx.au3>
#include <GuiEdit.au3>

Global $sIni = @ScriptDir & "\antwoorden.ini"

ConsoleWrite(InputGui("Test input", "Please enter a search term:" & @CRLF & "Available terms:" & @CRLF & @CRLF, 180, 130 ))

Func InputGui($titletxt = "", $TextMSG = "", $w = 300, $h = 120)
    ;Modified source from https://www.autoitscript.com/forum/topic/90035-check-for-valid-file-name-while-entering-text/
    ;by dan_555
    ;Cancel or X will always return an empty string and the @error will be set to 1
    ; Invalid chars are: ['<', '>', '|', '"', '\', '/', ':', '*', '?']
    If $w < 85 Then $w = 85
    If $h < 85 Then $h = 85

    Local $MyGui = GUICreate($titletxt, $w, $h, -1, -1, $WS_POPUP + $WS_CAPTION)
    Local $Edit = GUICtrlCreateEdit($TextMSG, 4, 4, $w - 8, $h - 55, BitOR($ES_AUTOVSCROLL, $ES_READONLY, $WS_VSCROLL))
    Local $input = GUICtrlCreateInput("", 4, $h - 50, $w - 8, 20, $ES_WANTRETURN)
    Local $buttonOK = GUICtrlCreateButton("OK", 4, $h - 25, 35, 20)
    Local $buttonCA = GUICtrlCreateButton("Cancel", $w - 45, $h - 25, 40, 20)
    GUISetState(@SW_SHOW)

    Local $final, $tmp, $itxt, $itxtold, $Err = 0
    Local $WinPos

    While 1
        $WinPos = WinGetPos($MyGui)
        $msg = GUIGetMsg()
        Select
            Case $msg = $GUI_EVENT_CLOSE Or $msg = $buttonCA
                $Err = 1
                ExitLoop
            Case $msg = $buttonOK
                ExitLoop
        EndSelect
        $itxt = GUICtrlRead($input)

        If $itxt <> $itxtold Then
            $itxtold = $itxt
            $tmp = ReadIniAnswers($itxt)

            _GUICtrlEdit_BeginUpdate($Edit)
            GUICtrlSetData($Edit, "", 1)
            GUICtrlSetData($Edit, $TextMSG & $tmp)
            _GUICtrlEdit_EndUpdate($Edit)
        EndIf

        Sleep(10)
    WEnd
    If $Err = 0 Then
        $final = GUICtrlRead($input)
        GUIDelete($MyGui)
        Return $final
    Else
        GUIDelete($MyGui)
        SetError(1)
        Return ""
    EndIf
EndFunc   ;==>InputGui

Func ReadIniAnswers($text)
    Local $array = IniReadSection($sIni, "answers")
    Local $list = ""
    Local $x, $y
    For $x = 1 To $array[0][0]
        For $y = StringLen($text) To 1 Step -1
            If StringLeft($array[$x][0], $y) = $text Then
                $list = $list & $array[$x][0] & @CRLF
                ExitLoop
            EndIf
        Next
    Next
    Return $list
EndFunc   ;==>ReadIniAnswers

 

Edited by Dan_555
Fixing some misstakes

Some of my script sourcecode

Link to comment
Share on other sites

Hey Dan, thanks for your quick reply, the code gives me an error.

ConsoleWrite(InputGui("Test input", "Please enter a search term:" & @CRLF & "Available terms:" & @CRLF, 180, 280, 0) & @CRLF)
ConsoleWrite(^ ERROR
>Exit code: 1    Time: 0.4334

 

Any ideas?

 

Link to comment
Share on other sites

Wow Dan, that's amazing work! I'm amazed by how fast you got that working. I love the fact it's listing while you type so if the "knowledgebase" is growing, you won't run into problems with the amount of found terms. You're the best, appreciate this a lot!

the only thing missing now is the "ctrl+1" hotkey command to bring up the screen

Edited by barkeeper
Link to comment
Share on other sites

You should be able to put it into your script without any problems, i guess !

just replace this

Func search()
    $zoek &= InputBox("seek", "seekterm:")
    _test()
EndFunc   ;==>search

with 

Func search()
    $zoek &= InputGui("Test input", "Please enter a search term:" & @CRLF & "Available terms:" & @CRLF & @CRLF, 180, 130 )
    _test()
EndFunc   ;==>search

Edit: Removed #include <array line from the script, and added  $WS_VSCROLL to the edit field options.

Edited by Dan_555

Some of my script sourcecode

Link to comment
Share on other sites

Dan,

Implemented that, as you said, appears to be working well, there's just a final thing (I guess) that doesn't work:

Pressing enter (or clicking OK) after typing a valid search term doesn't execute the pasting of the string into the field you've selected (Outlook new mail message for example)

antwoorden2.0.au3

Link to comment
Share on other sites

#include <Misc.au3>
#include <WindowsConstants.au3>
#include <guiconstantsEx.au3>
#include <GuiEdit.au3>

HotKeySet("^1", "search")

$zoek = "" ; if its going to be a string, its good practice to declare it as a string

Global $sIni = @ScriptDir & "\antwoorden.ini"

While 1
    Sleep(1)
WEnd

Func _test()

    Local $sAnswer = ""

    $sAnswer = IniRead($sIni, "answers", $zoek, "Sorry No answer available")

 If StringInStr($sAnswer,"<enter>") Then
        $aAnswer = StringSplit($sAnswer,"<enter>",1) ; 1 = entire delimiter string is needed to mark the split
        $sAnswer = ""
        For $i = 1 To $aAnswer[0]
            $sAnswer &= $aAnswer[$i] & @CR
        Next
    EndIf
            _Sendex ($sAnswer)

    $zoek = "" ;Reset string
EndFunc   ;==>_test

Func search()
    $zoek &= InputGui("Test input", "Please enter a search term:" & @CRLF & "Available terms:" & @CRLF & @CRLF, 180, 130 )
    _test()
EndFunc   ;==>search

Func _SendEx($ss)
    Local $iT = TimerInit()
    While _IsPressed("10") Or _IsPressed("11") Or _IsPressed("12")
        If TimerDiff($iT) > 2000 Then
            MsgBox(262144, "Warning", "Shift, Ctrl and Alt keys need to be released to proceed!")
            $iT = TimerInit() ; reset the timer
        EndIf
    WEnd
    Send($ss)
    $zoek = "" ;Reset string
EndFunc   ;==>_SendEx

Func InputGui($titletxt = "", $TextMSG = "", $w = 300, $h = 120)
    ;Modified source from https://www.autoitscript.com/forum/topic/90035-check-for-valid-file-name-while-entering-text/
    ;by dan_555
    ;Cancel or X will always return an empty string and the @error will be set to 1
    ; Invalid chars are: ['<', '>', '|', '"', '\', '/', ':', '*', '?']
    If $w < 85 Then $w = 85
    If $h < 85 Then $h = 85

    Local $MyGui = GUICreate($titletxt, $w, $h, -1, -1, $WS_POPUP + $WS_CAPTION)
    Local $Edit = GUICtrlCreateEdit($TextMSG, 4, 4, $w - 8, $h - 55, BitOR($ES_AUTOVSCROLL, $ES_READONLY, $WS_VSCROLL))
    Local $input = GUICtrlCreateInput("", 4, $h - 50, $w - 8, 20, $ES_WANTRETURN)
    Local $buttonOK = GUICtrlCreateButton("OK", 4, $h - 25, 35, 20)
    Local $buttonCA = GUICtrlCreateButton("Cancel", $w - 45, $h - 25, 40, 20)
    GUISetState(@SW_SHOW)

    Local $final, $tmp, $itxt, $itxtold, $Err = 0
    Local $WinPos

    While 1
        $WinPos = WinGetPos($MyGui)
        $msg = GUIGetMsg()
        Select
            Case $msg = $GUI_EVENT_CLOSE Or $msg = $buttonCA
                $Err = 1
                ExitLoop
            Case $msg = $buttonOK Or _IsPressed("0D")
                ExitLoop
        EndSelect
        $itxt = GUICtrlRead($input)

        If $itxt <> $itxtold Then
            $itxtold = $itxt
            $tmp = ReadIniAnswers($itxt)

            _GUICtrlEdit_BeginUpdate($Edit)
            GUICtrlSetData($Edit, "", 1)
            GUICtrlSetData($Edit, $TextMSG & $tmp)
            _GUICtrlEdit_EndUpdate($Edit)
        EndIf

        Sleep(10)
    WEnd
    If $Err = 0 Then
        $final = GUICtrlRead($input)
        GUIDelete($MyGui)
        Return $final
    Else
        GUIDelete($MyGui)
        SetError(1)
        Return ""
    EndIf
EndFunc   ;==>InputGui

Func ReadIniAnswers($text)
    Local $array = IniReadSection($sIni, "answers")
    Local $list = ""
    Local $x, $y
    For $x = 1 To $array[0][0]
        For $y = StringLen($text) To 1 Step -1
            If StringLeft($array[$x][0], $y) = $text Then
                $list = $list & $array[$x][0] & @CRLF
                ExitLoop
            EndIf
        Next
    Next
    Return $list
EndFunc   ;==>ReadIniAnswers

Atm the gui is not responding to the enter key, but i'm sure it can be done so.

Done.

Edited by Dan_555
Added enter key to the code.

Some of my script sourcecode

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

×
×
  • Create New...