Need help storing user input and calling it back later. Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
thrawn5499
Posts: 74
Joined: 26 Sep 2017, 15:36

Need help storing user input and calling it back later.

29 Jul 2021, 09:14

I am writing a script for my work. what it does is launches a program, then sets some options in that program, then opens files in the program, searches for tables and exports them as .csv files. I currently have it working, but it needs some clean up, and I need to add more capabilities to it.
For now I got it to do most of the first tasks using imagesearch. I did this just to get it into testing stages quickly. I wont want the final version to rely on the imagesearch because I wont have the image of those file names. Instead what I need it to do is start a GUI with a multi line input, ask for the files names, and store those names for later use. then instead of using the image of the file it will go through and get the name from the user input. it will run the program then go to the next file name from the user input. I want it to continue until it has opened all files the user put into the box. Some times it might be 3, sometimes it might be 15.

I have a stand alone GUI already with a multi line input for testing outside of the main script. I got it from stackexchange so it is not my work. I think it is working correctly though, except I will need to store the data it collects and get it back later. here is that GUI.

Code: Select all

f1::
MsgBox % MultiLineInputBox("Please Enter the file names you want to export tables from.", "", "Eclypse Drawing Table Export Utility")
return

MultiLineInputBox(Text:="", Default:="", Caption:="Multi Line Input Box"){
    static
    ButtonOK:=ButtonCancel:= false
    if !MultiLineInputBoxGui{
        Gui, MultiLineInputBox: add, Text, r1 w600  , % Text
        Gui, MultiLineInputBox: add, Edit, r10 w600 vMultiLineInputBox, % Default
        Gui, MultiLineInputBox: add, Button, w60 gMultiLineInputBoxOK , &OK
        Gui, MultiLineInputBox: add, Button, w60 x+10 gMultiLineInputBoxCancel, &Cancel
        MultiLineInputBoxGui := true
    }
    GuiControl,MultiLineInputBox:, MultiLineInputBox, % Default
    Gui, MultiLineInputBox: Show,, % Caption
    SendMessage, 0xB1, 0, -1, Edit1, A
    while !(ButtonOK||ButtonCancel)
        continue
    if ButtonCancel
        return
    Gui, MultiLineInputBox: Submit, NoHide
    Gui, MultiLineInputBox: Cancel
    return MultiLineInputBox
    ;----------------------
    MultiLineInputBoxOK:
    ButtonOK:= true
    return
    ;---------------------- 
    MultiLineInputBoxGuiEscape:
    MultiLineInputBoxCancel:
    ButtonCancel:= true
    Gui, MultiLineInputBox: Cancel
    return
}
Here is a section of code from the main script. I have a few problems and some cleaning up to do still, but it does run.

Code: Select all

Mousemove, 1240, 262
sleep 500
MouseClick, left
sleep 200
MouseClick, left
;WinWait Creo Parametric ahk_class HCS16139P ahk_exe xtop.exe
;winactivate, Creo Parametric
;winwaitactive,
;Mousemove, 124, 26
sleep 15000
winactivate, Creo Parametric
	;return

SetDirectory:
{
ImageSearch, imageX, imageY, 0, 0, %a_ScreenWidth%, %A_ScreenHeight%, *24 C:\Users\smoore\Desktop\select directory.png
If Errorlevel = 0
{
EnvAdd, Found, 1
MouseClick, left, %imageX%, %imageY%
Sleep 500
}

{
ImageSearch, imageX, imageY, 0, 0, %a_ScreenWidth%, %A_ScreenHeight%, *24 C:\Users\smoore\Desktop\c.png
If Errorlevel = 0
{
EnvAdd, Found, 1
MouseClick, left, %imageX%, %imageY%
Sleep 500
}
}
{
ImageSearch, imageX, imageY, 0, 0, %a_ScreenWidth%, %A_ScreenHeight%, *24 C:\Users\smoore\Desktop\working directory.png
If Errorlevel = 0
{
EnvAdd, Found, 1
MouseClick, left, %imageX%, %imageY%
Sleep 500
}
}
{
ImageSearch, imageX, imageY, 0, 0, %a_ScreenWidth%, %A_ScreenHeight%, *24 C:\Users\smoore\Desktop\ok.png
If Errorlevel = 0
{
EnvAdd, Found, 1
MouseClick, left, %imageX%, %imageY%
Sleep 500
}
}
}
gosub, openfile
return


openfile:

{
ImageSearch, imageX, imageY, 0, 0, %a_ScreenWidth%, %A_ScreenHeight%, *24 C:\Users\smoore\Desktop\open.png
If Errorlevel = 0
{
EnvAdd, Found, 1
MouseClick, left, %imageX%, %imageY%
Sleep 500
}
send 4055as4601_c.drw
;{
;ImageSearch, imageX, imageY, 0, 0, %a_ScreenWidth%, %A_ScreenHeight%, *24 C:\Users\smoore\Desktop\as4601.png
;If Errorlevel = 0
;{
;EnvAdd, Found, 1
;MouseClick, left, %imageX%, %imageY%
Sleep 500
;}
;}
{
ImageSearch, imageX, imageY, 0, 0, %a_ScreenWidth%, %A_ScreenHeight%, *24 C:\Users\smoore\Desktop\open-1.png
If Errorlevel = 0
{
EnvAdd, Found, 1
MouseClick, left, %imageX%, %imageY%
Sleep 500
}
}
}
gosub, savepl
return
Ok first off on the above script. I was trying to launch the program from the desktop. it has to be launched from the desktop to work correctly. I would prefer to do something besides clicking a mouse location to start it. I also was trying to get it to wait for the window to be active, but that wasn't working, so I put a 20 second sleep timer. I would like to fix this. after launching it does some actions using imagesearch. these can stay imagesearch, but it could also be done another way. then calls out gosub, openfile this is where I need to start getting data from the user input. currently it has.

Code: Select all

openfile:

{
ImageSearch, imageX, imageY, 0, 0, %a_ScreenWidth%, %A_ScreenHeight%, *24 C:\Users\smoore\Desktop\open.png
If Errorlevel = 0
{
EnvAdd, Found, 1
MouseClick, left, %imageX%, %imageY%
Sleep 500
}
send 4055as4601_c.drw
;{
;ImageSearch, imageX, imageY, 0, 0, %a_ScreenWidth%, %A_ScreenHeight%, *24 C:\Users\smoore\Desktop\as4601.png
;If Errorlevel = 0
;{
;EnvAdd, Found, 1
;MouseClick, left, %imageX%, %imageY%
I already commented out the one imagesearch and replaced it with a "send" followed by a text string. what I need to do is replace this with the first filename captured from the user input gui and place it here. and I will need to also place it in a few options later in the code (see below). then once all of the tables are exported I will need to call out the fileopen: again and have it repeat using the next file name from the user input gui.

I mentioned that I needed to also use that data elsewhere. so when I am saving the tables as .csv files, right now it is working because I am using
send 4055AS4061_wl1 then the next one I say send 4055AS4061_wl2 ect. what I need to do is replace "4055AS4061" with what ever file name it is working on at the time, and I would prefer to change the "wl1" "wl2" ect... input to have the number at the end increase by 1 each time it is saved. i think this would be cleaner. I think I remember how to do that and I think I can clean up most of this, but I do need help with the getting the data from the user, storing it for later use then recalling it when needed. I have done some of this a few years ago, but I have not used AHK for for a few years, and even when I was using it a lot, I was not really good with it.

Thanks in advance for the help. I am open to ideas and suggestions for doing this.
Last edited by thrawn5499 on 29 Jul 2021, 11:32, edited 1 time in total.
User avatar
mikeyww
Posts: 26437
Joined: 09 Sep 2014, 18:38

Re: Need help storing user input and calling it back later.

29 Jul 2021, 09:48

IniWrite is convenient for saving data that you can later retrieve.

WinWaitActive will wait for a specified window to become active.

To run a script from the desktop with a Windows hotkey, you could set up a file shortcut to your script, and then assign the shortcut file to a Windows shortcut key.
thrawn5499
Posts: 74
Joined: 26 Sep 2017, 15:36

Re: Need help storing user input and calling it back later.

29 Jul 2021, 10:23

mikeyww wrote:
29 Jul 2021, 09:48
IniWrite is convenient for saving data that you can later retrieve.

WinWaitActive will wait for a specified window to become active.

To run a script from the desktop with a Windows hotkey, you could set up a file shortcut to your script, and then assign the shortcut file to a Windows shortcut key.
OMG I am so stupid. I already have a hotkey assigned to launch that app from the desktop! I didn't even think about doing it that way.
I did try the WinWaitActive but don't think it was working. I set it to pop up a msgbox when it became active and a different message box after 30 seconds if it wasn't detected. I never got it to show this first box only the one after it wasn't detected....
User avatar
mikeyww
Posts: 26437
Joined: 09 Sep 2014, 18:38

Re: Need help storing user input and calling it back later.

29 Jul 2021, 10:42

Here is a demo (top of script). The timer simply delays running Notepad as part of the demonstration. You can change the interval to -3000 to see the failure.

Code: Select all

SetTimer, Notes, -1000 ; SetTimer, Notes, -3000
WinWaitActive, ahk_exe notepad.exe,, 2
If ErrorLevel
     MsgBox, 48, Failure, No window!
Else MsgBox, 64, Success, It worked!
Return
Notes:
Run, notepad
Return
thrawn5499
Posts: 74
Joined: 26 Sep 2017, 15:36

Re: Need help storing user input and calling it back later.

29 Jul 2021, 10:45

that is almost exactly what I used but I never got it to detect that the program I chose was open and make active. I replaced notpad.exe with Creo Parametric. also tried ahk_class HCS16139P, and ahk_exe xtop.exe. but it didn't work with any of them.
User avatar
mikeyww
Posts: 26437
Joined: 09 Sep 2014, 18:38

Re: Need help storing user input and calling it back later.

29 Jul 2021, 10:49

Feel free to post your revised script. Running Window Spy will show you the complete WinTitle.
thrawn5499
Posts: 74
Joined: 26 Sep 2017, 15:36

Re: Need help storing user input and calling it back later.

29 Jul 2021, 10:53

this is what I get from window spy
Creo Parametric
ahk_class HCS16139P
ahk_exe xtop.exe
ahk_pid 55744

I tried this code.


SetTimer, Notes, -1000 ; SetTimer, Notes, -3000
WinWaitActive, ahk_exe Creo Parametric.exe,, 2
If ErrorLevel
MsgBox, 48, Failure, No window!
Else MsgBox, 64, Success, It worked!
Return
Notes:
;Run, notepad
Return



I also tried without the .exe, and all of the other things from window spy. I always get the no window message.
thrawn5499
Posts: 74
Joined: 26 Sep 2017, 15:36

Re: Need help storing user input and calling it back later.

29 Jul 2021, 10:57

ok I tried it again with a fresh copy of Creo open. and added a winactivate, Creo Parametric above the WinWaitActive, ahk_exe xtop.exe
it works now. thanks
thrawn5499
Posts: 74
Joined: 26 Sep 2017, 15:36

Re: Need help storing user input and calling it back later.

29 Jul 2021, 11:11

I had another Idea, instead of asking the user to type out the file names, I could use something like this. (found it in my old code samples I have used in the past)

Code: Select all

#NoEnv
#SingleInstance Force
SetWorkingDir %A_ScriptDir%
Gui, Add, Button, Section, Set Directory 
Gui, Add, Edit, ys w178 vDir ReadOnly, 
Gui, Add, Text, xm, Pictures 
Gui, Add, ListBox, xm w260 r33 vPixList gPixList, 
Gui, Add, Picture, vPicImage ym, 
Gui, Show, , Picture Viewer 
return 

PixList: 
  Gui, Submit
  GuiControl,1:,PicImage,
  GuiControl,1:,PicImage, %currDir%\%PixList%
  GuiControlGet, PicImage, 1:Pos
  If PicImageW > 550
      GuiControl, 1:Move, PicImage, % "w550 h" . PicImageH*550/PicImageW
  GuiControlGet, PicImage, 1:Pos
  If PicImageH > 480
     GuiControl, 1:Move, PicImage, % "w" . PicImageW*480/PicImageH . " h480"
  Gui, Show, AutoSize
return 

ButtonSetDirectory: 
  FileSelectFolder currDir 
  GuiControl,,Dir, %currDir% 
  Loop, %currDir%\*.* 
    { 
      SplitPath, A_LoopFileFullPath,OutFileName , , OutExtension
     If OutExtension in jpg,JPG,gif,GIF,png,PNG,bmp,BMP,tif,TIF,tiff,TIFF,jpeg,JPEG
          GuiControl,1:,PixList, %OutFileName%
    }
Return 

GuiClose: 
ExitApp
[Mod edit: c-tags replaced with [code][/code] tags.]

I would need to modify it to instead of show the file when you click on it to store the names of the files so they can be used later. not sure how to do that though
gregster
Posts: 8885
Joined: 30 Sep 2013, 06:48

Re: Need help storing user input and calling it back later.

29 Jul 2021, 11:16

Just as a side note re forum use: It's better to post longer code between [code][/code] tags (instead of the c-tags for inline code you are currently using).
It's the 5th button from the left in the full editor (says either 'code' or '</>'). Thank you!
thrawn5499
Posts: 74
Joined: 26 Sep 2017, 15:36

Re: Need help storing user input and calling it back later.

29 Jul 2021, 11:30

thanks I looked for it and didn't see it. forgot were it was. thanks!
User avatar
mikeyww
Posts: 26437
Joined: 09 Sep 2014, 18:38

Re: Need help storing user input and calling it back later.

29 Jul 2021, 11:47

The IniWrite page that I linked earlier has an example at the bottom.
thrawn5499
Posts: 74
Joined: 26 Sep 2017, 15:36

Re: Need help storing user input and calling it back later.

29 Jul 2021, 12:21

yeah I saw it, not smart enough I guess to make it work
User avatar
mikeyww
Posts: 26437
Joined: 09 Sep 2014, 18:38

Re: Need help storing user input and calling it back later.

29 Jul 2021, 12:37

Below is an idea to get you started.

Code: Select all

image1 = ....png ; Full path to image file
ini   := StrReplace(A_ScriptFullPath, ".ahk", ".ini")
Gui, Add, Picture, gSave, %image1%
Gui, Show, w220, Picture
Return

Save:
IniWrite, %image1%, %ini%, Image, image1
MsgBox, 64, Done, Stored!
Return
thrawn5499
Posts: 74
Joined: 26 Sep 2017, 15:36

Re: Need help storing user input and calling it back later.

29 Jul 2021, 12:45

I will try that thanks. I did make some progress. I was being stopped by a dumb mistake on my part. I stopped running my test scripts from notepad++ and started running them from autoGUI, but I had not saved it and as a result it was not writing the data to my .ini files. once I saved it and then ran it, it started working. below is what I have for my latest.

Code: Select all

#NoEnv
#SingleInstance Force
SetWorkingDir %A_ScriptDir%
Gui, Add, Button, Section, Set Directory 
Gui, Add, Edit, ys w178 vDir ReadOnly, 
Gui, Add, Text, xm, Pictures 
Gui, Add, ListBox, xm w260 r33 vPixList gPixList, 
Gui, Add, Picture, vPicImage ym, 
Gui, Show, , Picture Viewer 
return 

PixList: 
  Gui, Submit
  GuiControl,1:,PicImage,
  GuiControl,1:,PicImage, %currDir%\%PixList%
  GuiControlGet, PicImage, 1:Pos
  If PicImageW > 550
      GuiControl, 1:Move, PicImage, % "w550 h" . PicImageH*550/PicImageW
  GuiControlGet, PicImage, 1:Pos
  If PicImageH > 480
     GuiControl, 1:Move, PicImage, % "w" . PicImageW*480/PicImageH . " h480"
  Gui, Show, AutoSize
return 
FileDelete,test.ini
ButtonSetDirectory: 
  FileSelectFolder currDir 
  GuiControl,,Dir, %currDir% 
  Loop, %currDir%\*.* 
    { 
      SplitPath, A_LoopFileFullPath,OutFileName , , OutExtension
     If OutExtension in jpg,JPG,gif,GIF,png,PNG,bmp,BMP,tif,TIF,tiff,TIFF,jpeg,JPEG
          GuiControl,1:,PixList, %OutFileName%
          ;FileDelete,test.ini
          FileAppend,%OutFileName%,test.ini
    }
Return 

GuiClose: 
ExitApp
this at least writes the data to the ini file, but it is not formatted yet.
thrawn5499
Posts: 74
Joined: 26 Sep 2017, 15:36

Re: Need help storing user input and calling it back later.

29 Jul 2021, 13:03

mikeyww wrote:
29 Jul 2021, 12:37
Below is an idea to get you started.

Code: Select all

image1 = ....png ; Full path to image file
ini   := StrReplace(A_ScriptFullPath, ".ahk", ".ini")
Gui, Add, Picture, gSave, %image1%
Gui, Show, w220, Picture
Return

Save:
IniWrite, %image1%, %ini%, Image, image1
MsgBox, 64, Done, Stored!
Return
thanks for hitting me over the head a few times. sometimes it take a little bit for it to sink in. I think I have it working like I want so far. Now I will have to be able to pull the data from the file as needed. here is the code i am using.

Code: Select all

#NoEnv
#SingleInstance Force
SetWorkingDir %A_ScriptDir%
Gui, Add, Button, Section, Set Directory 
Gui, Add, Edit, ys w178 vDir ReadOnly, 
Gui, Add, Text, xm, Pictures 
Gui, Add, ListBox, xm w260 r33 vPixList gPixList, 
Gui, Add, Picture, vPicImage ym, 
Gui, Show, , Picture Viewer 
return 

PixList: 
  Gui, Submit
  GuiControl,1:,PicImage,
  GuiControl,1:,PicImage, %currDir%\%PixList%
  GuiControlGet, PicImage, 1:Pos
  If PicImageW > 550
      GuiControl, 1:Move, PicImage, % "w550 h" . PicImageH*550/PicImageW
  GuiControlGet, PicImage, 1:Pos
  If PicImageH > 480
     GuiControl, 1:Move, PicImage, % "w" . PicImageW*480/PicImageH . " h480"
  Gui, Show, AutoSize
return 
FileDelete,C:\Users\smoore\Desktop\test.ini
ButtonSetDirectory: 
  FileSelectFolder currDir 
  GuiControl,,Dir, %currDir% 
  Loop, %currDir%\*.* 
    { 
      SplitPath, A_LoopFileFullPath,OutFileName , , OutExtension
     If OutExtension in jpg,JPG,gif,GIF,png,PNG,bmp,BMP,tif,TIF,tiff,TIFF,jpeg,JPEG
          GuiControl,1:,PixList, %OutFileName%
          IniWrite, %OutFileName%, C:\Users\smoore\Desktop\test.ini, section1
         
    }
Return 

GuiClose: 
ExitApp
User avatar
mikeyww
Posts: 26437
Joined: 09 Sep 2014, 18:38

Re: Need help storing user input and calling it back later.

29 Jul 2021, 13:08

It is my pleasure. IniRead is convenient for reading your INI file.
thrawn5499
Posts: 74
Joined: 26 Sep 2017, 15:36

Re: Need help storing user input and calling it back later.

29 Jul 2021, 13:13

I am going to try that next. I think for a test, I am going to use the last code I posted to store the data in a ini file. then I am going to try to use the iniread to get the data from the ini file and show it in a msgbox. if I am successful, then that should be the start of making the other program read the ini file and get the files names from it to open them.
thrawn5499
Posts: 74
Joined: 26 Sep 2017, 15:36

Re: Need help storing user input and calling it back later.

29 Jul 2021, 14:59

Ok So now I have this. I have a few questions though in the msgbox it shows the entire contents of the directory selected. I expect this because I did not specify what line to read. Is that possible? also when I use this to get the file names from the ifi file, how do I tell it to go to the next line in the ini file?

If I change my iniread to this

Code: Select all

IniRead, OutputVar, C:\Users\smoore\Desktop\test.ini, section1, key
MsgBox, The value is %OutFileName%.
it grabs the first line from the ini file. not sure if this is the correct way to do it though. If so how do I set it to get the next line after I use the first one? maybe a loop?

here is my loop

Code: Select all

Loop, read, C:\Users\smoore\Desktop\test.ini
{
    Loop, parse, A_LoopReadLine, %A_Tab%
    {
             MsgBox, %A_LoopField%.
    }
}

It reads the [section1] from my ini file along with the other lines of text
how do I skip that?

Code: Select all

#NoEnv
#SingleInstance Force
FileDelete,C:\Users\smoore\Desktop\test.ini
SetWorkingDir %A_ScriptDir%
Gui, Add, Button, Section, Set Directory 
Gui, Add, Edit, ys w178 vDir ReadOnly, 
Gui, Add, Text, xm, Files 
Gui, Add, ListBox, xm w260 r33 vPixList gPixList, 
Gui, Add, Picture, vPicImage ym, 
Gui, Add, Button, default, OK  ; The label ButtonOK (if it exists) will be run when the button is pressed.
Gui, Show, , File Viewer 
return 

ButtonOK:
Gui, Submit  ; Save the input from the user to each control's associated variable.
return


PixList: 
  Gui, Submit
  GuiControl,1:,PicImage,
  GuiControl,1:,PicImage, %currDir%\%PixList%
  GuiControlGet, PicImage, 1:Pos
  If PicImageW > 550
      GuiControl, 1:Move, PicImage, % "w550 h" . PicImageH*550/PicImageW
  GuiControlGet, PicImage, 1:Pos
  If PicImageH > 480
     GuiControl, 1:Move, PicImage, % "w" . PicImageW*480/PicImageH . " h480"
  Gui, Show, AutoSize
return 

ButtonSetDirectory: 
  FileSelectFolder currDir 
  GuiControl,,Dir, %currDir% 
  Loop, %currDir%\*.* 
    { 
      SplitPath, A_LoopFileFullPath,OutFileName , , OutExtension
     
          GuiControl,1:,PixList, %OutFileName%
          IniWrite, %OutFileName%, C:\Users\smoore\Desktop\test.ini, section1
         
    }
;Return 

IniRead, Text, C:\Users\smoore\Desktop\test.ini, Section1,

MsgBox %Text%					; The MsgBox shows:

return

GuiClose:
 ExitApp
User avatar
mikeyww
Posts: 26437
Joined: 09 Sep 2014, 18:38

Re: Need help storing user input and calling it back later.

29 Jul 2021, 16:56

Without seeing your files or how you are using your script, my hunch is that you are not using a standard INI file format. If you just want to store a plain-text listing of many file paths, you can use an unstructured plain text file instead of an INI file. You can then use Loop to read each line. Except section headings, an INI file has a text pair such as color=white on each line. IniWrite and IniRead manage those pairs conveniently, enabling easy reading and updating of the values. You might not need that to happen for your purpose.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Google [Bot], morkovka18, Shifted_Right and 195 guests