PowerShell/Scripts/export-to-serenade.ps1

48 lines
1.9 KiB
PowerShell
Raw Normal View History

<#
.SYNOPSIS
Exports all scripts to Serenade for voice control
.DESCRIPTION
2021-10-13 10:52:55 +02:00
This script exports all PowerShell scripts to Serenade to execute them by voice.
.PARAMETER WakeWord
Specifies the wakeword (none by default)
.PARAMETER FilePattern
Specifies the file pattern for the scripts ("$PSScriptRoot/*.ps1" by default)
2021-10-24 20:13:47 +02:00
.PARAMETER Application
Specifies the application to be used
2021-10-13 10:52:55 +02:00
.PARAMETER TargetFile
2021-10-23 10:47:18 +02:00
Specifies the target file ("$HOME/.serenade/scripts/PowerShell.js" by default)
.EXAMPLE
2021-10-13 10:52:55 +02:00
PS> ./export-to-serenade.ps1 Computer
2021-10-23 10:47:18 +02:00
Exporting 264 PowerShell scripts to C:\Users\Markus/.serenade/scripts/PowerShell.js...
2021-10-21 17:15:48 +02:00
exported 264 scripts with wakework "Computer" to Serenade in 22 sec
.NOTES
Author: Markus Fleschutz · License: CC0
.LINK
https://github.com/fleschutz/PowerShell
#>
#requires -version 2
2021-10-24 20:13:47 +02:00
param([string]$WakeWord = "", [string]$FilePattern = "$PSScriptRoot/*.ps1", [string]$Application = "terminal", [string]$TargetFile = "$HOME/.serenade/scripts/PowerShell.js")
try {
$StopWatch = [system.diagnostics.stopwatch]::startNew()
$Scripts = Get-ChildItem "$FilePattern"
2021-10-23 10:47:18 +02:00
"⏳ Found $($Scripts.Count) scripts, exporting them to $TargetFile..."
2021-10-13 10:52:55 +02:00
"/* NOTE: This file has been generated automatically by export-to-serenade.ps1 */" | Set-Content "$TargetFile"
foreach ($Script in $Scripts) {
$ScriptName = $Script.basename
2021-10-10 10:13:53 +02:00
$Keyword = $ScriptName -replace "-"," "
2021-10-24 20:13:47 +02:00
"serenade.global().command(`"$($WakeWord.toLower()) $Keyword`", async (api) => { await api.focusOrLaunchApplication(`"$Application`"); await api.typeText(`"$ScriptName.ps1`"); await api.pressKey(`"return`"); });" | Add-Content "$TargetFile"
}
[int]$Elapsed = $StopWatch.Elapsed.TotalSeconds
2021-10-23 10:47:18 +02:00
"✔️ exported $($Scripts.Count) PowerShell scripts with wakeword `"$WakeWord`" to Serenade in $Elapsed sec"
exit 0 # success
} catch {
write-error "⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
exit 1
}