Play a single random sound from this folder Topic is solved

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
User avatar
hced
Posts: 9
Joined: 11 Nov 2019, 13:30

Play a single random sound from this folder

24 Aug 2023, 06:32

Hi, I'm trying to cobble together a script that should reside in folders containing sound files.

Image

Here's what I got so far:

Code: Select all

; Play a single random sound from this folder
path := A_ScriptDir
Loop Files, path "\*.wav"
    lst .= A_LoopFilePath "`n"
lst := Sort(lst, "Random")
for x,y in StrSplit(lst,"`n")
    SoundPlay(y, Wait)
But I'm getting a

Code: Select all

Warning: This variable appears to never be assigned a value
pointing to line 7:

Code: Select all

SoundPlay(y, Wait)
...

Image

For the record, I'm to implement this script in V2.

Would some kind-hearted person please help me to make this script work in V2?
gregster
Posts: 9114
Joined: 30 Sep 2013, 06:48

Re: Play a single random sound from this folder

24 Aug 2023, 06:46

Afaics, "Wait" should be a string in v2, thus quoted.
Unquoted it will be evaluated as a variable (which is empty in this case).
User avatar
hced
Posts: 9
Joined: 11 Nov 2019, 13:30

Re: Play a single random sound from this folder

24 Aug 2023, 06:55

@gregster That worked as far as playback goes, thank you. Now, I'm not particularly great at coding and just realize I've made it to play ALL files in a LOOP. I wish to just play ONE single file once. How should I adjust this script accordingly?
User avatar
SKAN
Posts: 1551
Joined: 29 Sep 2013, 16:58

Re: Play a single random sound from this folder  Topic is solved

24 Aug 2023, 09:23

hced wrote:
24 Aug 2023, 06:55
I wish to just play ONE single file once.
 

Code: Select all

#Requires AutoHotkey v2.0
#SingleInstance

path := A_WinDir "\Media"
Loop Files, path "\*.wav"
    lst .= A_LoopFileFullPath "`n"
lst := RTrim(lst, "`n")
arr := StrSplit(lst, "`n")
rnd := Random(1, arr.Length)

SoundPlay(arr[rnd], "Wait")
User avatar
hced
Posts: 9
Joined: 11 Nov 2019, 13:30

Re: Play a single random sound from this folder

24 Aug 2023, 09:31

SKAN wrote:
24 Aug 2023, 09:23
Thank you SKAN, that did the trick! :wave:

Note. For anyone wanting the exact functionality per my first post, I added A_ScriptDir (to search the current folder of the script) and #NoTrayIcon to get rid of the systray icon popup.

Code: Select all

; Play a single random sound from this folder
#Requires AutoHotkey v2.0
#SingleInstance
#NoTrayIcon

path := A_ScriptDir
Loop Files, path "\*.wav"
    lst .= A_LoopFileFullPath "`n"
lst := RTrim(lst, "`n")
arr := StrSplit(lst,"`n")
rnd := Random(1, arr.Length)

SoundPlay(arr[rnd], "Wait")

Return to “Ask for Help (v2)”

Who is online

Users browsing this forum: No registered users and 98 guests