1

I need to use mac terminal to open an audio stream from a URL through an applescript. I know that mac terminal is already equipped with afplay, but that only plays a file which is available on your computer. An alternative is mpg123 which can be installed in mac by means of brew. My question is how I can use this command in an apple script.

1 Answer 1

1

Command line programs can be run using the do shell script "..." command in AppleScript. You can generally write a command as you would in Terminal, keeping in mind that double quotes need to be escaped with a preceding backslash \. The do shell script command returns the last output to stdout.

So, for example:

do shell script "myvar=hello world; echo \"$myvar\""

will return hello world as a string.

These commands are run without a terminal, so can't interact with the user, e.g. to receive user input. If you need a terminal, you can tell application "Terminal" to tell the front window to tell the front tab to do script "...".

But, do shell script "..." is perfect for most situations where bash commands need to be employed.

If it's useful to know, iTunes can also play audio streams from the internet. You can do this from the command line like this:

open -a itunes "http://ssl.gstatic.com/dictionary/static/sounds/20160317/pronunciation--_us_1.mp3"

From within AppleScript, this might look like:

do shell script "open -a itunes \"http://ssl.gstatic.com/dictionary/static/sounds/20160317/pronunciation--_us_1.mp3\""

or, using iTunes scriptability:

tell application "iTunes" to open location "http://ssl.gstatic.com/dictionary/static/sounds/20160317/pronunciation--_us_1.mp3"
2
  • But I prefer to play the sound through terminal. I specifically need to know the syntax for using mpg123 command so that terminal can play the sound.
    – developer
    Commented Aug 11, 2018 at 19:41
  • Type this into Terminal: man mpg123 and it'll bring up everything you need to know on how to use the command.
    – CJK
    Commented Aug 12, 2018 at 1:49

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .