How to open a URL with Terminal through applescript by using mpg123

applescripthomebrewterminal

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.

Best Answer

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"