How to play a sound or tone when a program ends?

Right, I suspect this is related to spaces in a path with powershell (see this discussion). I think the following will catch Windows folders with and without spaces:

function play_a_tune2(SoundFileName1)
    script = ```\$sound = New-Object System.Media.SoundPlayer\;
                \$sound.SoundLocation = \& \"$SoundFileName1\" \;
                \$sound.Play\(\)```
    run(`powershell /c $script`)
end

I tested this with a .wav file with two paths on my Windows 10 machine:

play_a_tune2("C:\\folder_without_spaces\\elephant.wav")

play_a_tune2("C:\\folder with spaces\\elephant.wav")

I hear an elephant with both function calls. Or I could also be going crazy.

4 Likes