Play sound from variable

Hi.

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.

Does someone know how I can achieve that ?

Hi @edarchis,

Instead of Play Sound, I suggest trying an Execute a Shell Script action with an afplay command, like this:

afplay "$KMVAR_LocalFile"
(replace "LocalFile" with the variable being used in your macro)
image

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:
07

Not really ideal but it's probably good enough.

1 Like

You could hack the Play Sound Action:

  1. Setup a Play Sound action that gets the sound from a file (any file)
  2. Select this Action
  3. right-click on it and "Copy as XML" and use in below AppleScript
  4. Create an AppleScript
    • Get's the sound file path from KM
    • Replaces the path in the above XML with this file path
    • tell KM Engine to "do script" with the revised XML

Give it a shot. If you need help, let us know.

1 Like

I love that hack ! Here is the result:

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"

instead of using a shell but it didn't work.

Anyway, it works great. Thanks @JMichaelTX

1 Like

You are very close. You just need to tell KM Engine like this:

tell application "Keyboard Maestro Engine" to set KMVAR_classetag to getvariable "KMVAR_classetag"

For more info see Using AppleScript with Keyboard Maestro .

In the future please put your scripts in a Code Block. I fixed your post for you this time. :smile:

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

You can also optionally set the volume as well.

It is actually:

set classetag to getvariable "classetag"

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:
31

This is much simpler although sending to the "soundeffects" output doesn't really work for me. Thanks for the suggestion though.

Yes, that is correct. I was just starting with your statement:

thinking that you had actually named your KM variable "KMVAR_classetag".

So, let me present a more clear, complete statement:

Given a KM variable named "classetag", you can get the value of this Variable in AppleScript using this simple script:

tell application "Keyboard Maestro Engine"
   set kmClassetag to getvariable "classetag"
end tell

I like to use the prefix of "km" with my AppleScript variables to denote that the variable came from Keyboard Maestro.

All of this is clearly documented in the KM wiki article Using AppleScript to Get and Set Keyboard Maestro Variables.