How to connect Cortana commands to custom scripts?

前端 未结 2 1880
栀梦
栀梦 2021-01-30 06:59

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:

相关标签:
2条回答
  • 2021-01-30 07:41

    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".

    0 讨论(0)
  • 2021-01-30 07:46

    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.

    0 讨论(0)
提交回复
热议问题