Create Hotstring from .txt-file. Topic is solved

Get help with using AutoHotkey and its commands and hotkeys
RubbeH
Posts: 43
Joined: 13 Jul 2020, 08:40

Create Hotstring from .txt-file.

15 Sep 2020, 07:59

Hello!

I've been searching for an answer for a long time but I haven't managed to find one..
What I want to do is simply, in my script, I want to be able to write hotstrings in an .txt-file, read this file and create hotstrings from it.

txt-file might be:
btw = by the way
ama = ask me anything
woops = oops I did it again.
abc = abcdefghijklmnopqrstuvwyz
Piggies = Once upon a time there was an old mother pig who had three little pigs and not enough food to feed them. So when they were old enough, she sent them out into the world to seek their fortunes.

donkeys = little
donkey
had a big big
farm

shoots = I shot the sheriff, but I didn't shoot no... what?
DEPUTY!


and If possible, make is to that it includes linebreaks in a good way?

Example, if I read the above txt-file, i'd like my script to contain the following hotstrings in the following way (I assume it needs to be the same for every string, else it'll get really complicated :p ) :

Code: Select all

:*:btw::by the way
:*:ama::ask me anything
:*:woops::oops I did it again.
:*:abc::abcdefghijklmnopqrstuvwyz
:*:Piggies::Once upon a time there was an old mother pig who had three little pigs and not enough food to feed them. So when they were old enough, she sent them out into the world to seek their fortunes.
:*:donkeys::
(
little
donkey
had a big big
farm
)
gregster
Posts: 5131
Joined: 30 Sep 2013, 06:48

Re: Create Hotstring from .txt-file.

15 Sep 2020, 08:08

Use the Hotstring() function to dynamically create hotstrings during runtime, for example from your parsed text file elements (requires AHK v1.1.28+).
RubbeH
Posts: 43
Joined: 13 Jul 2020, 08:40

Re: Create Hotstring from .txt-file.

15 Sep 2020, 09:05

gregster wrote:
15 Sep 2020, 08:08
Use the Hotstring() function to dynamically create hotstrings during runtime, for example from your parsed text file elements (requires AHK v1.1.28+).
I'm sorry but I don't understand how its done, the example script works pretty much as intended for creating hotstrings whilst running the script (and keeping them whilst in .ahk-format), I got that part covered already.

The problem is that it disappeares after reloading the script (note: I use compiled .exe format and not .ahk).
and there's not really a good way of finding which hotstrings you've added or editing them.
gregster
Posts: 5131
Joined: 30 Sep 2013, 06:48

Re: Create Hotstring from .txt-file.

15 Sep 2020, 09:19

Where is the problem? You just parse your text file and then call Hotstring() with this information.

Since you create the hotstrings yourself, you should be able to keep an overview (create a list, array, object or whatever), but you can assume that you created all hotstrings which you parsed from your text file (if their format can be parsed correctly by your script), if you loop through all of them.

For example:

Code: Select all

textfile =		; read this variable from your text file instead
(
btw = by the way
ama = ask me anything
)

hotstringlist := {}
loop, Parse, textfile, `n, `r
{ 
	arr := StrSplit(A_loopfield, "=")
	Hotstring(":*:" Trim(arr[1]), Trim(arr[2]))		; add or remove options as you like
	hotstringlist[arr[1]] := arr[2]			; keep tabs on created hotstrings
}	
return

^s::			; press Ctrl+s to show hotstrings
list := ""
for hstring, replacement in hotstringlist
	list .= hstring " = " replacement "`n"
msgbox % list				; or just show the original variable, read from your text file, if it has a good format already.
return
Press Ctrl+s to see a list of the created hotstrings in a msgbox.

Edit:
If instead you want to create a new stand-alone ahk-file with normal hotstring syntax, then just write the information similarly to a text (.ahk) file (adding the right hotstring syntax, of course), instead of calling Hotstring(). You could also append the hotstrings to your main (or any other) .ahk-script (obviously, you would need to reload/run that script afterwards to let them take effect - and obviously you can't do this with an exe-file, at least not easily).
RubbeH
Posts: 43
Joined: 13 Jul 2020, 08:40

Re: Create Hotstring from .txt-file.

15 Sep 2020, 13:48

gregster wrote:
15 Sep 2020, 09:19
Where is the problem? You just parse your text file and then call Hotstring() with this information.

Since you create the hotstrings yourself, you should be able to keep an overview (create a list, array, object or whatever), but you can assume that you created all hotstrings which you parsed from your text file (if their format can be parsed correctly by your script), if you loop through all of them.
This works as intended, awesome! Thank you! :bravo:

Code: Select all

FileRead, textfile, C:\ww\stringfile.txt
textfile1 := Trim(textfile, " `t`r`n")

loop, Parse, textfile1, `n, `r
{ 
	arr := StrSplit(A_loopfield, "=")
	Hotstring(":*:" Trim(arr[1]), Trim(arr[2]))
}	
return
Solved problem with leading or trailing space / empty lines aswell.

Also, I can't figure out how to ignore empty lines, or how to create multi-line hotstrings, example below :|
it crashed on the empty line because "Parameter must not be blank"
btw = by the way
ama = ask me anything
abc = awesome

asd = asdasd

ppp = lots
of
Pn in
this string
gregster
Posts: 5131
Joined: 30 Sep 2013, 06:48

Re: Create Hotstring from .txt-file.  Topic is solved

15 Sep 2020, 15:35

RubbeH wrote:
15 Sep 2020, 13:48
Also, I can't figure out how to ignore empty lines, or how to create multi-line hotstrings, example below :|
it crashed on the empty line because "Parameter must not be blank"
btw = by the way
ama = ask me anything
abc = awesome

asd = asdasd

ppp = lots
of
Pn in
this string
Well, your text file format is a bit ambiguous, because a line like ppp = lots looks exactly like the one-liners (for ex., asd = asdad).
That means, you need to know how the next line, after the current A_loopfield, looks like, to decide if it's a multi-liner or not. So, either use a clearer format, or you could do something like this, modifying an existing hotstring (the last one created), if the script discovers that it shouldn't be a one-liner:

Code: Select all

textfile =
(
btw = by the way
ama = ask me anything
abc = awesome

asd = asdasd

ppp = lots
of
Pn in
this string
)

;FileRead, textfile, C:\ww\stringfile.txt
textfile1 := Trim(textfile, " `t`r`n")

loop, Parse, textfile1, `n, `r
{ 
	If Instr(A_loopfield, "=") {
		arr := StrSplit(A_loopfield, "=")
		Hotstring(":*:" Trim(arr[1]), Trim(arr[2]))
		rpl := Trim(arr[2])
	}
	else if (A_loopfield != "") {
		rpl .= "`n" Trim(a_loopfield)
		Hotstring(":*:" Trim(arr[1]), rpl)		
	}
}	
return
Only tested shortly. Empty lines get currently ignored, also in multi-liners (perhaps consider some kind of placeholder for empty lines that should belong to a multi-liner - or make all empty lines count). For easy parsing, your text file format should be as unambiguous as possible.
RubbeH
Posts: 43
Joined: 13 Jul 2020, 08:40

Re: Create Hotstring from .txt-file.

15 Sep 2020, 16:24

gregster wrote: That means, you need to know how the next line, after the current A_loopfield, looks like, to decide if it's a multi-liner or not. So, either use a clearer format, or you could do something like this, modifying an existing hotstring (the last one created), if the script discovers that it shouldn't be a one-liner:

Code: Select all

textfile =
(
btw = by the way
ama = ask me anything
abc = awesome

asd = asdasd

ppp = lots
of
Pn in
this string
)

;FileRead, textfile, C:\ww\stringfile.txt
textfile1 := Trim(textfile, " `t`r`n")

loop, Parse, textfile1, `n, `r
{ 
	If Instr(A_loopfield, "=") {
		arr := StrSplit(A_loopfield, "=")
		Hotstring(":*:" Trim(arr[1]), Trim(arr[2]))
		rpl := Trim(arr[2])
	}
	else if (A_loopfield != "") {
		rpl .= "`n" Trim(a_loopfield)
		Hotstring(":*:" Trim(arr[1]), rpl)		
	}
}	
return
Only tested shortly. Empty lines get currently ignored, also in multi-liners (perhaps consider some kind of placeholder for empty lines that should belong to a multi-liner - or make all empty lines count). For easy parsing, your text file format should be as unambiguous as possible.
Thats amazing, thank you a million. I appreciate your effort helping out and at the same time educating me! :superhappy:

Return to “Ask For Help”

Who is online

Users browsing this forum: AHKStudent, Albireo, Batsmaru0410, Bing [Bot], boiler, f1ster, Google [Bot], Rohwedder, RubbeH and 52 guests