Skip to content

Commit

Permalink
Merge pull request #46 from t1m0thyj/update-5.5
Browse files Browse the repository at this point in the history
Update scripts to receive params from stdin
  • Loading branch information
t1m0thyj authored Apr 24, 2024
2 parents 6c30844 + 5c8c884 commit ddcdab9
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: `$params.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
)
$params = $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
$params = $Input | ConvertFrom-Json

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(($params | 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
)
$params = $Input | ConvertFrom-Json

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 ($params.imagePaths -And (Test-Path "~\AppData\Local\Microsoft\Edge\User Data\Default")) {
Copy-Item $params.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
)
$params = $Input | ConvertFrom-Json

$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 ($params.themeMode -Eq 0) {$params.daySegment2} Else {$params.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
)
$params = $Input | ConvertFrom-Json

If (-Not ($PSBoundParameters.ContainsKey("imagePath"))) {
If (-Not $params.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 $params.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
)
$params = $Input | ConvertFrom-Json

# 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 ($params.daySegment4 -Eq -1) {
$params.daySegment4 = $params.daySegment2 * 2 + 1
}

$brightnessPercent = switch ( $daySegment4 )
$brightnessPercent = switch ($params.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
)
$params = $Input | ConvertFrom-Json

$registryPath = "HKCU:\Software\Microsoft\Windows\CurrentVersion\Themes\Personalize"
$dwordValue = If ($nightMode) {0} Else {1 - $daySegment2}
$dwordValue = 1 - (If ($params.themeMode -Eq 0) {$params.daySegment2} Else {$params.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
)
$params = $Input | ConvertFrom-Json

$registryPath = "HKCU:\Software\Microsoft\Windows\CurrentVersion\Themes\Personalize"
$dwordValue = If ($nightMode) {0} Else {1 - $daySegment2}
$dwordValue = 1 - (If ($params.themeMode -Eq 0) {$params.daySegment2} Else {$params.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
)
$params = $Input | ConvertFrom-Json

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

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

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

0 comments on commit ddcdab9

Please sign in to comment.