Need help with a project

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
vvdee
Posts: 14
Joined: 08 Mar 2019, 08:41

Need help with a project

02 May 2024, 09:57

Hello i need help with a project, im stuck on some parts which are probably easy for you.

1)

Code: Select all

000001 := A example
000002 := B example
000003 := C example
000004 := D example
000005 := E example

F2::
Random, randNum, 0, 5
formattedNum := Format("{:06}", randNum) 
Clipboard := formattedNum
sleep, 250
send, %formattedNum%
sleep, 250
~When i press F2 it generates random number but i need it to send the text defined for that number, so how can i do that? Also i will define more than 300 text, so you can suggest if there is better way like getting defines from another txt file or something instead of putting them in ahk script

Code: Select all

F3::
{
    filePath := "C:\Users\W11\Desktop\x\000005\readme.txt"
     FileRead, fileContent, %filePath%
    

    Clipboard := fileContent

}
Return
2)~And i want to use generated number as folder name to find file location like:

Code: Select all

filePath := "C:\Users\W11\Desktop\x\%formattedNum%\readme.txt"
So how can i make it work?

Thanks
Rohwedder
Posts: 7706
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: Need help with a project

02 May 2024, 10:53

Hallo,
use Arrays https://www.autohotkey.com/docs/v1/Objects.htm#Usage_Simple_Arrays

Code: Select all

Text := {} ; Array
Text.Push("A example","B example","C example","D example","E example")
F2::
Random, randNum, 1, Text.Length()
SendInput,% Text[randNum]
Return
User avatar
mikeyww
Posts: 27161
Joined: 09 Sep 2014, 18:38

Re: Need help with a project

02 May 2024, 11:00

Code: Select all

#Requires AutoHotkey v1.1.33.11
000001 := "A example"
000002 := "B example"
000003 := "C example"
000004 := "D example"
000005 := "E example"

F2::
Random rnd, 1, 2
num := Format("{:06}", rnd)
SendInput % "{Text}" %num%
Return

F3::
Random rnd, 1, 2
FileRead txt, % Format("{:06}", rnd) "\snippets.txt"
SendInput % "{Text}" txt
Return
Explained: Expressions

:= is always followed by an expression, never a literal string.
RussF
Posts: 1290
Joined: 05 Aug 2021, 06:36

Re: Need help with a project

02 May 2024, 11:21

Also...

From https://www.autohotkey.com/docs/v1/Concepts.htm#names
Although a variable name may consist entirely of digits, this is generally used only for incoming command line parameters. Such numeric names cannot be used in expressions because they would be seen as numbers rather than variables. It is best to avoid starting a name with a digit, as such names are confusing and will be considered invalid in AutoHotkey v2.
Russ
User avatar
Chunjee
Posts: 1447
Joined: 18 Apr 2014, 19:05
Contact:

Re: Need help with a project

02 May 2024, 12:15

vvdee wrote:
02 May 2024, 09:57
I would use one array otherwise you may end up juggling 300+ variables 😱🙅🚫


Additionally you can shuffle an array, and pull from the end. If a random non-repeating order is ever desired

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: AlFlo, arrondark, Chunjee, slowwd, sn1perwild, Xeilous and 100 guests