PowerShell/Scripts/export-to-serenade.ps1
2021-11-23 07:57:58 +01:00

50 lines
1.9 KiB
PowerShell
Executable File

<#
.SYNOPSIS
Exports all scripts to Serenade
.DESCRIPTION
This script exports all PowerShell scripts to Serenade to execute them by voice.
.PARAMETER WakeWord
Specifies the wake word (none by default)
.PARAMETER FilePattern
Specifies the file pattern for the scripts ("$PSScriptRoot/*.ps1" by default)
.PARAMETER Application
Specifies the application to be used
.PARAMETER TargetFile
Specifies the target file ("$HOME\.serenade\scripts\PowerShell.js" by default)
.EXAMPLE
PS> ./export-to-serenade.ps1 Computer
⏳ Found 499 PowerShell scripts...
⏳ Writing to custom JavaScript file C:\Users\Markus\.serenade\scripts\PowerShell.js...
✔️ export to Serenade with wake word "Computer" finished in 3 sec
.NOTES
Author: Markus Fleschutz · License: CC0
.LINK
https://github.com/fleschutz/PowerShell
#>
#requires -version 2
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"
"⏳ Found $($Scripts.Count) PowerShell scripts..."
"⏳ Writing to custom JavaScript file $TargetFile..."
"/* NOTE: This file has been generated automatically by export-to-serenade.ps1 */" | Set-Content "$TargetFile"
foreach ($Script in $Scripts) {
$ScriptName = $Script.basename
$Keyword = $ScriptName -replace "-"," "
"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
"✔️ export to Serenade with wake word `"$WakeWord`" finished in $Elapsed sec"
exit 0 # success
} catch {
write-error "⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
exit 1
}