PowerShell/Scripts/export-to-serenade.ps1

50 lines
1.9 KiB
PowerShell
Raw Normal View History

<#
.SYNOPSIS
Exports scripts to Serenade
.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 wake word (none by default)
2021-10-13 10:52:55 +02:00
.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
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-12-01 07:48:52 +01:00
Found 534 PowerShell scripts...
Writing custom JavaScript file: C:\Users\Markus\.serenade\scripts\PowerShell.js...
2021-11-29 07:59:13 +01:00
Exported to Serenade with wake word "Computer" 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"
2021-11-23 07:53:19 +01:00
"⏳ Found $($Scripts.Count) PowerShell scripts..."
2021-12-01 07:48:52 +01:00
"⏳ Writing custom JavaScript file: $TargetFile..."
2021-11-29 15:20:48 +01:00
"/* DO NOT EDIT! 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-12-11 17:44:30 +01:00
"serenade.global().command(`"$($WakeWord.toLower()) $Keyword`",async(api)=>{await api.focusApplication(`"$Application`");await api.pressKey(`"return`");await api.typeText(`"$ScriptName.ps1`");await api.pressKey(`"return`");});" | Add-Content "$TargetFile"
}
[int]$Elapsed = $StopWatch.Elapsed.TotalSeconds
2021-11-29 07:59:13 +01:00
"✔️ Exported to Serenade with wake word `"$WakeWord`" in $Elapsed sec"
exit 0 # success
} catch {
write-error "⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
exit 1
}