Search code examples
user-interfacecomboboxtext-filesautohotkeysubmit-button

Autohotkey storing combobox variable as a text file name


First I was able to create a combobox from a list I have on my computer. Then I tried to select an item from the combobox, store it as a variable, create a text file with that variable name, and append clipboard contents to the file. My script will do this but only with the very last selection in the combobox. I want it to work with any selected item. Please help me.

Fileread, List, %A_ScriptDir%\My List.txt
Sort, List
Gui, Add, Button, x425 y2 w40 h30 , Write
Gui +Delimiter`n
Gui, Add, combobox, x10 y36 w500 h200 vVar, %list%
Gui, Add, Text, x10 y3 w300 h30 , Select one of  the file names from the dropdown list. Then press "Write" to paste clipboard contents to a new or existing text file.
Gui, Show, AutoSize, ASR Field Information
Return

ButtonWrite:
gui, submit, nohide
sleep 100
msgbox Text file will be named: %VAR%.txt
FILEAPPEND,
(
%clipboard%
), %A_ScriptDir%\%var%.txt
sleep 1000
run %A_ScriptDir%\var.txt
return

Solution

  • Your script seems to work as you intended after I fixed what appear to be a couple minor bugs:

    Fixes:

    ...
    var := trim(var, " `t`r`n")                   ; trim trailing LF/CR
    msgbox Text file will be named: %VAR%.txt
    ...
    run %A_ScriptDir%\%var%.txt                   ; added %'s to expand var
    ...
    

    My List.txt

    Alpha
    Beta
    Delta
    Gamma
    

    I copied text to the clipboard, reloaded AutoHotKey to run your code, selected "Alpha" from the dropdown (the first line in my file), then pressed the Write button. MsgBox says "Text file will be named: Alpha.txt" then Alpha.txt opens with my clipboard text appended to the end