Skip to content

Commit

Permalink
Update scripts to receive params from stdin
Browse files Browse the repository at this point in the history
  • Loading branch information
t1m0thyj committed Apr 11, 2024
1 parent 6c30844 commit 4ec8ff7
Show file tree
Hide file tree
Showing 10 changed files with 41 additions and 79 deletions.
18 changes: 8 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,19 @@ Select a category of scripts to browse:

## Create New Scripts

PowerShell scripts are run by WinDynamicDesktop when the wallpaper image is updated. They are invoked with the following parameters:
PowerShell scripts are run by WinDynamicDesktop when the wallpaper image is updated. They are invoked with a JSON object that contains the following parameters:

`<scriptName> [-daySegment2] <int> [-daySegment4] <int> [-nightMode] <bool> [-imagePath <string>]`
* `daySegment2` - 0 = Day, 1 = Night
* `daySegment4` - -1 = N/A, 0 = Sunrise, 1 = Day, 2 = Sunset, 3 = Night
* `themeMode` - 0 = Automatic, 1 = Light Mode, 2 = Dark Mode
* `imagePaths` - List of paths to current wallpaper image for each display

To read the values of these parameters, add the following lines at the top of your script:
To read the values of these parameters, add the line below to the top of your script and access them like this: `$event.daySegment2`

```powershell
param (
[Parameter(Mandatory=$true)][int]$daySegment2, # 0 = Day, 1 = Night
[Parameter(Mandatory=$true)][int]$daySegment4, # -1 = N/A, 0 = Sunrise, 1 = Day, 2 = Sunset, 3 = Night
[Parameter(Mandatory=$true)][bool]$nightMode, # True if night mode is enabled
[Parameter(Mandatory=$false)][string]$imagePath # Path to current wallpaper image
)
$event = $Input | ConvertFrom-Json
```

A sample script that makes use of these variables can be found [here](./SampleScript.ps1). When the sample script is installed and gets run by WinDynamicDesktop, it will display the values of all the parameters.
A sample script that uses parameters can be found [here](./SampleScript.ps1). When the sample script is installed and gets run by WinDynamicDesktop, it will display the values of all the parameters.

If you create a script and would like to share it with other users of the app, pull requests in this repository are welcome.
8 changes: 8 additions & 0 deletions SampleEvent.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"daySegment2": 0,
"daySegment4": 1,
"themeMode": 0,
"imagePaths": [
"C:\\Windows\\Web\\Wallpaper\\Windows\\img0.jpg"
]
}
10 changes: 3 additions & 7 deletions SampleScript.ps1
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
param (
[Parameter(Mandatory=$true)][int]$daySegment2, # 0 = Day, 1 = Night
[Parameter(Mandatory=$true)][int]$daySegment4, # -1 = N/A, 0 = Sunrise, 1 = Day, 2 = Sunset, 3 = Night
[Parameter(Mandatory=$true)][bool]$nightMode, # True if night mode is enabled
[Parameter(Mandatory=$false)][string]$imagePath # Path to current wallpaper image
)
# To test with sample event, run: cat SampleEvent.json | .\SampleScript.ps1
$event = $Input | ConvertFrom-Json

Check warning

Code scanning / PSScriptAnalyzer

The Variable 'event' is an automatic variable that is built into PowerShell, assigning to it might have undesired side effects. If assignment is not by design, please use a different name. Warning

The Variable 'event' is an automatic variable that is built into PowerShell, assigning to it might have undesired side effects. If assignment is not by design, please use a different name.

Add-Type -AssemblyName PresentationCore,PresentationFramework
[System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
[System.Windows.MessageBox]::Show([string]$daySegment2 + ' ' + [string]$daySegment4 + ' ' + [string]$nightMode + ' ' + $imagePath)
[System.Windows.MessageBox]::Show(($event | Format-List | Out-String), "WinDynamicDesktop")
15 changes: 3 additions & 12 deletions experimental/ChangeEdgeBackgroundDefault.ps1
Original file line number Diff line number Diff line change
@@ -1,14 +1,5 @@
param (
[Parameter(Mandatory=$true)][int]$daySegment2, # 0 = Day, 1 = Night
[Parameter(Mandatory=$true)][int]$daySegment4, # -1 = N/A, 0 = Sunrise, 1 = Day, 2 = Sunset, 3 = Night
[Parameter(Mandatory=$true)][bool]$nightMode, # True if night mode is enabled
[Parameter(Mandatory=$false)][string]$imagePath # Path to current wallpaper image
)
$event = $Input | ConvertFrom-Json

Check warning

Code scanning / PSScriptAnalyzer

The Variable 'event' is an automatic variable that is built into PowerShell, assigning to it might have undesired side effects. If assignment is not by design, please use a different name. Warning

The Variable 'event' is an automatic variable that is built into PowerShell, assigning to it might have undesired side effects. If assignment is not by design, please use a different name.

If (-Not ($PSBoundParameters.ContainsKey("imagePath"))) {
Break
}

If (Test-Path "~\AppData\Local\Microsoft\Edge\User Data\Default") {
Copy-Item $imagePath "~\AppData\Local\Microsoft\Edge\User Data\Default\edge_background.jpg"
If ($event.imagePaths -And (Test-Path "~\AppData\Local\Microsoft\Edge\User Data\Default")) {
Copy-Item $event.imagePaths[0] "~\AppData\Local\Microsoft\Edge\User Data\Default\edge_background.jpg"
}
11 changes: 3 additions & 8 deletions experimental/ChangeMicrosoftEdgeTheme.ps1
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
param (
[Parameter(Mandatory=$true)][int]$daySegment2, # 0 = Day, 1 = Night
[Parameter(Mandatory=$true)][int]$daySegment4, # -1 = N/A, 0 = Sunrise, 1 = Day, 2 = Sunset, 3 = Night
[Parameter(Mandatory=$true)][bool]$nightMode, # True if night mode is enabled
[Parameter(Mandatory=$false)][string]$imagePath # Path to current wallpaper image
)
$event = $Input | ConvertFrom-Json

Check warning

Code scanning / PSScriptAnalyzer

The Variable 'event' is an automatic variable that is built into PowerShell, assigning to it might have undesired side effects. If assignment is not by design, please use a different name. Warning

The Variable 'event' is an automatic variable that is built into PowerShell, assigning to it might have undesired side effects. If assignment is not by design, please use a different name.

$registryPath = "HKCU:\Software\Classes\Local Settings\Software\Microsoft\Windows\CurrentVersion\AppContainer\Storage\microsoft.microsoftedge_8wekyb3d8bbwe\MicrosoftEdge\Main"
$dwordValue = If ($nightMode) {1} Else {$daySegment2}
$dwordValue = If ($event.themeMode -Eq 0) {$event.daySegment2} Else {$event.themeMode - 1}

if ( Test-Path $registryPath )
if (Test-Path $registryPath)
{
New-ItemProperty -Path $registryPath -Name "Theme" -Value $dwordValue -PropertyType DWORD -Force | Out-Null
}
11 changes: 3 additions & 8 deletions experimental/SyncVirtualDesktops.ps1
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
param (
[Parameter(Mandatory=$true)][int]$daySegment2, # 0 = Day, 1 = Night
[Parameter(Mandatory=$true)][int]$daySegment4, # -1 = N/A, 0 = Sunrise, 1 = Day, 2 = Sunset, 3 = Night
[Parameter(Mandatory=$true)][bool]$nightMode, # True if night mode is enabled
[Parameter(Mandatory=$false)][string]$imagePath # Path to current wallpaper image
)
$event = $Input | ConvertFrom-Json

Check warning

Code scanning / PSScriptAnalyzer

The Variable 'event' is an automatic variable that is built into PowerShell, assigning to it might have undesired side effects. If assignment is not by design, please use a different name. Warning

The Variable 'event' is an automatic variable that is built into PowerShell, assigning to it might have undesired side effects. If assignment is not by design, please use a different name.

If (-Not ($PSBoundParameters.ContainsKey("imagePath"))) {
If (-Not $event.imagePaths) {
Break
}

Expand All @@ -23,7 +18,7 @@ if (-Not (Get-Module -ListAvailable -Name VirtualDesktop)) {

Get-DesktopList | ForEach-Object {
if (-Not (Test-CurrentDesktop -Desktop $_.Number)) {
Set-DesktopWallpaper -Desktop $_.Number -Path $imagePath
Set-DesktopWallpaper -Desktop $_.Number -Path $event.imagePaths[0]
}
}

Expand Down
14 changes: 4 additions & 10 deletions stable/ChangeDisplayBrightness.ps1
Original file line number Diff line number Diff line change
@@ -1,23 +1,17 @@
# NOTE: Requires hardware that supports DDC/CI (the ability to change display brightness in Windows)

param (
[Parameter(Mandatory=$true)][int]$daySegment2, # 0 = Day, 1 = Night
[Parameter(Mandatory=$true)][int]$daySegment4, # -1 = N/A, 0 = Sunrise, 1 = Day, 2 = Sunset, 3 = Night
[Parameter(Mandatory=$true)][bool]$nightMode, # True if night mode is enabled
[Parameter(Mandatory=$false)][string]$imagePath # Path to current wallpaper image
)
$event = $Input | ConvertFrom-Json

Check warning

Code scanning / PSScriptAnalyzer

The Variable 'event' is an automatic variable that is built into PowerShell, assigning to it might have undesired side effects. If assignment is not by design, please use a different name. Warning

The Variable 'event' is an automatic variable that is built into PowerShell, assigning to it might have undesired side effects. If assignment is not by design, please use a different name.

# Edit these values to be a valid screen brightness percent between 0 and 100
$sunriseBrightness = 40
$dayBrightness = 80
$sunsetBrightness = 30
$nightBrightness = 20

if ( $daySegment4 -eq -1 ) {
$daySegment4 = $daySegment2 * 2 + 1
if ($event.daySegment4 -Eq -1) {
$event.daySegment4 = $event.daySegment2 * 2 + 1
}

$brightnessPercent = switch ( $daySegment4 )
$brightnessPercent = switch ($event.daySegment4)
{
0 { $sunriseBrightness }
1 { $dayBrightness }
Expand Down
11 changes: 3 additions & 8 deletions stable/ChangeWindowsAppTheme.ps1
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
param (
[Parameter(Mandatory=$true)][int]$daySegment2, # 0 = Day, 1 = Night
[Parameter(Mandatory=$true)][int]$daySegment4, # -1 = N/A, 0 = Sunrise, 1 = Day, 2 = Sunset, 3 = Night
[Parameter(Mandatory=$true)][bool]$nightMode, # True if night mode is enabled
[Parameter(Mandatory=$false)][string]$imagePath # Path to current wallpaper image
)
$event = $Input | ConvertFrom-Json

Check warning

Code scanning / PSScriptAnalyzer

The Variable 'event' is an automatic variable that is built into PowerShell, assigning to it might have undesired side effects. If assignment is not by design, please use a different name. Warning

The Variable 'event' is an automatic variable that is built into PowerShell, assigning to it might have undesired side effects. If assignment is not by design, please use a different name.

$registryPath = "HKCU:\Software\Microsoft\Windows\CurrentVersion\Themes\Personalize"
$dwordValue = If ($nightMode) {0} Else {1 - $daySegment2}
$dwordValue = 1 - (If ($event.themeMode -Eq 0) {$event.daySegment2} Else {$event.themeMode - 1})

if ( Test-Path $registryPath )
if (Test-Path $registryPath)
{
New-ItemProperty -Path $registryPath -Name "AppsUseLightTheme" -Value $dwordValue -PropertyType DWORD -Force | Out-Null
}
11 changes: 3 additions & 8 deletions stable/ChangeWindowsSystemTheme.ps1
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
param (
[Parameter(Mandatory=$true)][int]$daySegment2, # 0 = Day, 1 = Night
[Parameter(Mandatory=$true)][int]$daySegment4, # -1 = N/A, 0 = Sunrise, 1 = Day, 2 = Sunset, 3 = Night
[Parameter(Mandatory=$true)][bool]$nightMode, # True if night mode is enabled
[Parameter(Mandatory=$false)][string]$imagePath # Path to current wallpaper image
)
$event = $Input | ConvertFrom-Json

Check warning

Code scanning / PSScriptAnalyzer

The Variable 'event' is an automatic variable that is built into PowerShell, assigning to it might have undesired side effects. If assignment is not by design, please use a different name. Warning

The Variable 'event' is an automatic variable that is built into PowerShell, assigning to it might have undesired side effects. If assignment is not by design, please use a different name.

$registryPath = "HKCU:\Software\Microsoft\Windows\CurrentVersion\Themes\Personalize"
$dwordValue = If ($nightMode) {0} Else {1 - $daySegment2}
$dwordValue = 1 - (If ($event.themeMode -Eq 0) {$event.daySegment2} Else {$event.themeMode - 1})

if ( Test-Path $registryPath )
if (Test-Path $registryPath)
{
New-ItemProperty -Path $registryPath -Name "SystemUsesLightTheme" -Value $dwordValue -PropertyType DWORD -Force | Out-Null
}
11 changes: 3 additions & 8 deletions stable/ChangeWindowsTerminalBackground.ps1
Original file line number Diff line number Diff line change
@@ -1,17 +1,12 @@
param (
[Parameter(Mandatory=$true)][int]$daySegment2, # 0 = Day, 1 = Night
[Parameter(Mandatory=$true)][int]$daySegment4, # -1 = N/A, 0 = Sunrise, 1 = Day, 2 = Sunset, 3 = Night
[Parameter(Mandatory=$true)][bool]$nightMode, # True if night mode is enabled
[Parameter(Mandatory=$false)][string]$imagePath # Path to current wallpaper image
)
$event = $Input | ConvertFrom-Json

Check warning

Code scanning / PSScriptAnalyzer

The Variable 'event' is an automatic variable that is built into PowerShell, assigning to it might have undesired side effects. If assignment is not by design, please use a different name. Warning

The Variable 'event' is an automatic variable that is built into PowerShell, assigning to it might have undesired side effects. If assignment is not by design, please use a different name.

If (-Not ($PSBoundParameters.ContainsKey("imagePath"))) {
If (-Not $event.imagePaths) {
Break
}

$jsonFilePath = "$Env:LocalAppData\Packages\Microsoft.WindowsTerminal_8wekyb3d8bbwe\LocalState\settings.json"
$jsonDataOld = Get-Content -Path $jsonFilePath
$imagePath = $imagePath.Replace('\', '\\')
$imagePath = $event.imagePaths[0].Replace('\', '\\')

If ($jsonDataOld -match '(?<!//\s*)"backgroundImage":') {
$jsonDataNew = $jsonDataOld -replace ('(?<!//\s*)"backgroundImage": ".+"', "`"backgroundImage`": `"$imagePath`"")
Expand Down

0 comments on commit 4ec8ff7

Please sign in to comment.