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
Send random txt message from a .txt file when i press a key Topic is solved
Re: Send random txt message from a .txt file when i press a key
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
-
- Posts: 1472
- Joined: 05 May 2018, 12:23
Re: Send random txt message from a .txt file when i press a key
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
Re: Send random txt message from a .txt file when i press a key
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.AHKStudent wrote: ↑17 Sep 2020, 09:47this uses a text fileCode: 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
Last edited by gregster on 17 Sep 2020, 10:45, edited 1 time in total.
Reason: Post was approved after TheDewd's following post.
Reason: Post was approved after TheDewd's following post.
Re: Send random txt message from a .txt file when i press a key Topic is solved
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
Re: Send random txt message from a .txt file when i press a key
Thank you very much guys. I really appreciate your help.