PowerShell/Scripts/export-to-serenade.ps1

49 lines
2.0 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)
.PARAMETER TargetFile
2021-10-21 16:26:09 +02:00
Specifies the target file ("$HOME/.serenade/scripts/custom.js" by default)
.EXAMPLE
2021-10-13 10:52:55 +02:00
PS> ./export-to-serenade.ps1 Computer
2021-10-21 16:58:26 +02:00
Exporting 264 scripts with wakeword 'Computer' to C:\Users\Markus/.serenade/scripts/custom.js...
2021-10-12 22:14:33 +02:00
exported 264 PowerShell scripts to Serenade in 22 sec
.NOTES
Author: Markus Fleschutz · License: CC0
.LINK
https://github.com/fleschutz/PowerShell
#>
#requires -version 2
2021-10-21 16:26:09 +02:00
param([string]$WakeWord = "", [string]$FilePattern = "$PSScriptRoot/*.ps1", [string]$TargetFile = "$HOME/.serenade/scripts/custom.js")
try {
$StopWatch = [system.diagnostics.stopwatch]::startNew()
$Scripts = Get-ChildItem "$FilePattern"
2021-10-21 16:58:26 +02:00
"⏳ Exporting $($Scripts.Count) scripts with wakeword `"$WakeWord`" 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-10 10:45:03 +02:00
"" | Add-Content "$TargetFile"
2021-10-13 10:52:55 +02:00
"serenade.global().command(`"$WakeWord $Keyword`", async (api) => {" | Add-Content "$TargetFile"
2021-10-21 16:58:26 +02:00
"await api.focusOrLaunchApplication(`"terminal`"); await api.typeText(`"$ScriptName.ps1`"); await api.pressKey(`"return`");" | Add-Content "$TargetFile"
2021-10-10 10:45:03 +02:00
"});" | Add-Content "$TargetFile"
}
[int]$Elapsed = $StopWatch.Elapsed.TotalSeconds
2021-10-12 22:14:33 +02:00
"✔️ exported $($Scripts.Count) PowerShell scripts to Serenade in $Elapsed sec"
exit 0 # success
} catch {
write-error "⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
exit 1
}