Functions/StreamDeck/New-StreamDeckAction.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
function New-StreamDeckAction
{
    <#
    .Synopsis
        Creates a StreamDeck action
    .Description
        Creates a StreamDeck action, to be used as part of a profile.
    .Example
        New-StreamDeckAction -HotKey "CTRL+F4" -Title "Close"
    .Example
        New-StreamDeckAction -Uri https://github.com/ -Title "GitHub"
    .Link
        Get-StreamDeckAction
    #>

    [OutputType('StreamDeck.Action')]
    [Diagnostics.CodeAnalysis.SuppressMessageAttribute("Test-ForParameterSetAmbiguity", "", Justification="Ambiguity Desired")]
    [Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSUseShouldProcessForStateChangingFunctions", "", Justification="Does not change state")]
    param(
    # The name of the plugin.
    [Parameter(Mandatory,ParameterSetName='PluginName',ValueFromPipelineByPropertyName)]
    [string]
    $Name,

    # The StreamDeck Plugin UUID.
    [Parameter(ParameterSetName='PluginName',ValueFromPipelineByPropertyName)]
    [string]
    $UUID,

    # A sequence of hotkeys
    [Parameter(Mandatory,ParameterSetName='Hotkey',ValueFromPipelineByPropertyName)]
    [string[]]
    $HotKey,

    # A URI.
    [Parameter(Mandatory,ParameterSetName='OpenUri',ValueFromPipelineByPropertyName)]
    [alias('Url')]
    [uri]
    $Uri,

    # A PowerShell ScriptBlock.
    # Currently, this will run using pwsh by default, and use -WindowsPowerShell if provided.
    [Parameter(Mandatory,ParameterSetName='ScriptBlock',ValueFromPipelineByPropertyName)]
    [ScriptBlock]
    $ScriptBlock,

    # If set, will run the ScriptBlock in Windows PowerShell.
    # By default, will run the Scriptblock in PowerShell core.
    # This option is obviously not supported on MacOS.
    [Parameter(ValueFromPipelineByPropertyName)]
    [switch]
    $WindowsPowerShell,

    # The path to an application.
    [Parameter(Mandatory,ValueFromPipelineByPropertyName,ParameterSetName='ApplicationPath')]
    [string]
    $ApplicationPath,

    # The name of a StreamDeck profile.
    [Parameter(Mandatory,ValueFromPipelineByPropertyName,ParameterSetName='ProfileName')]
    [string]
    $ProfileName,

    # The device UUID, when switching profiles.
    [Parameter(ValueFromPipelineByPropertyName,ParameterSetName='ProfileName')]
    [string]
    $DeviceUUID,

    # The next page. This should be created by New-StreamDeckProfile, passing -IsNextPage
    [Parameter(Mandatory,ValueFromPipelineByPropertyName,ParameterSetName='NextPage')]
    [PSTypeName('StreamDeck.Profile')]
    [PSObject]
    $NextPage,

    # A Child Profile. These should be created by New-StreamDeckProfile, passing -IsChildProfile
    [Parameter(Mandatory,ValueFromPipelineByPropertyName,ParameterSetName='ChildProfile')]
    [PSTypeName('StreamDeck.Profile')]
    [PSObject]
    $ChildProfile,

    # If set, will create an action that will navigate back to the parent folder.
    [Parameter(Mandatory,ValueFromPipelineByPropertyName,ParameterSetName='BackToParent')]
    [switch]
    $BackToParent,

    # If set, will create an action that will navigate back to the previous page.
    [Parameter(Mandatory,ValueFromPipelineByPropertyName,ParameterSetName='PreviousPage')]
    [switch]
    $PreviousPage,

    # The text that should be automatically typed
    [Parameter(Mandatory,ValueFromPipelineByPropertyName,ParameterSetName='Text')]
    [string]
    $Text,

    # If set, will send an enter key after typing the text.
    [Parameter(ValueFromPipelineByPropertyName,ParameterSetName='Text')]
    [Alias('Enter','Return', 'SendReturn')]
    [switch]
    $SendEnter,

    # The settings passed to the plugin.
    [Parameter(ValueFromPipelineByPropertyName)]
    [Alias('Settings')]
    [PSObject]
    $Setting = @{},

    # The title of the action
    [Parameter(ValueFromPipelineByPropertyName)]
    [string]
    $Title,

    # The image used for the action
    [Parameter(ValueFromPipelineByPropertyName)]
    [string]
    $Image,

    # The font size
    [Parameter(ValueFromPipelineByPropertyName)]
    [Alias('FSize')]
    [string]
    $FontSize,

    # The font family
    [Parameter(ValueFromPipelineByPropertyName)]
    [Alias('FFamily')]
    [string]
    $FontFamily,

    # If set, will underline the action title
    [Parameter(ValueFromPipelineByPropertyName)]
    [Alias('FontUnderline')]
    [switch]
    $Underline,

    # The possible states of the plugin.
    [Parameter(ValueFromPipelineByPropertyName)]
    [PSObject[]]
    $States = @(),

    # The state index.
    [Parameter(ValueFromPipelineByPropertyName)]
    [uint32]
    $State = 0,

    # If set, will not show a title.
    [Parameter(ValueFromPipelineByPropertyName)]
    [switch]
    $HideTitle
    )

    begin {
        $streamDeckActions = Get-StreamDeckAction
    }

    process {
            switch ($PSCmdlet.ParameterSetName) {
                PluginName {

                    if (-not $UUID) { # If UUID has not been provided
                        $uuid =
                            $streamDeckActions |
                                Where-Object Name -EQ $name |
                                Select-Object -ExpandProperty UUID # Attempt to find the plugin by name
                        if (-not $uuid) {
                            # If we could not, assume a base name
                            Write-Warning "Could not find UUID for Name '$name', assuming com.elgato.streamdeck.system.$name"
                            $uuid = "com.elgato.streamdeck.system.$name"
                        }
                    }


                    $matchingPlugin =
                        $streamDeckActions | Where-Object UUID -EQ $UUID
                    $uuid = $matchingPlugin.uuid
                }
                ChildProfile {
                    $name = 'Create Folder'
                    $uuid = 'com.elgato.streamdeck.profile.openchild'
                    $Setting = $ChildProfile
                }
                NextPage {
                    $name = 'Next Page'
                    $uuid = 'com.elgato.streamdeck.page.next'
                    $Setting = $NextPage
                }
                PreviousPage {
                    $name = 'Previous'
                    $uuid = 'com.elgato.streamdeck.page.previous'
                }
                BackToParent {
                    $name = 'BackToParent'
                    $uuid = 'com.elgato.streamdeck.profile.backtoparent'
                }
                ProfileName {
                    #region Switch Profile
                    # If switching profiles, find the profile
                    $streamDeckProfile = 
                        if ($ProfileName -as [guid]) {
                            $ProfileName
                        } else {
                            $streamDeckProfile = Get-StreamDeckProfile -Name $ProfileName
                            if (-not $streamDeckProfile) { Write-Error "Could not find profile named '$ProfileName'"; return}
                            $streamDeckProfile.guid
                        }
                    # If we could not, error out.
                    
                    $name = 'Switch Profile'
                    $UUID = 'com.elgato.streamdeck.profile.rotate'
                    $Setting = @{
                        DeviceUUID= $DeviceUUID
                        ProfileUUID = $streamDeckProfile
                    }
                    #endregion Switch Profile
                }
                OpenURI {
                    #region Website
                    $name = 'Website'
                    $uuid = 'com.elgato.streamdeck.system.website'
                    $Setting = [Ordered]@{
                        openInBrowser = $true
                        path = "$Uri"
                    }
                    #endregion Website
                }
                ApplicationPath {
                    #region Application
                    $name = 'open'
                    $uuid = 'com.elgato.streamdeck.system.open'
                    $Setting = [Ordered]@{
                        openInBrowser =  $true
                        path = $ApplicationPath
                    }
                    #endregion Application
                }
                Text {
                    #region Text
                    $name ='text'
                    $uuid = 'com.elgato.streamdeck.system.text'
                    $setting = @{
                        isSendingEnter = ($SendEnter -as [bool])
                        pastedText = $Text
                    }
                    #region Text
                }
                HotKey {
                    #region HotKey
                    $name = 'HotKey'
                    $uuid = 'com.elgato.streamdeck.system.hotkey'
                    # Hotkeys are tricky. First we need a RegEx to get the sequence parts out.
                    $HotKeyRegex = [Regex]::new(@'
(?<Modifier>
    (?>
        (?<Control>Control|Ctrl|LeftCtrl|RightCtrl)
        |
        (?<Command>Command|Cmd|LeftCmd|RightCmd|Windows|Win|Apple|OpenApple)
        |
        (?<Alt>Option|Alt|LeftAlt|RightAlt)
        |
        (?<Shift>Shift|LeftShift|RightShift)
    )
\+){0,3}
(?<Key>.+$)
'@
, 'IgnoreCase,IgnorePatternWhitespace')
                    $Setting =
                        [Ordered]@{
                            Hotkeys = @(
                                foreach ($hot in $HotKey) { # Then we need to walk over each -HotKey
                                    $matched =  $HotKeyRegex.Match($Hot)

                                    # Find what matched
                                    $keyCmd = $matched.Groups["Command"].Success
                                    $keyControl = $matched.Groups["Control"].Success
                                    $keyOption = $matched.Groups["Alt"].Success
                                    $keyShift = $matched.Groups["Shift"].Success
                                    $modifiers = 0
                                    # Set the right modifiers
                                    if ($keyShift) { $modifiers = $modifiers -bor 1 }
                                    if ($keyControl) { $modifiers = $modifiers -bor 2 }
                                    if ($keyOption ) { $modifiers = $modifiers -bor 4 }
                                    if ($keyCmd) { $modifiers = $modifiers -bor 8 }
                                    $nativeCode = $vKey = $qtKey = # and map the key.
                                        if ($matched.Groups["Key"].Length -eq 1) {
                                            # If it's a single char, it's
                                            # easy mode, turn the key into a char to get the v-key.
                                            [char]$matched.Groups["Key"].Value
                                        } else {
                                            # Otherwise, it's lookup table time.
                                            $matchedKey = $matched.Groups["Key"].Value
                                            switch -Regex ($matchedKey)  {
                                                'F(?>[1-2][0-9]|[0-9])' { # F1-F24
                                                    [int]($matchedKey -replace 'F') + 0x6f
                                                }
                                                '(?>#|Num|NumPad)[0-9]' { # Numpad
                                                    [int]($matchedKey -replace '\D') + 0x60
                                                }
                                                'Back|Backspace' {
                                                    0x8
                                                }
                                                Tab { 0x9 }
                                                Clear { 0xc }
                                                End {0x23}
                                                Home {0x24}
                                                Left {0x25}
                                                Up { 0x26}
                                                Right { 0x27}
                                                Down { 0x28}
                                                Select { 0x29}
                                                Print { 0x2a}
                                                Execute { 0x2b}
                                                'Insert|Ins' { 0x2d}
                                                'Delete|Del' { 0x2e}
                                                Help { 0x2f}
                                                'Space|Spacebar|\s' { 0x20}
                                                'Prior|PageUp' {0x21}
                                                'Next|PageDown' {0x22}
                                                'Enter|Return' { 0xd}
                                                'Caps|CapsLock' { 0x14}
                                                'Kana|Hangul|Hanguel'{ 0x15}
                                                'Junja' { 0x17}
                                                'Final' { 0x18}
                                                'Hanja|Kanji' {0x19}
                                                Sleep {0x5f}
                                                Multiply { 0x6a}
                                                Add { 0x6b}
                                                Subtract { 0x6d}
                                                Decimal { 0x6e}
                                                Divide { 0x6f}
                                                'Num|NumLock' {0x90}
                                                BrowserBack {0xa6}
                                                BrowserForward {0xa7}
                                                BrowserRefresh {0xa8}
                                                BrowserStop {0xa9}
                                                BrowserSearch { 0xaa}
                                                BrowserHome {0xac}
                                                'Mute|VolumeMute|MediaMute' {0xad}
                                                'VolumeDown|MediaDown' {0xae}
                                                'VolumeUp|MediaUp' {0xaf}
                                                'NextTrack|MediaNextTrack' { 0xb0}
                                                'PreviousTrack|MediaPreviousTrack' { 0xb1}
                                                'Stop|MediaStop' { 0xb2 }
                                                'Play|Pause|MediaPlayPause|TogglePlay' {0xb3}
                                                'Mail|LaunchMail' {0xb4}
                                                '\+|Plus|OEMPlus' {0xbb}
                                                ',|Comma|OEMComma' {0xbc}
                                                '\-|Minus|OEMMinus' {0xbd}
                                                '\.|Period|OEMPeriod' {0xbe}
                                            }
                                        }

                                    # If we could not find a match, error and return
                                    if (-not $nativeCode) { Write-Error "Could not map key code for HotKey '$hot'"; return}

                                    # Otherwise, create the type of object StreamDeck will want.
                                    [PSCustomObject][Ordered]@{
                                        KeyCmd = $keyCmd
                                        KeyCtrl = $keyControl
                                        KeyModifiers = $modifiers
                                        KeyOption = $keyOption
                                        KeyShift = $keyShift
                                        NativeCode = $nativeCode -as [int]
                                        QTKeyCode = $qtKey -as [int]
                                        VKeyCode = $vKey -as [int]
                                    }
                                }
                                # All hotkeys must be followed by this "non-key".
                                [PSCustomObject][Ordered]@{
                                    KeyCmd = $false
                                    KeyCtrl = $false
                                    KeyModifiers = 0
                                    KeyOption = $false
                                    KeyShift = $false
                                    NativeCode = 146
                                    QTKeyCode = 33554431
                                    VKeyCode = -1
                                }
                            )
                        }
                    #region HotKey
                }

                ScriptBlock {
                    #region ScriptBlock
                    $name = 'Open'
                    $uuid = 'com.elgato.streamdeck.system.open'

                    $cmdSequence =
                        @(
                        if ($WindowsPowerShell) {
                            'powershell'
                        }
                        else {
                            'pwsh'
                        }
                        '-noexit'
                        '-noprofile'
                        '-nologo'
                        '-encodedCommand'
                        [Convert]::ToBase64String([Text.Encoding]::Unicode.GetBytes($ScriptBlock.ToString()))
                        ) -join ' '

                    $Setting = [Ordered]@{
                        openInBrowser = $true
                        path = $cmdSequence
                    }
                    #endregion ScriptBlock
                }
            }


        if (-not $States) {
            $States =
                @(if ($matchingPlugin.states) {
                    foreach ($s in $matchingPlugin.states) { # If the plugin had a default state
                        $sc = [Ordered]@{} # copy it so we don't change that data.
                        foreach ($p in $s.psobject.properties) {
                            $sc[$p.Name] = $p.Value
                        }
                        [PSCustomObject]$sc
                    }
                } else {
                    [PSCustomObject][Ordered]@{ # Otherwise, create a state object based off of built-in parameters
                        FFamily= $fontFamily
                        FSize = $fontSize
                        FUnderline = $Underline -as [bool]
                        Image = $Image
                        Title = $title
                        TitleAlignment = $titleAlignment
                        TitleColor = $titleColor
                        TitleShow = (-not $HideTitle)
                    }
                })
        }
        foreach ($s in $States) {
            if ($Image) { $s | Add-Member NoteProperty Image $Image -Force }
            if ($Title) { $s | Add-Member NoteProperty Title $Title -Force }
            if ($FontSize) { $s | Add-Member NotePropety FSize $FontSize -Force}
            if ($Underline) { $s | Add-Member NotePropety FUnderline ($Underline -as [bool]) -Force }
            if ($FontFamily) { $s | Add-Member NoteProperty FFamily $FontFamily -Force}
            if ($titleColor) { $s | Add-Member NoteProperty TitleColor $titleColor -Force }
            if ($titleAlignment) { $s | Add-Member NoteProperty TitleAlignment $titleAlignment -Force }
            if ($HideTitle) { $s | Add-Member TitleShow (-not $HideTitle) -Force  }
        }


        [PSCustomobject]([Ordered]@{
            Name = $Name
            Settings = $Setting
            State = $State
            States = @($States)            
            UUID = $UUID
            PSTypeName = 'StreamDeck.Action'
        })
    }
}