How to connect Cortana commands to custom scripts?
Asked Answered
I

2

41

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.

Immorality answered 25/5, 2015 at 5:21 Comment(2)
Where is your code? what have you allready trying to do?Steinberg
Just have a look at this page. You're going to need to know some .NET language and have a small bit of XML skills. Doing this all from batch isn't going to work and I don't think VBScript has a cortana api yet. Refer to #27671196 for a similiar answer.Concave
C
43

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 [web archive].

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 [web archive].

Comparable answered 13/7, 2015 at 15:42 Comment(3)
Your tutorial is great! Thank you! One question, I'm trying to modify the commands, but any updates I make to the VCD xml file do not get updated in Cortana. Any suggestions?Adaminah
@Adaminah After you modify the VCD file, are you recompiling, deploying and opening the app again? The App.OnLaunched need to be called because it's responsible for InstallCommandDefinitionsFromStorageFileAsync. Let me known if it works!Comparable
Yah, that works. I realized that I have made a mistake in the xml config. Thanks!Adaminah
E
1

What you can do is write a .bat file and add a shortcut to the file to the Folder: C:\ProgramData\Microsoft\Windows\Start Menu\Programs you can name the shortcut whatever you want and trigger the shutdown by saying: "Hey Cortana open/start [shortcut name]". Make sure that Cortana only listens to you to not get "pranked".

Elea answered 9/10, 2015 at 3:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.