Display Contents of File Whose FilePath is a variable

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
AlFlo
Posts: 363
Joined: 29 Nov 2021, 21:46

Display Contents of File Whose FilePath is a variable

Post by AlFlo » 13 Jan 2023, 14:28

Okay, after banging my head against the wall for many hours, I'm going to ask for help...

Mikeyww wrote the following script to break each line of text stored in my clipboard, and then display it as a menu item to paste where needed:

Code: Select all

For each, line in StrSplit(Clipboard, "`n", "`r")
 Menu, scripts, Add, %line%, Execute
Menu, scripts, Show
Return

Execute:
SendInput {Text}%A_ThisMenuItem%
Return
The script worked really well at first, but has recently been throwing the error: "Menu item name too long" even though no line of text in my clipboard exceeds 260 characters.

Moreover, I've realized that what I really need is to be able to access certain client data anytime. So I need to store it in a text file (one in each client's specific subfolder).

So I want a script which is in the format:

Code: Select all

FileRead, Contents, %FilePath%
where FilePath is a variable specified through a GUI and loop function which gets the FilePath for a specific client (I can post that code if it helps).

So 2 issues:

(1) How do I copy the contents of the file without throwing a menu item name too long error; and
(2) Can I do a

Code: Select all

FileRead, contents
when the FilePath is not fixed, but is variable?

Thanks in advance for any help! Of course, if an array or a list works better than a menu, that's okay. I just want it to be robust.
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: Display Contents of File Whose FilePath is a variable

Post by swagfag » 13 Jan 2023, 14:49

"Menu item name too long" even though no line of text in my clipboard exceeds 260 characters.
how would u know? have u checked? how did u check? i think the computer knows better than u

Code: Select all

For each, line in StrSplit(Clipboard, "`n", "`r")
{
	try
		Menu, scripts, Add, %line%, Execute
	catch Err
	{
		MsgBox % Err.Message "`nLength was: " StrLen(line) "`nContent was: >>" line "<<"
		ExitApp
	}
}

if the user is typing the literal text FilePath into an InputBox/Gui, and u have a variable called FilePath to which something resembling an actual file path is assigned, u need to double-deref whatever variable collects ur users' input(and also abide by any variable scoping rules, if relevant), eg:

Code: Select all

FilePath := "path/to/wherever.exe"
...
FileRead, Contents, % %where_i_keep_what_my_user_writes% ; and hopefully they wrote FilePath
AlFlo
Posts: 363
Joined: 29 Nov 2021, 21:46

Re: Display Contents of File Whose FilePath is a variable

Post by AlFlo » 13 Jan 2023, 18:18

Thanks SF,

Your script helped me realize that the ENTIRE contents of the clipboard is somehow counting towards the 260 character limit, not just each line, outputting the following message:
Target label does not exist.
Length was: 555
So I guess my script is not parsing each line of text in the clipboard before the menu tries to do its thing ...

Could I force each line into a separate variable to "brute force" the menu into reading just one line at a time? Here's my clumsy attempt, which undoubtedly uses the wrong syntax:

Code: Select all

FileRead, content, FilePath

Var := StrSplit(content,"`n","`r") 

For each, line in Var
Menu, scripts, Add, %line%, Execute
Menu, scripts, Show
Return
Last edited by AlFlo on 13 Jan 2023, 18:50, edited 1 time in total.
AlFlo
Posts: 363
Joined: 29 Nov 2021, 21:46

Re: Display Contents of File Whose FilePath is a variable

Post by AlFlo » 13 Jan 2023, 18:21

In terms of the FilePath, instead of a normal InputBox or Gui, I want to use the following script ... which is great because I only have to type a couple letters of the client's name and then it will pop up as a choice:

Code: Select all

BasePath := "C:\Clients\"
focusFirst := True ; Focus on the first item in the list
Gui, Font, s10
Gui, Add, Edit   , w1000 vfind    gTyped

Gui, Add, ListBox, wp   vscript  gClik  r20
Gui, Show,, Party Info Sheet
Return

Gosub, Filter
Gosub, F3
F3::Gui, Show,, Scripts (F3 = show again)

Typed:
SetTimer, Filter, -300
Return

Filter:
Gui, Submit, NoHide
FileList = |

loop, Files, % BasePath "*.*", D
	If InStr(A_LoopFileFullPath, find)
		FileList .= A_LoopFileFullPath "|"
FileList := RTrim(FileList, "|")
 Sort, FileList, D|

(focusFirst) && FileList := StrReplace(FileList, BasePath)
GuiControl,, script, %FileList%
GuiControl, % focusFirst && FileList > "|" ? "Enable" : "Disable", Run

return

Clik:
If (A_GuiEvent != "DoubleClick") {
Gui, Submit
GuiControl, Focus, find
	 
; HERE IS WHERE I SOMEHOW FEED THE FOLLOWING FILEPATH INTO THE FILEREAD FUNCTION: C:\Clients\"%script%"\Client and Party Info\Party Info Sheet.docx
 
 Gui , Hide
Return
}
crypter
Posts: 90
Joined: 15 Dec 2020, 09:57

Re: Display Contents of File Whose FilePath is a variable

Post by crypter » 13 Jan 2023, 21:10

Here's an example of how you can read the contents of a text file and store them in a variable without using a menu, which should avoid the "Menu item name too long" error:

;

Code: Select all

 Prompt the user to select a file
FileSelectFile, FilePath, , , Select a file to read, , , , , , , , , %A_Desktop%

; Read the contents of the file into a variable
FileRead, Contents, %FilePath%

; Loop through the contents of the variable, one line at a time
Loop, Parse, Contents, `n, `r
{
    ; Do something with each line of the file
    MsgBox, %A_LoopField%
}
Alternatively, you can use a ListView control to display the contents of the file in a more organized way:

Code: Select all

Gui, Add, ListView, vMyListView gMyListView
Gui, Show

; Read the contents of the file into a variable
FileRead, Contents, %FilePath%

; Loop through the contents of the variable, one line at a time
Loop, Parse, Contents, `n, `r
{
    ; Add each line of the file to the ListView control
    LV_Add("", A_LoopField)
}

MyListView:
; Get the selected item
LV_GetText(SelectedItem, A_EventInfo, 1)
; Do something with the selected item
MsgBox, %SelectedItem%
Regarding the second issue, you can use the variable FilePath as the parameter for the FileRead function, like this:

Code: Select all

FileRead, Contents, %FilePath%
You should make sure that FilePath contains the correct path before calling the FileRead function.

Hope this helps!
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: Display Contents of File Whose FilePath is a variable

Post by swagfag » 13 Jan 2023, 21:51

AlFlo wrote:
13 Jan 2023, 18:18
So I guess my script is not parsing each line of text in the clipboard before the menu tries to do its thing ...
to know how to parse the clipboard, u need to be sure first of what it contains

Code: Select all

StrSplit(.....,"`n","`r")
splits on LFs and strips CRs. its possible whatever ure copying has its line endings set to CR instead. use nirsoft insideclipboard to inspect the raw hex contents

Code: Select all

...
Clik:
If (A_GuiEvent != "DoubleClick") {
Gui, Submit
GuiControl, Focus, find
	 
; HERE IS WHERE I SOMEHOW FEED THE FOLLOWING FILEPATH INTO THE FILEREAD FUNCTION: C:\Clients\"%script%"\Client and Party Info\Party Info Sheet.docx
mylongpath = "C:\Clients\%script%\Client and Party Info\Party Info Sheet.docx"
MsgBox % mylongpath

 Gui , Hide
Return
}
AlFlo
Posts: 363
Joined: 29 Nov 2021, 21:46

Re: Display Contents of File Whose FilePath is a variable

Post by AlFlo » 14 Jan 2023, 12:52

Thank you, SF!!!

NirCommand showed me that my clipboard is inconsistent with using carriage returns or indications of any kind of a line break. I simply switched the order of

Code: Select all

'n
and

Code: Select all

'r
like this, and it works!

Code: Select all

For each, line in StrSplit(Contents, "`r", "`n")
AlFlo
Posts: 363
Joined: 29 Nov 2021, 21:46

Re: Display Contents of File Whose FilePath is a variable

Post by AlFlo » 14 Jan 2023, 15:21

Thank you, crypter! I will play around with these.
Post Reply

Return to “Ask for Help (v1)”