Amulet users often ask us for additional voice commands to check the weather, perform unit conversion, read news headlines, and so on.
While we hope to incorporate some of these in a future software release, you can do a surprising amount right now using Amulet’s custom command capability, especially when you combine it with some VBScript.
To demonstrate, I’ve written a simple Visual Basic script to fetch the current weather forecast for your city from Yahoo Weather, decode the XML data returned, and speak the result using Amulet’s voice. Below, I walk you through it.
Note: This discussion is quite technical. If you’re comfortable editing text files with Notepad, you should be able to follow along fine, and adjust the script to suit your own purposes.
Creating the script
Before starting, make sure Windows Explorer is configured to show hidden files & folders. In any Explorer window, press and release the ALT key to show the menu bar, then choose Tools menu -> Folder Options -> (View) property page, and ensure that “Show hidden files, folders & drives” is selected.
Now, download Amulet.Weather.vbs by right-clicking on this link and choosing Save As. This is the VBScript file that fetches the weather. Save the file to the folder “C:\ProgramData\Amulet Devices”, which is where all of Amulet’s configuration files are stored. (If you are using Internet Explorer, you may need to change the file extension back to .vbs when saving it, since IE will have changed it to .txt).
Next, open that folder in Windows Explorer, right-click on Amulet.Weather.vbs and choose Edit to open the file in Notepad. You’ll need to make a change to the file to ensure it works correctly on your system.
The script has about 60 lines, and is quite straightforward. It works like this:
- Fetch the Yahoo XML feed corresponding to your city
- Decode the XML to retrieve the weather status for today
- Form a sentence from the weather status, and speak it
This same template can be applied to many other applications also.
Near the start of the script, you’ll see a line like this:
WeatherRSS = "http://weather.yahooapis.com/forecastrss?p=EIXX0014&u=f"
This is the RSS feed for the weather in Dublin, Ireland (where I am currently located). If you’re not in Dublin, you’ll want to change this to your own city. To do so, visit weather.yahoo.com and type in the name of your city to see a summary weather page for your area.
To the right of the main page heading is a small button marked RSS (just underneath the “Add weather to your website” link). Click on the RSS button to get the XML data feed for your city. Now cut & paste the web address shown in the address bar into Notepad, replacing the default Dublin address.
See the ‘&u=f’ at the end of the XML address? That’s a signal to Yahoo that you like your temperature units in Fahrenheit; if you change this to ‘&u=c’, then Yahoo will use Centigrade instead.
You can save the script now since you don’t need to make any other changes. While it’s open, however, take a look at this line near the bottom of the script:
Response = "It's " & weatherCondition & " and " & currTemp & " degrees. High " & highTemp & ", low " & lowTemp & ", in " + weatherCity & " today."
We’ve previously decoded the variables weatherCondition, currTemp, highTemp, lowTemp and weatherCity from Yahoo’s XML data (as you can see from the preceding lines in the script). This line simply ties them all together into a sentence that Amulet can speak back.
For example, right now Amulet tells me “It’s Partly Cloudy and 45 degrees. High 48, low 36, in Dublin today.”
You can vary the way this sentence is assembled to suit your taste. It took me a couple of iterations to get it sounding natural. My first attempt had a comma after “45 degrees” instead of a period, and it sounded too fast; the period introduces an extra pause which is helpful.
I also had no comma after “low 36”, and Amulet seemed to be muttering the “in Dublin” section. I eventually figured out that “low 36 in Dublin today” was being interpreted by Microsoft’s Speech Synthesizer as “low 36 inches Dublin today” — it assumed “36 in” was an abbreviation for “36 inches”. Adding a comma after “36” fixed it.
Just as important is this line:
Response = "Sorry, I can't predict the weather today."
If for some reason your Internet connection, or Yahoo’s website, is down then this ensures that you’ll still get a valid response from Amulet instead of just wondering why your command was ignored.
Having saved your script, you can try it out immediately. Just double-click on Amulet.Weather.vbs in the folder and it should respond with the current weather (you don’t need to be running Media Center at the time). If you’d like to see the text version of the weather, as well as hear it spoken, uncomment the line reading
' WScript.Echo Response
near the bottom of the script (just remove the quote from the start of the line), and a pop-up message box with the weather description will then appear each time you run the script.
The final few lines of the script simply pass this response to Amulet.Talker.exe, which is the Amulet component that speaks back to you. Calling Amulet.Talker.exe directly like this is a convenient way to add speech responses to any script that you are running on your system, especially background scripts that periodically perform housekeeping duties.
So that’s the weather taken care of. Now how do we persuade Amulet to run it when we issue a voice command?
Creating a Custom Command
All of Amulet’s custom commands are stored in a file called CustomCommands.txt which lives in the same “C:\ProgramData\Amulet Devices” folder where you stored the weather script. If you don’t already have a CustomCommands.txt file, you can copy the supplied CustomCommands-Sample.txt file instead.
The format of a custom command is very simple. It comprises three lines:
Command=(what Amulet recognizes)
Response=(what Amulet replies)
Action=(what Amulet should do to carry out your command)
A full description of Custom Commands is a little beyond the scope of this article, though you’ll find a lot of useful information in the examples at the top of CustomCommands-Sample.txt.
For our purposes, we can ignore the Response section which is for speaking a preset response message. Because our response is dynamically generated, we pass it directly to Amulet.Talker bypassing the Custom Commands response, so we can just leave out the Response line entirely.
To run a Visual Basic script, we use the Windows command line utility CScript.exe which lives in C:\Windows\System32, passing it the full pathname of the script we want to run. To avoid any unwanted pop-up messages or other distractions, we use Amulet’s Runback action.
Putting it altogether, we get these lines which can be added to the bottom of your CustomCommands.txt file:
; Fetch the current weather from Yahoo, and speak it
Command=What is the weather
Action=runback "C:\Windows\System32\CScript.exe" "C:\ProgramData\Amulet Devices\Amulet.Weather.vbs"
(Make sure the Action entry appears on a single line in the file; there should be a space between Amulet and Devices.)
Save your updated CustomCommands.txt file containing the above lines and restart Media Center. You can now try out the command by simply asking Amulet, “What is the weather?” You can create additional commands with the same Action if you want to be able to ask Amulet what the weather is like in different ways.
(Hint: you can run the command “C:\Program Files\Amulet Devices\Amulet.Scanner.exe” /Custom to tell Amulet when custom commands have been updated without needing to restart Media Center.)
As you can see, adding new custom commands to Amulet is quite easy, and opens up an unlimited range of possibilities for extending Amulet’s functionality.
We don’t currently support passing parameters to custom commands, so it’s not yet possible to use voice to, for example, convert pounds to kilos or schedule a timer to remind you about something in a few minutes. We hope to significant extend the custom command capability in a future release so that you can do things like that.
If you use this script as the starting point for adding your own dynamic Amulet commands, we’d love to hear about it — please post a message in our General Forum.