I've been playing around with custom keyboard shortcuts on my Mac, and I've constructed one using Automator that is supposed to pick and play a sound from a small folder.

This was achieved using an Automator service (as usual) and writing a small amount of Python shell code to play a sound with os.system and afplay.

from __future__ import absolute_import
import os
import random
import getpass
usr = getpass.getuser()
soundnum = random.randint(1, 22)
os.system(u'afplay /Users/{0}/sounds/sound{1}.mp3'.format(usr, soundnum))

(It was run through 3to2 beforehand so that's why it looks a little messy)

Testing this in Automator resulted in it playing a sound as intended, whereas after adding it as a shortcut in System Preferences it would not play.

Is there any reason as to why this is?


  • Can you replace the Automator action calling the python script with a shell script action just calling afplay /Users/YOURUSER/sounds/sound11.mp3 and try again? If it still doesn't work, please add the original Automator script to the question as well. – nohillside Jan 13 at 6:27
  • That would work, but the issue is that I have a friend intending on using this same service I have made on their Mac also, meaning I need to use getpass.getuser in order to get their native user folder name. The Automator script only consists of the Python code above, so I'm not sure what else is required, but please tell me if there is anything! – Small-Child Jan 13 at 6:33
  • It‘s just a debugging step to see where the problem is, you can keep the original version once it works – nohillside Jan 13 at 6:42
  • Also, which version of macOS and how do you call the python script? – nohillside Jan 13 at 6:43
  • I tested out just using afplay and it works just fine. I'm currently using Sierra 10.12.6, and I'm calling the python script using the Run Shell Script action to the Python shell (/usr/bin/python). – Small-Child Jan 13 at 6:46

You must log in to answer this question.

Browse other questions tagged .