1
votes

I wrote an AHK script which prompts for User Input through InputBox, navigates through a text file based on user input line by line and copy the output to clipboard.

Example:

  • If "4max" is given as UserInput, it should copy the output "sleep" to clipboard.

  • If "3ben" is given as UserInput, it should copy the output "jog" to clipboard.

Sample Text File :

max:eat:drink:sleep:play
jerry:eat:play:drink:jog
laura:drink:eat:sleep:play
ben:sleep:jog:eat:drink

Can someone please enhance the below script?

Also when I run the script with a blank Notepad open, it is pasting the current item in Clipboard to notepad. :(.

Script:

#SingleInstance, force
#Include C:\Users\mpechett\Desktop\ahk\tf.ahk

InputBox, SearchText, Search for Name

x = %SearchText%
RegExMatch(x, "(\d*)(\w*)", y)
SearchText:= % y2
ptext = % TF_Find("C:\Users\mpechett\Desktop\ahk\test.txt", "","", SearchText, 1, 1)
StringSplit, word_array, ptext, :, .

;msgBox % word_array%y1%

Clipboard = % word_array%y1%
Msgbox, %Clipboard%

Esc::ExitApp
2

2 Answers

0
votes

Okay, so I didn't fully implement your design. It looks like your Clipboard assignment is using deprecated = sign vs := which is now standard for assigning objects, this may have been an issue. I didn't test your code, decided it would be better to rewrite the entire solution, without using any outside Library.

Try:

    testdata =
    (
    max:eat:drink:sleep:play
    jerry:eat:play:drink:jog
    laura:drink:eat:sleep:play
    ben:sleep:jog:eat:drink
    )

    InputBox, SearchText, Search for Name

    MsgBox % clipboard := grepFile(SearchText, testdata)

    grepFile(userinput, file) {
        For e, v in StrSplit(userinput) {
            If v is alpha
                key .= v
            else 
                num .= v
        }

        arr := formatFile(file)

        return arr[key][num]
    }

    formatFile(file) {
        ourObj := {}
        For e, line in StrSplit(file, "`n", "`r") {
            arr := StrSplit(line, ":")
            ourObj[(arr.1)] := arr             
        }
        return ourObj
    } 

edit:

testdata =
(
max-test:eat:drink:sleep:play
jerry-test:eat:play:drink:jog
laura-test:drink:eat:sleep:play
ben-test:sleep:jog:eat:drink
)

testData := StrReplace(testData, "-test")

InputBox, SearchText, Search for Name

MsgBox % clipboard := grepFile(SearchText, testdata)

grepFile(userinput, file) {
    For e, v in StrSplit(userinput) {
        If v is alpha
            key .= v
        else 
            num .= v
    }

    arr := formatFile(file)

    return arr[key][num]
}

formatFile(file) {
    ourObj := {}
    For e, line in StrSplit(file, "`n", "`r") {
        arr := StrSplit(line, ":")
        ourObj[(arr.1)] := arr             
    }
    return ourObj
}
0
votes
#SingleInstance, force
SetTitleMatchMode, 2
SetWorkingDir, %A_ScriptDir% ; if an absolute path of the file isn't specified

; Put tf.ahk in the folder:  %A_MyDocuments%\AutoHotkey\Lib\TF-master
#Include <TF-master\tf>

F1:: ; assign a hotkey to the code (the script is persistent because of "Esc::ExitApp").
InputBox, SearchText, Search for Name
if ErrorLevel
    return
x = %SearchText%
RegExMatch(x, "(\d*)(\w*)", y)
SearchText:= % y2
ptext = % TF_Find("C:\Users\mpechett\Desktop\ahk\test.txt", "","", SearchText, 1, 1)
StringSplit, word_array, ptext, :, .

; Instead of the clipboard you can use another variable to save the output:
myVar = % word_array%y1%
; Msgbox, %myVar%

IfWinExist, Untitled - Notepad
{
    WinActivate 
    WinWaitActive
    SendInput, %myVar%
}
return

Esc::ExitApp