Send random txt message from a .txt file when i press a key Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
coke2315
Posts: 3
Joined: 17 Sep 2020, 09:11

Send random txt message from a .txt file when i press a key

Post by coke2315 » 17 Sep 2020, 09:25

Hi guys and sorry if already was some topic about that, i did some search on this forum about that but i didn't found a concrete script at what i need.
If anyone would help me, i will be very thankfully.

I want a script like this:
When i press a key, like Ctrl+ Z i want to paste a random text from a .txt file with multiple texts.
I tried different versions of scripts that i fount there but no one worked. Can anyone help me, please?:D

User avatar
TheDewd
Posts: 1503
Joined: 19 Dec 2013, 11:16
Location: USA

Re: Send random txt message from a .txt file when i press a key

Post by TheDewd » 17 Sep 2020, 09:43

Something like this will work... You could make random more advanced, not showing values that have already been shown until all available choices have been used, etc...

Code: Select all

#SingleInstance, Force

TextList =
(
Text001
Text002
Text003
Text004
Text005
Text006
Text007
Text008
Text009
Text010
)

StringReplace, TextList, TextList, `n, `n, UseErrorLevel

TotalLines := ErrorLevel + 1

^z::
	Random, RndLine , 1, % TotalLines

	Loop, Parse, TextList, `n
	{
		If (A_Index = RndLine) {
			Send, % A_LoopField
		}
	}
return

AHKStudent
Posts: 1472
Joined: 05 May 2018, 12:23

Re: Send random txt message from a .txt file when i press a key

Post by AHKStudent » 17 Sep 2020, 09:47

this uses a text file

Code: Select all

if !FileExist("abctest.txt")
{
	FileAppend, the quick lazy`ndog jumped over`nthe red crazy fox`nnext to the river,abctest.txt
}
FileRead, x, abctest.txt
loop, parse, x, `n
	lns := A_Index
random, y, 1, % lns
FileReadLine, v, abctest.txt, % y
msgbox, % v
ExitApp

coke2315
Posts: 3
Joined: 17 Sep 2020, 09:11

Re: Send random txt message from a .txt file when i press a key

Post by coke2315 » 17 Sep 2020, 10:32

AHKStudent wrote:
17 Sep 2020, 09:47
this uses a text file

Code: Select all

if !FileExist("abctest.txt")
{
	FileAppend, the quick lazy`ndog jumped over`nthe red crazy fox`nnext to the river,abctest.txt
}
FileRead, x, abctest.txt
loop, parse, x, `n
	lns := A_Index
random, y, 1, % lns
FileReadLine, v, abctest.txt, % y
msgbox, % v
ExitApp
And how can I attach this to a key? Like first one. Also thanks a lot for your fast reply guys, you are really amazing.
Last edited by gregster on 17 Sep 2020, 10:45, edited 1 time in total.
Reason: Post was approved after TheDewd's following post.

User avatar
TheDewd
Posts: 1503
Joined: 19 Dec 2013, 11:16
Location: USA

Re: Send random txt message from a .txt file when i press a key  Topic is solved

Post by TheDewd » 17 Sep 2020, 10:35

Non-repeating random, from text file:

Code: Select all

#SingleInstance, Force ; Allow only one running instance of script

; Generate text file if not already existing
If !(FileExist("TextList.txt")) {
	File := FileOpen("TextList.txt", "w")
	File.WriteLine("Text001")
	File.WriteLine("Text002")
	File.WriteLine("Text003")
	File.WriteLine("Text004")
	File.WriteLine("Text005")
	File.WriteLine("Text006")
	File.WriteLine("Text007")
	File.WriteLine("Text008")
	File.WriteLine("Text009")
	File.WriteLine("Text010")
	File.Close()
}

; Read contents of text file to variable
FileRead, TextVar, TextList.txt

; Remove leading and trailing whitespace
TextVar := RegexReplace(TextVar, "^\s+|\s+$")

arrTextList := [] ; Initialize array

; Loop each line of stored text from variable
Loop, Parse, TextVar, `n
{
	arrTextList.Insert(A_LoopField) ; Insert text line into array
}

^z:: ; Ctrl + Z
	If (!arrTextList.MaxIndex()) {
		MsgBox, Complete ; Show message when all options have been sent
		return
	}

	; Generate random number within range of options
	Random, rnd, % arrTextList.MinIndex(), % arrTextList.MaxIndex()

	; Send text from array
	Send, % arrTextList[rnd]

	; Remove text from array once sent to avoid duplication
	arrTextList.Remove(rnd) 
return

coke2315
Posts: 3
Joined: 17 Sep 2020, 09:11

Re: Send random txt message from a .txt file when i press a key

Post by coke2315 » 17 Sep 2020, 11:17

Thank you very much guys. I really appreciate your help.

Post Reply

Return to “Ask for Help (v1)”