-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSet random Wallpaper.ps1
More file actions
725 lines (577 loc) · 24.5 KB
/
Copy pathSet random Wallpaper.ps1
File metadata and controls
725 lines (577 loc) · 24.5 KB
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
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
param(
[switch]$UpdateList,
[switch]$SetWallpapersPath,
[switch]$ForgetWallpapersPath,
[switch]$All,
[string]$IncludeTags,
[string]$ExcludeTags,
[switch]$Show,
[switch]$NoTags,
[switch]$Help,
[switch]$Restart,
[switch]$Man,
[switch]$Debug
)
# Stop the script if an error occurs.
$ErrorActionPreference = 'Stop'
if ($Debug) {
$DebugPreference = 'Continue'
}
if ($PSVersionTable.PSVersion.Major -lt 7) {
Write-Host "This script requires PowerShell 7 or later." -Red
Write-Host "Run it in a pwsh terminal environment." -Red
Write-Host "If not installed, install 'Microsoft.PowerShell' via Winget or Uniget." -Red
exit 1
}
<#*==========================================================================
* ℹ PARAMETERS
Run the script with the -help parameter to know how to use it
===========================================================================#>
<#*==========================================================================
* ℹ DEFAULT VARIABLES
===========================================================================#>
$OriginalWorkingDir = Get-Location
Set-Location $PSScriptRoot
$WorkingDir = Get-Location
New-Variable -Name ListFile -Value "Wallpapers list.ps1" -Option Constant
New-Variable -Name ListPath -Value $(Join-Path -Path $WorkingDir -ChildPath $ListFile) -Option Constant
New-Variable -Name EnvPathName -Value "WallpapersPath" -Option Constant
New-Variable -Name EnvPrevName -Value "WallpaperPrevious" -Option Constant
New-Variable -Name WallpapersFolder1 -Value "wallpapers" -Option Constant
New-Variable -Name WallpapersFolder2 -Value "SaveData/wptmp" -Option Constant
New-Variable -Name LibraryPathFallback -Value "$env:LOCALAPPDATA/Lively Wallpaper/Library" -Option Constant
New-Variable -Name LivelyBin -Value "$env:ProgramFiles/Lively Wallpaper/Lively.exe"
New-Variable -Name repo -Value "https://github.com/Fred-Vatin/smart-random-wallpaper/wiki" -Option Constant
<#*==========================================================================
* ℹ GLOBAL INIT
===========================================================================#>
# If you set a custom path with -SetWallpapersPath, get it
$LibraryPath = [System.Environment]::GetEnvironmentVariable($EnvPathName, "User")
if (-not ($LibraryPath)) {
$LibraryPath = $WorkingDir
if (-not (Test-Path $LibraryPath -PathType Container)) {
$LibraryPath = $LibraryPathFallback
}
if (-not (Test-Path $LibraryPath -PathType Container)) {
Write-Host "LibraryPath: `"$LibraryPath`" does not exit. Run `"-Help`" or `"-Man`" or `"-Debug`" to know more." -ForegroundColor Red
}
}
$WallpapersPath1 = (Join-Path -Path $LibraryPath -ChildPath $WallpapersFolder1)
$WallpapersPath2 = (Join-Path -Path $LibraryPath -ChildPath $WallpapersFolder2)
if (-not (Test-Path $WallpapersPath1 -PathType Container)) {
Write-Host "WallpapersPath1: `"$WallpapersPath1`" does not exit. Run `"-Help`" or `"-Man`" or `"-Debug`" to know more." -ForegroundColor Red
}
$WallpapersPaths = @(
$WallpapersPath1
$WallpapersPath2
)
# If lively is not installed in program files, try to find the CLI tool
if (-not (Test-Path -Path $LivelyBin -PathType Leaf)) {
if (Get-Command Livelycu -ErrorAction SilentlyContinue) {
$LivelyBin = "Livelycu"
}
else {
Write-Host "`nERROR: LIVELY BINARY NOT FOUND" -ForegroundColor Red
Write-Host "`t- `"Lively.exe`" not found in `"%ProgramFiles%\Lively Wallpaper`"" -ForegroundColor Red
Write-Host "`t- `"Livelycu`" CLI tool not found`n" -ForegroundColor Red
Write-Host "If Lively is already installed, you need to install the CLI tool." -ForegroundColor Red
Read-Host "Press [Enter] to open the wiki on your browser and learn how to do it..."
Start-Process "$repo"
Set-Location $OriginalWorkingDir
exit
}
}
Write-Debug "LibraryPath: $LibraryPath"
Write-Debug "WallpapersPath1: $WallpapersPath1"
Write-Debug "WallpapersPath2: $WallpapersPath2"
Write-Debug "ListPath: $ListPath"
Write-Debug "LivelyBin: $LivelyBin"
<#*==========================================================================
* ℹ FUNCTIONS
===========================================================================#>
# Display help message with specified formatting
function Show-Help {
Write-Host "`nℹ`tPARAMETERS" -ForegroundColor Magenta
Write-Host "========================`n" -ForegroundColor Magenta
Write-Host "-Help" -ForegroundColor Magenta
Write-Host "`tDisplays this help (default)`n"
Write-Host "-Man" -ForegroundColor Magenta
Write-Host "`tUse this to open wiki at `"$repo`"`n"
Write-Host "-UpdateList" -ForegroundColor Magenta
Write-Host "`tCreate or update the wallpapers list and save it to: "
Write-Host "`t$ListPath`n" -ForegroundColor Cyan
Write-Host "`tThis function is automatically called if the list is not found when using other parameters below." -ForegroundColor DarkGray
Write-Host "`tUse it to force an update of the list.`n" -ForegroundColor DarkGray
Write-Host "-Restart" -ForegroundColor Magenta
Write-Host "`tRestart Lively. Useful if wallpaper is bugging or crashes.`n"
Write-Host "Here are the parameters to set a random wallpaper using tags." -ForegroundColor Yellow
Write-Host "You can use both -IncludeTags and -ExcludeTags at the same time." -ForegroundColor Yellow
Write-Host "But -IncludeTags must come first." -ForegroundColor Yellow
Write-Host "-All" -ForegroundColor Magenta
Write-Host "`tIt will set a wallpaper using any wallpapers in `"$ListFile`"`n"
Write-Host "-IncludeTags <String>" -ForegroundColor Magenta
Write-Host "`tPass one or several tags as word like `"day clock`""
Write-Host "`tOnly wallpapers having those tags in description will be used`n"
Write-Host "-ExcludeTags <String>" -ForegroundColor Magenta
Write-Host "`tPass one or several tags as word like `"day clock`""
Write-Host "`tWallpapers having those tags in description will NOT be used even if they are in -IncludeTags`n"
Write-Host "You can add parameters below after -All or -IncludeTags or -ExcludeTags" -ForegroundColor Yellow
Write-Host "-Show" -ForegroundColor Magenta
Write-Host "`tDisplay the list returned by tags filter in a table in the console."
Write-Host "`tNo wallpaper is set when using this parameter.`n"
Write-Host "`tSpecial: use " -ForegroundColor Red -NoNewline
Write-Host "-All -Show -NoTags" -ForegroundColor Yellow
Write-Host "`tto display the list of wallpapers that don’t have tags set."
Write-Host "`tNo wallpaper is set when using these parameters.`n"
Write-Host "`nℹ`tDefault Paths" -ForegroundColor Magenta
Write-Host "========================`n" -ForegroundColor Magenta
Write-Host "-SetWallpapersPath" -ForegroundColor Magenta
Write-Host "`tUse this parameter to run an assistant to help you to define"
Write-Host "`tthe custom lively wallpapers path to use."
Write-Host "`tIt will be saved in the environment variable named `"$EnvPathName`"."
Write-Host "`tBy default, this script use the working directory (the same path as this script)."
Write-Host "`tThe path expects some sub-directories: `"wallpapers`" and `"SaveData\wptmp`"."
Write-Host "`tIf not found it tries to use the default path: `"$LibraryPathFallback`"."
Write-Host "`tCurrent value: " -NoNewline
Write-Host "$LibraryPath`n" -ForegroundColor Cyan
Write-Host "-ForgetWallpapersPath" -ForegroundColor Magenta
Write-Host "`tUse this parameter to delete the " -NoNewline
Write-Host "$EnvPathName " -ForegroundColor Cyan -NoNewline
Write-Host "environment variable."
Write-Host "`tThen this script will use the current working directory `"$WorkingDir`"`n"
Write-Host "`tOr `"$LibraryPathFallback`" if not found.`n"
Write-Host "ListPath" -ForegroundColor Yellow
Write-Host "`tThis is where the `"$ListFile`" is saved." -ForegroundColor DarkGray
Write-Host "`tBy default in the same path as this script." -ForegroundColor DarkGray
Write-Host "`tCurrent value: " -NoNewline
Write-Host "$ListPath`n" -ForegroundColor Cyan
Write-Host "`tEdit this script to customize this path if you really need it."
Write-Host "Binary path" -ForegroundColor Yellow
Write-Host "`tThis is the install path of Lively.exe or the Livelycu" -ForegroundColor DarkGray
Write-Host "`tBy default it searches for `"$env:ProgramFiles/Lively Wallpaper/Lively.exe`"" -ForegroundColor DarkGray
Write-Host "`tIf not found, it searches if Livelycu CLI tool is available in environment variable path" -ForegroundColor DarkGray
Write-Host "`tBinary found and in use: " -NoNewline
Write-Host "$LivelyBin`n" -ForegroundColor Cyan
}
function TerminateWithError {
param(
[string]$errorMessage = "Error happened.`nEXIT",
[System.Exception]$exception
)
[console]::beep(1000, 100)
[console]::beep(1000, 100)
[console]::beep(1000, 100)
[console]::beep(1000, 1000)
if ($exception) {
$line = $_.InvocationInfo.ScriptLineNumber
if ($line) {
Write-Host "`n$errorMessage :`n`t$($exception.Message)`n`tLine: $line`nEXIT" -ForegroundColor Red
}
else {
Write-Host "`n$errorMessage :`n$($exception.Message)`nEXIT" -ForegroundColor Red
}
}
else {
Write-Host "ERROR`n" -ForegroundColor Red
Write-Host " $errorMessage`n`nEXIT" -ForegroundColor Red
}
Set-Location $OriginalWorkingDir
exit 1
}
function WriteTitle {
param(
[Parameter(Mandatory = $true)]
[string]$title
)
Write-Host "`n===== $title =====`n" -ForegroundColor Magenta
}
function Test-CLI-Installation {
if (-not (Get-Command Livelycu -ErrorAction SilentlyContinue)) {
TerminateWithError -errorMessage "`"Livelycu.exe`" is not installed globally on the system. Download it and add it to the user PATH."
}
}
function SetPath {
Write-Host "`nDo NOT need to use double quotes if your path contains spaces." -ForegroundColor Yellow
do {
$path = Read-Host -Prompt 'Please enter a path (or press Enter to cancel)'
if (-not $path) {
Write-Host 'Operation cancelled.' -ForegroundColor Yellow
return $null
}
if (-not (Test-Path -Path $path -PathType Container)) {
Write-Host "`nError : the path " -ForegroundColor Red
Write-Host "$path" -ForegroundColor cyan
Write-Host "doesn’t exist. Try again or press Ctrl+C to cancel." -ForegroundColor Red
$isValidPath = $false
}
else {
$isValidPath = $true
}
} until ($isValidPath)
try {
$null = New-ItemProperty -Path "HKCU:\Environment" -Name $EnvPathName -Value $path -PropertyType String -Force
}
catch {
TerminateWithError -errorMessage "The path failed to be registered as a user environment variable."
}
$LibraryPath = [System.Environment]::GetEnvironmentVariable($EnvPathName, "User")
# Clear-Host
Write-Host "`nLibrary path: " -ForegroundColor Green -NoNewline
Write-Host "$LibraryPath" -ForegroundColor Cyan
Write-Host "saved in the user environment variable named " -ForegroundColor Green -NoNewline
Write-Host "$EnvPathName" -ForegroundColor Cyan -NoNewline
Write-Host "`nThis will take effect in a new pwsh session though." -ForegroundColor Yellow
# Retourne le chemin.
return $LibraryPath
}
function UpdateList {
$Output = '$Wallpapers = @()' + [Environment]::NewLine
foreach ($path in $WallpapersPaths) {
Write-Debug "Path: $path"
if (Test-Path -Path $path -PathType Container) {
# Browse each Wallpapers child folder (non-recursively)
Get-ChildItem -Path $path -Directory | ForEach-Object {
$CurrentFolder = $_.FullName
# Path to the LivelyInfo.json file in the current folder
$jsonFile = Join-Path -Path $_.FullName -ChildPath 'LivelyInfo.json'
if (Test-Path -Path $jsonFile -PathType Leaf) {
try {
# Read JSON file
$jsonContent = Get-Content -Path $jsonFile -Raw | ConvertFrom-Json
# Get wallpaper title
$Title = $jsonContent.Title
# Get wallpaper tags
$Tags = $jsonContent.Desc
if ($Tags) {
$Tags = ($Tags | Select-String -Pattern '#\w+' -AllMatches).Matches.Value -join ' '
$Tags = $Tags.Replace("#", "")
}
if ($Title) {
# If title is like "The 80's", replace with "The 80''s" to avoid parsing error later
$Title = $Title.Replace("'", "''")
$Title = $Title.Replace("’", "''")
$Title = $Title.Replace('"', "''")
# Add formated text to $Output
$Output += "`$Wallpapers += New-Object -TypeName psobject -Property @{" + [Environment]::NewLine
$Output += " 'Folder' = '$CurrentFolder'" + [Environment]::NewLine
$Output += " 'Title' = '$Title'" + [Environment]::NewLine
if ($Tags) {
$Output += " 'Tags' = '$Tags'" + [Environment]::NewLine
}
$Output += "}" + [Environment]::NewLine
}
}
catch {
Write-Warning "Error reading file `"LivelyInfo.json`" in $CurrentFolder : $_"
}
}
}
}
else {
Write-Warning "`"$path`" folder doesn’t exist in: "
Write-Host "$LibraryPath" -ForegroundColor Cyan
Set-Location $OriginalWorkingDir
}
}
try {
Write-Output ""
$Output | Out-File -FilePath $ListPath -Encoding UTF8 -ErrorAction Stop
Write-Host "`"$ListFile`" sucessfully updated in:" -ForegroundColor Green
Write-Host "$ListPath`n" -ForegroundColor Cyan
}
catch {
TerminateWithError -errorMessage "Failed to create or write in `"$ListPath`""
}
}
function Pause {
Read-Host "Paused. Press Enter to continue..."
}
# Usage example
# $proc = Get-Process -Name "notepad"
# Stop-ProcessTree -ParentId $proc.Id
function Stop-ProcessTree {
param(
[Parameter(Mandatory)]
[int]$ParentId
)
# Find all children recursively
$children = Get-CimInstance Win32_Process | Where-Object { $_.ParentProcessId -eq $ParentId }
foreach ($child in $children) {
Stop-ProcessTree -ParentId $child.ProcessId
}
# Finally stop the parent
Stop-Process -Id $ParentId -Force -ErrorAction SilentlyContinue
}
$ImportList = {
if (-not (Test-Path -Path $ListPath -PathType Leaf)) {
Write-Host "`nAuto update list because not found in:" -ForegroundColor Yellow
Write-Host "$ListPath`n" -ForegroundColor Cyan
UpdateList
}
. "$ListPath"
if (-not $Wallpapers) {
TerminateWithError -errorMessage "No valid wallpapers found to filter in: `"$ListPath`". Try to open a new pwsh session and use the -UpdateList parameter."
}
}
$SetRandom = {
param($List = $Wallpapers)
$WallpaperPrevious = [System.Environment]::GetEnvironmentVariable($EnvPrevName, "User")
do {
$randomWallpaper = $List | Get-Random
if ($randomWallpaper.Folder -eq $WallpaperPrevious) {
$isNew = $false
}
else {
$isNew = $true
}
} until (
$isNew
)
try {
$null = New-ItemProperty -Path "HKCU:\Environment" -Name $EnvPrevName -Value $randomWallpaper.Folder -PropertyType String -Force
}
catch {
TerminateWithError -errorMessage "`"$($randomWallpaper.Folder)`" failed to be registered as the user environment variable `"$EnvPrevName`""
}
if (-not (Test-Path $randomWallpaper.Folder -PathType Container)) {
TerminateWithError -errorMessage "`"$($randomWallpaper.Folder)`" does not exist. You need to use -UpdateList."
}
& $LivelyBin setwp --file "$($randomWallpaper.Folder)"
Write-Debug "$LivelyBin setwp --file `"$($randomWallpaper.Folder)`""
Write-Host "`nNew wallapaper set: " -NoNewline
Write-Host "$($randomWallpaper.Title)" -ForegroundColor Green -NoNewline
Write-Host "`ttagged: " -NoNewline
Write-Host "$($randomWallpaper.Tags)" -ForegroundColor Green -NoNewline
}
$Exclude = {
param($List = $Wallpapers)
$RequiredTags = $ExcludeTags.Trim() -split '\s+'
$FilteredList = $Wallpapers | Where-Object {
$tags = $_.Tags -split '\s+'
($RequiredTags | Where-Object { $tags -contains $_ }).Count -eq 0
}
}
$TagsListIn = {
if ($IncludeTags) {
Write-Host "Including tags: " -NoNewline
Write-Host "$IncludeTags" -ForegroundColor Green
}
}
$TagsListOut = {
if ($ExcludeTags) {
Write-Host "Excluding tags: " -NoNewline
Write-Host "$ExcludeTags" -ForegroundColor Red
}
}
<#*==========================================================================
* ℹ HANDLE HELP PARAMETER
===========================================================================#>
if ((-not $UpdateList -and -not $All -and -not $Restart -and -not $Man -and -not $SetWallpapersPath -and -not $ForgetWallpapersPath -and -not $IncludeTags -and -not $ExcludeTags -and -not $NoTags -and -not $Show -and -not $Help -and -not $Man) -or ($Help)) {
Show-Help
Set-Location $OriginalWorkingDir
exit
}
<#*==========================================================================
* ℹ HANDLE MAN PARAMETER
===========================================================================#>
if ($man) {
Start-Process "$repo"
Set-Location $OriginalWorkingDir
exit
}
<#*==========================================================================
* ℹ HANDLE -UpdateList PARAMETER
===========================================================================#>
if ($UpdateList) {
UpdateList
Set-Location $OriginalWorkingDir
exit
}
<#*==========================================================================
* ℹ HANDLE -SetWallpapersPath PARAMETER
===========================================================================#>
if ($SetWallpapersPath) {
$Path = SetPath
Set-Location $OriginalWorkingDir
exit
}
<#*==========================================================================
* ℹ HANDLE -ForgetWallpapersPath PARAMETER
===========================================================================#>
if ($ForgetWallpapersPath) {
try {
if ($null -ne [System.Environment]::GetEnvironmentVariable($EnvPathName, "User")) {
Remove-ItemProperty -Path "HKCU:\Environment" -Name $EnvPathName
Write-Host "`nThe environment variable named " -ForegroundColor Green -NoNewline
Write-Host "$EnvPathName " -ForegroundColor Cyan -NoNewline
Write-Host "has been deleted." -ForegroundColor Green
Write-Host "`nThis will take effect in a new pwsh session though." -ForegroundColor Yellow
}
else {
Write-Host "`nThe environment variable named " -ForegroundColor Yellow -NoNewline
Write-Host "$EnvPathName " -ForegroundColor Cyan -NoNewline
Write-Host "was not found." -ForegroundColor Yellow
Write-Host "If you expected otherwise, open a new pwsh session and run the command " -ForegroundColor Yellow
Write-Host "[System.Environment]::GetEnvironmentVariable(`"$EnvPathName`", `"User`")" -ForegroundColor Magenta
Write-Host "to be sure this variable doesn’t exist or open an environment manager like rapidEE." -ForegroundColor Yellow
}
}
catch {
TerminateWithError -errorMessage "The environment variable `"$EnvPathName`" failed to be deleted."
}
Set-Location $OriginalWorkingDir
exit
}
<#*==========================================================================
* ℹ HANDLE -All PARAMETER
===========================================================================#>
if ($All) {
. $ImportList
if ($Show -and -not $NoTags) {
WriteTitle "HERE IS THE COMPLETE LIST : $($Wallpapers.Count)"
Write-Host "$ListPath`n" -ForegroundColor Cyan
$Wallpapers |
Select-Object -Property Title, Tags, Folder |
Sort-Object -Property Title
Set-Location $OriginalWorkingDir
exit
}
if ($Show -and $NoTags) {
$FilteredList = $Wallpapers | Where-Object { -not $_.Tags -or $_.Tags.Count -eq 0 } |
Select-Object -Property Title, Folder |
Sort-Object -Property Title
WriteTitle "LIST WITH FILTER: NO TAGS : $($FilteredList.Count)"
Write-Host "$ListPath`n" -ForegroundColor Cyan
$FilteredList | Select-Object Title, Tags, Folder | Sort-Object Title
Set-Location $OriginalWorkingDir
exit
}
WriteTitle "SET RANDOM WALLPAPERS FROM COMPLETE LIST"
Write-Host "$ListPath`n" -ForegroundColor Cyan
. $SetRandom
Set-Location $OriginalWorkingDir
exit
}
<#*==========================================================================
* ℹ HANDLE -IncludeTags PARAMETER
===========================================================================#>
if ($IncludeTags) {
. $ImportList
$RequiredTags = $IncludeTags.Trim() -split '\s+'
$FilteredList = $Wallpapers | Where-Object {
$tags = $_.Tags -split '\s+'
# Put the pipeline result in parentheses before comparing
(( $RequiredTags |
ForEach-Object { $tags -contains $_ } |
Where-Object { -not $_ } |
Measure-Object |
Select-Object -ExpandProperty Count) -eq 0)
}
if ($ExcludeTags) {
. $Exclude -List $FilteredList
}
if ($Show) {
WriteTitle "FILTERED LIST : $($FilteredList.Count)"
Write-Host "$ListPath`n" -ForegroundColor Cyan
& $TagsListIn
& $TagsListOut
$FilteredList | Select-Object Title, Tags, Folder | Sort-Object Title
}
else {
WriteTitle "SET RANDOM WALLPAPERS FILTERED FROM LIST"
Write-Host "$ListPath`n" -ForegroundColor Cyan
& $TagsListIn
& $TagsListOut
. $SetRandom -List $FilteredList
}
Set-Location $OriginalWorkingDir
exit
}
<#*==========================================================================
* ℹ HANDLE -ExcludeTags PARAMETER
===========================================================================#>
if ($ExcludeTags) {
. $ImportList
. $Exclude
if ($Show) {
WriteTitle "FILTERED LIST : $($FilteredList.Count)"
Write-Host "$ListPath`n" -ForegroundColor Cyan
& $TagsListIn
& $TagsListOut
$FilteredList | Select-Object Title, Tags, Folder | Sort-Object Title
}
else {
WriteTitle "SET RANDOM WALLPAPERS FILTERED FROM LIST"
Write-Host "$ListPath`n" -ForegroundColor Cyan
& $TagsListIn
& $TagsListOut
. $SetRandom -List $FilteredList
}
Set-Location $OriginalWorkingDir
exit
}
<#*==========================================================================
* ℹ HANDLE RESTART
===========================================================================#>
if ($Restart) {
$ProcessName = "Lively"
$SubProcessName = "Lively.Player.WebView2"
if ($LivelyBin -eq "Livelycu") {
$ProcessName = $LivelyBin
}
$process = Get-Process -Name $ProcessName -ErrorAction SilentlyContinue
if ($process) {
Write-Host "Shutdown Lively…`n"
# & $LivelyBin --shutdown true
Stop-ProcessTree -ParentId $process.Id
# Wait for the process to be stopped but continue the script if process still running after 4 seconds
Wait-Process -Name $ProcessName -Timeout 2 -ErrorAction SilentlyContinue
Start-Sleep -Seconds 2 # a short delay is necessary for some reason
# Need to check if no other instance is running
$process = Get-Process -Name $ProcessName -ErrorAction SilentlyContinue
if ($process) {
Write-Host "Lively process still detected, trying to kill it…" -ForegroundColor Yellow
Stop-Process -InputObject $process -Force
Wait-Process -InputObject $process -ErrorAction SilentlyContinue
Start-Sleep -Seconds 2 # a short delay is necessary for some reason
$processCheck = Get-Process -Name $ProcessName -ErrorAction SilentlyContinue
if (-not $processCheck) {
Write-Host "Success : Lively has been stopped." -ForegroundColor Green
}
else {
TerminateWithError -errorMessage "Fail : Lively still active."
}
}
else {
Write-Host "Success : Lively has been stopped." -ForegroundColor Green
}
$SubProcess = Get-Process -Name $SubProcessName -ErrorAction SilentlyContinue
if ($SubProcess) {
Write-Host "It seems `"$SubProcessName`" process is still active though, trying to kill it…" -ForegroundColor Yellow
Stop-ProcessTree -ParentId $SubProcess.Id
Wait-Process -InputObject $SubProcess -ErrorAction SilentlyContinue
Start-Sleep -Seconds 2 # a short delay is necessary for some reason
$processCheck = Get-Process -Name $SubProcessName -ErrorAction SilentlyContinue
if (-not $processCheck) {
Write-Host "Success : `"$SubProcessName`" has been stopped." -ForegroundColor Green
}
else {
TerminateWithError -errorMessage "Fail : `"$SubProcessName`" still active."
}
}
}
else {
Write-Host "Lively is not running."
}
Write-Host "`nStarting Lively…`n"
& $LivelyBin --showApp true
$process = Get-Process -Name "Lively" -ErrorAction SilentlyContinue
if ($process) {
Write-Host "Lively is running !" -ForegroundColor Green
}
else {
TerminateWithError -errorMessage "Lively failed to launch probably because the binary is not found: `"$LivelyBin`""
}
Set-Location $OriginalWorkingDir
exit
}