I created a macro that lets me choose a name from a list and I would like to play the according sound from a folder. So if I select "foo", I would play /Users/eric/Sounds/foo.mp3.
The Play Sound action however only lets me select a real file, not provide the location in a variable.
Thanks @gglick.
The drawback with afplay is that I can't specify which output device to use. Most of the time, my sound output is on the screen or the bluetooth headphones. Asking to play sound on the default output allowed me to make sure that the sound was playing audibly without having music at the same time.
So, the closest I've managed with afplay is to pause iTunes (and hope I don't use it with a Youtube video playing), save default output, switch to builtin ouput, play the sound and then switch back:
I know, I could have extracted the path and generally made it better but I'm an AppleScript noob.
tell application "Keyboard Maestro Engine"
set KMVAR_classetag to do shell script "echo $KMVAR_classetag"
set snd_cmd to "<?xml version=\"1.0\" encoding=\"UTF-8\"?><!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\"><plist version=\"1.0\"><array><dict><key>DeviceID</key><string>AppleHDAEngineOutput:1B,0,1,1:0</string><key>MacroActionType</key><string>PlaySound</string><key>Path</key><string>/Users/darcher/Downloads/classe-am/" & KMVAR_classetag & ".mp3</string><key>TimeOutAbortsMacro</key><true/></dict></array></plist>"
do script snd_cmd
end tell
I tried to do:
set KMVAR_classetag to getvariable "KMVAR_classetag"
You can also use the “play sound” AppleScript command in the Keyboard Maestro Engine, which lets you choose whether to use the sound effect or default device.
tell app "Keyboard Maestro" to play sound SOUNDFILE with/without soundeffect
The variable name should be in double quotes and without KMVAR since KMVAR_ is just the prefix for shell environment. I like this much better than the shell thing. (The security guy inside me was screaming)
Ok, so TIL that you can check the exact syntax for the commands exposed by an app by using, in the Applescript editor, the file => open dictionary and selecting the app:
This is much simpler although sending to the "soundeffects" output doesn't really work for me. Thanks for the suggestion though.