Tag Archives: Windows 8

Quickie Custom ‘Speak’ Protocol

Posted on by

I mentioned on Facebook I was thinking about adding the second protocol I did and also about changing this to a more tech related blog… Well, here’s the “Speak” protocol and I’m still debating on if/when I may do the blog changes.

If you came directly to this post, read the previous one to get an idea about Custom Web Protocols and an idea of what’s going on.

*Required Software*
For starters, this protocol uses some freeware called NirCmd (by NirSoft) which can be downloaded from the bottom of this page. Make sure to download the correct 32bit/64bit version and after you extract it, copy NirCmd.exe to your Windows\System32 directory either manually or by running NirCmd (it offers a button to copy it there automatically).


If you still have the *.reg file from the “Sublime Protocol” you can make a copy of that file and do some simple changes to create this, or if you’re starting from scratch open “Sublime Text” (or your favorite text editor… yes, even Notepad works) and copy/paste the following into your document. Don’t forget to save it with the *.reg extension.

Registry File
Warning:
Modifying the Windows Registry can be hazardous to your computer and cause data corruption, data loss, or just outright kill your system, therefore proceed at your own risk and I will not be held responsible for any damage you may/may not cause. This registry file has been tested and deemed safe on both Windows 7 and Windows 8 but has not been tested on XP or Vista.

Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\speak]
"URL Protocol"=""
@="URL:SpeakText"

[HKEY_CLASSES_ROOT\speak\DefaultIcon]
@="C:\\Windows\\System32\\shell32.dll,282"

[HKEY_CLASSES_ROOT\speak\shell]

[HKEY_CLASSES_ROOT\speak\shell\open]

[HKEY_CLASSES_ROOT\speak\shell\open\command]
@="C:\\Windows\\System32\\cmd.exe /K nirSpeak.cmd \"%1\""

Notes:

  • If you change the trigger for the protocol (speak) it needs all five references changed.
  • The double \ in the file paths are required.

The last line needs to be altered if you change the name of the Command/Batch file or you get a security error for running the Command Prompt (see previous post for info on this and how to fix it).

Batch File
Taking the name from the registry file, in this case “nirSpeak.cmd”, we create a new empty document with that name and copy the below code into it.

@ECHO OFF
TITLE Archigos NirSpeak
for /F "tokens=1,2* delims=/" %%a in ("%~1") do set protocol=%%a&set str=%%b
nircmd speak text "%str%"
exit

*Using*
There’s one large difference between this and the previous protocol (other than the obvious usage) in which this does NOT encode/decode the information sent which changes a few things with the usage. With the Sublime Protocol for example, if a filename contained a space, that would be converted to %20 and back again where required, but the Speak Protocol would actually say “percent two zero” if you tried to manually encode it. The easiest way to create speech is to create a variable with the entire string you want and than just putting the variable into the URI (which also prevents certain errors due to spaces like speak://multiple words would. It wouldn’t cause a PHP error, but NirCmd would most likely error out or only say the first word).

A great example of how you may want to use this would be to greet your user after they log into your site.

$username = $_POST['username]';
$greeting = "Greetings ".$username;

And then you’d use a JavaScript function or similar to invoke it (in this case: speak://$greeting) on page load or whatever. You could also use buttons or inputs in a similar fashion, like the button I use on one of my sites that reads the stats to the user.

echo "
";

*Final Thoughts*
Obviously both this and the Sublime Protocol could benefit from hiding the Command Window that is spawn but I wanted to write these using as little third party software as I could to make it easier on the reader. Oh and for those that want to play around with the speech function there’s a bunch of information on NirSoft’s website, but for the lazy, open a command prompt and type nircmd speak text "Hello World" and it will say whatever is between the quotation marks.