Noob: Play random sound from a folder using "play sound" action

I have a folder with a bunch of sound effects in it. I'd like to play a random one from the folder when I trigger a hotkey with the "play sound" action.

I'm pretty sure this is possible with KM but I have no idea how to do this.

I've seen some similar topics but nothing that I'm smart enough to convert to exactly what I'm trying to do. Any help would be GREATLY APPRECIATED!

thanks in advance!

I do something similar myself in one of my macros, but the files are images to search the screen for rather than sounds. I will have to go find it, although what I possibly did was get a list of the files and shuffle the list then take the first one from the list.

There may be a simpler way, if you redefine "random" to mean "use the files in rotation." You see, KM has an action called For Each which allows you to search on "The Items in Directory" and you can sort by "date last used" and you can click the button "reversed". If you do this, then the first file in that search will probably be the last file used. You can break from the loop at that point. This trick should work because each time you access the file to play it, the "last used date" is probably updated to the current time.

Here's a solution that I found on this website:

That solution was intended to search subdirectories too, but that shouldn't be a problem for you.

1 Like

Okay, here's some simple code that will play files in a "cyclical order" (not random order). I was really trying to make this code even simpler, but for some reason I can't understand, opening a file doesn't change the "last used data" on a file. I guess maybe it's only when the file is written that a file is "used". I'm going to continue to research that, but for now you will need the action that "touches" the file in order to change its date.

This is a pretty simple solution, but if you need to random order, the other post I mentioned probably contains an example.

Here's a version that filters out any extensions that aren't "wav": (this is the version I tested since my data folder contains dozens of files)

thanks Sleepy! I'll give that a shot... thanks for all the info!

is there any way to incorporate the "play sound" action instead of playing through an application? I don't really have an application that I want to play these through – the "play sound" option is really a perfect option for this!

I didn't think of that. Let me think about it. It appears that KM allows you to play files by name, but you have to select the name when you write the macro. This is one of the very rare cases where KM lacks flexibility!

Maybe AppleScript has a way to play a sound file by name. (It probably does.) I'll have to research that. It may take me an hour. I think we'll get you a solution.

AHA! There's a builtin shell command called afplay for just this sort of things. Give me a few minutes.

oops, is there some way to cancel an active KM 'command'?

I was experimenting and now it's playing the same audio clip over and over again – once for every file in that folder, I assume!

You can cancel the Engine from the KM menu. "Quit keyboard Maestro Engine." There are other ways too. Then you have to restart the engine from the KM Menu.

1 Like

Here's the simple solution to play the audio file: replace the Execute action in the macro above with this:

It's actually better to create a macro that contains the following, and assign that macro to some function key, like I did for F12:

If it's looping like that, maybe you didn't include the Break action that I had included. If you upload an image of your macro I can read it and check for errors.

1 Like

awesome! thanks so much for all your help... I'll try to implement this and see what happens. :wink:

1 Like

here's what I've got...

which isn't working.

I took out the "wav" part... did I take something else out by mistake?

I'm not sure if the folder you indicated is a folder or a file (that's one of the goofiest folder names I've ever seen.) Are you sure the folder name actually ends in "[mp3]"?? More likely, you thought that's how to specify a bunch of files, but if that's true, you are not correct. Please confirm the exact name of your folder. Are you sure the folder name is:

/Volumes/OchoSupreme/[ 0 ] W I P/atHELLO/atHELLO clips [mp3]

Minor detail: if you deleted the IF statement, then you don't need the Split action. But depending on how you answer the first question, you may need to put the IF statement back in.

I'm on a mac, so we can get as goofy as we like. :crazy_face:

Yes, that is the FOLDER name. Note: I can modify the name if the spaces, or any other character, happens to be throwing errors of any kind!

clarification: after looking at the folder name again, I realized I originally wanted to name the folder "@Hello" but my mac didn't like the "@" – so I had to spell it out. So, I was wrong: even macs don't let you do ANYTHING! :joy:

fwiw, these sound files are all movie quotes, which is where @Hello comes from – "you had me at hello"

1 Like

Ok, so does that mean everything is working now? I can't tell. I want to get you working.

nope. still not working. I was just answering your question about the folder name.

Since your code looks good, I'm guessing it's the crazy folder name, so I want you to open a Terminal window and then copy and paste this line into it.

ls /Volumes/OchoSupreme/[ 0 ] W I P/atHELLO/atHELLO clips [mp3]

I suspect it will fail because it looks like your folder name has multiple consecutive spaces, and since you didn't post the actual macro and I can't count the spaces out of your image, I need you to use the ls command on the folder name and tell me what it outputs.

Basically, my macro is so short that there's hardly anything that can go wrong except your folder name.

I just changed the name of the folder and also the location to this: ~/Desktop/audioClips

and it works!

I don't really understand what it's doing though. I thought it would just play them in the order listed in the folder. Instead, it's playing a random one, then changing the modified date... which is fine with me; just not what I expected.

thanks so much for your help Sleepy!!!

I did some more investigating and realized that not only can the folder names not have any spaces; but, the file names themselves can't have spaces either.

Good to know.