How to connect Cortana commands to custom scripts?

Charles Clayton picture Charles Clayton · May 25, 2015 · Viewed 34.5k times · Source

This may be a little early to ask this, but I'm running Windows 10 Technical Preview Build 10122. I'd like to set up Cortana to have custom commands. Here's how she works:

Hey Cortana, <she'll listen and process this command>

Microsoft will process the command and if there isn't anything for it, she'll just search the input on bing. However, I'd like to be able to say something like, just for example

Hey Cortana, I'm going to bed now

And have the input I'm going to bed now trigger run a batch script, a VBScript, a command, or any some sort some of custom response that basically does the following.

C:\> shutdown -s

Is there a way to set up a predefined custom commands for Cortana?

Update:

I created this basic YouTube tutorial and this more advanced one with a corresponding GitHub repo based on talkitbr's excellent and very helpful answer below.

At first his answer was beyond my understanding so I decided to break it down in a bit more detail for future users like myself.

Answer

talkitbr picture talkitbr · Jul 13, 2015

You can create commands for Cortana to listen for. These commands need to be described in a XML file called Voice Command Definitions or VCD.

Here's an example:

<?xml version="1.0" encoding="utf-8" ?>
<VoiceCommands xmlns="http://schemas.microsoft.com/voicecommands/1.2">
    <CommandSet xml:lang="en-us" Name="HomeControlCommandSet_en-us">
        <CommandPrefix>HomeControl</CommandPrefix>
        <Example>Control alarm, temperature, light and others</Example>

        <Command Name="Activate_Alarm">
            <Example>Activate alarm</Example>
            <ListenFor>[Would] [you] [please] activate [the] alarm [please]</ListenFor>
            <ListenFor RequireAppName="BeforeOrAfterPhrase">Activate alarm</ListenFor>
            <ListenFor RequireAppName="ExplicitlySpecified">Activate {builtin:AppName} alarm</ListenFor>
            <Feedback>Activating alarm</Feedback>
            <Navigate />
        </Command>
        ...
    </CommandSet>
</VoiceCommands>

After create this definition, you need to register it at App Startup:

protected async override void OnLaunched(LaunchActivatedEventArgs e)
{
    ...
    // Install the VCD
    try
    {
        StorageFile vcdStorageFile = await Package.Current.InstalledLocation.GetFileAsync(@"HomeControlCommands.xml");
        await VoiceCommandDefinitionManager.InstallCommandDefinitionsFromStorageFileAsync(vcdStorageFile);
    }
    catch (Exception ex)
    {
        System.Diagnostics.Debug.WriteLine("There was an error registering the Voice Command Definitions", ex);
    }
}

An then override the App.OnActivated method to handle when the events are triggered:

protected override void OnActivated(IActivatedEventArgs e)
{
    // Handle when app is launched by Cortana
    if (e.Kind == ActivationKind.VoiceCommand)
    {
        VoiceCommandActivatedEventArgs commandArgs = e as VoiceCommandActivatedEventArgs;
        SpeechRecognitionResult speechRecognitionResult = commandArgs.Result;

        string voiceCommandName = speechRecognitionResult.RulePath[0];
        string textSpoken = speechRecognitionResult.Text;
        IReadOnlyList<string> recognizedVoiceCommandPhrases;

        System.Diagnostics.Debug.WriteLine("voiceCommandName: " + voiceCommandName);
        System.Diagnostics.Debug.WriteLine("textSpoken: " + textSpoken);

        switch (voiceCommandName)
        {
            case "Activate_Alarm":
                System.Diagnostics.Debug.WriteLine("Activate_Alarm command");
                break;

The tutorial shows the complete code.

After you do all of this, you can call your batch scripts using ProcessStartInfo or System.Diagnostics.Process.Start.

Also, if you are interested in responding to the user through Cortana window, check this post regarding Cortana in background.