Skip to content

Commit 36ab095

Browse files
committed
Let user provide PowerShell script to load a desktop wallpaper image
1 parent 5ef51a5 commit 36ab095

File tree

1 file changed

+21
-17
lines changed

1 file changed

+21
-17
lines changed

modifier/Personalization.cs

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ public record class SolidWallpaperSettings(
1212
Color Color
1313
) : IWallpaperSettings;
1414

15-
public record class ImageWallpaperSettings(
16-
byte[] Image
15+
public record class ScriptWallpaperSettings(
16+
string Script
1717
) : IWallpaperSettings;
1818

1919
public interface IColorSettings;
@@ -66,25 +66,29 @@ void WriteWallpaperScript(Action<StringWriter> after)
6666

6767
switch (Configuration.WallpaperSettings)
6868
{
69-
case ImageWallpaperSettings settings:
69+
case ScriptWallpaperSettings settings:
70+
string imageFile = @"C:\Windows\Setup\Scripts\Wallpaper";
71+
string getterFile = AddTextFile("GetWallpaper.ps1", settings.Script);
72+
SpecializeScript.Append($$"""
73+
try {
74+
$bytes = Get-Content -LiteralPath '{{getterFile}}' -Raw | Invoke-Expression;
75+
[System.IO.File]::WriteAllBytes( '{{imageFile}}', $bytes );
76+
} catch {
77+
$_;
78+
}
79+
""");
80+
WriteWallpaperScript(writer =>
7081
{
71-
string file = @"C:\Windows\Setup\Scripts\Wallpaper";
72-
AddBinaryFile(settings.Image, file);
73-
WriteWallpaperScript(writer =>
74-
{
75-
writer.WriteLine(@$"Set-WallpaperImage -LiteralPath '{file}';");
76-
});
77-
break;
78-
}
82+
writer.WriteLine(@$"Set-WallpaperImage -LiteralPath '{imageFile}';");
83+
});
84+
break;
7985

8086
case SolidWallpaperSettings settings:
87+
WriteWallpaperScript(writer =>
8188
{
82-
WriteWallpaperScript(writer =>
83-
{
84-
writer.WriteLine($"Set-WallpaperColor -HtmlColor '{ColorTranslator.ToHtml(settings.Color)}';");
85-
});
86-
break;
87-
}
89+
writer.WriteLine($"Set-WallpaperColor -HtmlColor '{ColorTranslator.ToHtml(settings.Color)}';");
90+
});
91+
break;
8892
}
8993
}
9094
}

0 commit comments

Comments
 (0)