0
votes

I am at my wit's end trying to figure out what's wrong with my program.

It's an AutoHotkey v1.1.19.02 script designed to do the following: When you press Win+V, read a file from disk into the clipboard and then paste it into a text field. It has a simple GUI with a drop-down list to select which file should be read. It also has a checkbox which, when selected, will advance to the next file every time the hotkey is pressed.

So you should be able to press Win+V over and over again, and the script will paste in one file at a time. The dropdown list informs you which file is next and lets you pick a different one if you like.

I have the functionality for reading the file and pasting it in, but I can't get the dropdown list to update based on which file is selected. Here's my script:

SendMode Play
SetWorkingDir, C:\files

global FileIndex
global AutoAdvance = 1
global File1 := "folder 1\file1.html"
global File2 := "folder 1\file2.html"
global File3 := "folder 1\file3.html"
global File4 := "folder 2\file1.html"
global File5 := "folder 2\file2.html"
global File6 := "folder 2\file3.html"
; etc

Gui, New
Gui, Add, DropDownList, vFileDropdown gGuiSubmit w250 AltSubmit HwndFileDropdownHwnd
Gui, Add, Checkbox, vAutoAdvance gGuiSubmit checked, Auto-advance
Loop, 6
{
    GuiControl, , FileDropdown, % File%A_Index%
    ;TrayTip, PortalPaste, %A_LoopFileName%
}
GuiControl, Choose, FileDropdown, %File1%
Gui, Show,, PortalPaste

GuiSubmit:
    Gui, Submit, NoHide
    FileIndex := FileDropdown
    Gosub, GuiUpdate
    ;TrayTip, PortalPaste, Auto-advance: %AutoAdvance%. FileIndex: %FileIndex%. File name: %FileName%
return

#q::
GuiUpdate:
    FileN := File%FileIndex%
    TrayTip, PortalPaste, File5: %File5%`nFileN: %FileN%
    GuiControl, ChooseString, ComboBox1, %FileN%
return

TrayTip, PortalPaste, Assistant ready. File dir: %A_WorkingDir%

>!r::Reload
>!e::Edit

#v::
    Send, ^a ; select all
    FileName := File%FileIndex%
    FileRead, Clipboard, %FileName%
    Send, ^v
    if (AutoAdvance) {
        FileIndex++
    }
    Gosub, GuiUpdate
return

I've isolated the problem to the GuiUpdate subroutine:

GuiUpdate:
    FileN := File%FileIndex%
    TrayTip, PortalPaste, File5: %File5%`nFileN: %FileN%
    GuiControl, ChooseString, ComboBox1, %FileN% ; this line is the problem
return

I've used TrayTip to verify that my variables have the values I intend at that point. If I edit that line to refer to %File5% instead of %FileN%, GuiControl will set the ComboBox to entry #5. But as long as I have %FileN% on that line, GuiControl does nothing--even when the variables FileN and File5 have identical contents!

I even put these lines into GuiUpdate: to verify that the variables are equal.

truth := FileN == File5
TrayTip, PortalPaste, File5: %File5%`nFileN: %FileN%`nThe two are equal: %truth%

When FileIndex is 5, truth evaluates to 1 in the tooltip. How can GuiControl, ChooseString, ComboBox1, %File5% work and GuiControl, ChooseString, ComboBox1, %FileN% not, when File5 == FileN?

1

1 Answers

0
votes

MJs of the AHKscript.org forums answered my question. GuiControl wasn't targeting the right control because I created the window with Gui, New. Removing Gui, New fixed the problem.