Create Hotstring from .txt-file. Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
RubbeH
Posts: 49
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: 9002
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: 49
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: 9002
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: 49
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: 9002
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: 49
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:
RubbeH
Posts: 49
Joined: 13 Jul 2020, 08:40

Re: Create Hotstring from .txt-file.

20 Apr 2023, 09:00

bumping this old thread.

I haven't used this code in a few years, and now when I try to run it I get an error whilst calling Hotstring - Idk if something changed? Any ideas?
Note: The code might be slightly changed compared to the last time I used it, but I haven't used ahk syntax in years so I got no clue what to look for :|

I tried checking my ahk version and it seems to be 1.1.23.01

Error: Call to nonexistent function.

Specifically: Hotstring(":CT*:" Trim(arr[1]), Trim(arr[2]))

Line#
---> 071: Hotstring(":CT*:" Trim(arr[1]), Trim(arr[2]))


current code:

Code: Select all

FileRead, stringfile, C:\Users\blabla\blabla.txt
stringfile := Trim(stringfile, " `t`r`n")
error_string = Error, you messed up i blabla.txt
loop, Parse, stringfile, `n, `r
{
	If Instr(A_loopfield, "####") {
		continue
	}
	If Instr(A_loopfield, "=") {
		arr := StrSplit(A_loopfield, "=")
		If (Trim(arr[2]) = "") {
			MsgBox % error_string a_loopfield
			break
		}
		else If (Trim(arr[1]) = "") {
			MsgBox % error_string a_loopfield
			break
		}
		Hotstring(":CT*:" Trim(arr[1]), Trim(arr[2]))
		rpl := Trim(arr[2])
		Hotstringslist .= Trim(arr[1]) " -> " Trim(arr[2]) "`n"
	}
	else if (A_loopfield != "") {
		rpl .= "`n" Trim(a_loopfield)
		Hotstring(":CT*:" Trim(arr[1]), rpl)
		Hotstringslist .= Trim(rpl[1]) Trim(a_loopfield) "`n"
	}
}
return
User avatar
boiler
Posts: 16931
Joined: 21 Dec 2014, 02:44

Re: Create Hotstring from .txt-file.

20 Apr 2023, 09:38

RubbeH wrote: I haven't used this code in a few years, and now when I try to run it I get an error whilst calling Hotstring - Idk if something changed? Any ideas?

I tried checking my ahk version and it seems to be 1.1.23.01

Error: Call to nonexistent function.
Not quite sure how you got that error and even noted what your AHK version is and didn’t reach the conclusion that you should update to the latest v1 version and try that. The documentation for the Hotstring() function shows that it was added at version 1.1.28.
RubbeH
Posts: 49
Joined: 13 Jul 2020, 08:40

Re: Create Hotstring from .txt-file.

21 Apr 2023, 07:26

boiler wrote:
20 Apr 2023, 09:38
RubbeH wrote: I haven't used this code in a few years, and now when I try to run it I get an error whilst calling Hotstring - Idk if something changed? Any ideas?

I tried checking my ahk version and it seems to be 1.1.23.01

Error: Call to nonexistent function.
Not quite sure how you got that error and even noted what your AHK version is and didn’t reach the conclusion that you should update to the latest v1 version and try that. The documentation for the Hotstring() function shows that it was added at version 1.1.28.
Thats a really good question how I didn't come to that conclusion by myself.. :headwall: Thanks for enlightening me of my own stupidity :D

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Frogrammer, gongnl and 265 guests