From 60191fb66fd51a8dadaa5153c2c501bc42752578 Mon Sep 17 00:00:00 2001 From: Angel Wang Date: Tue, 23 Jun 2026 01:56:56 -0600 Subject: [PATCH] fix(apply): fix Windows script --- src/apply/index.ts | 14 ++++++++++++-- src/utils.ts | 41 +++++++++++++++++++---------------------- 2 files changed, 31 insertions(+), 24 deletions(-) diff --git a/src/apply/index.ts b/src/apply/index.ts index 7f8275a..ef87e46 100644 --- a/src/apply/index.ts +++ b/src/apply/index.ts @@ -1,5 +1,14 @@ import type { ImageAnalysisData } from "../analyze/analysis.ts" -import { applyConfigPath, cachePath, chalkDebug, loadConfig, map, mapEaseOutExp, mapEaseOutQuint, windowsApplyScriptPath } from "../utils.ts" +import { + applyConfigPath, + cachePath, + chalkDebug, + loadConfig, + map, + mapEaseOutExp, + mapEaseOutQuint, + windowsApplyScriptPath, +} from "../utils.ts" import { type ApplicationConfig, defaultConfig, findMatchingImages, getOpenMeteoData } from "./application.ts" import * as SunCalc from "suncalc" import "zx/globals" @@ -83,6 +92,7 @@ console.info(`Setting wallpaper...`) if (Deno.build.os != "windows") { await $`eval ${config.applyWallpaperCommand.replace("%s", targetImage.path)}` } else { - await $`pwsh ${windowsApplyScriptPath}` + const command = new Deno.Command(`pwsh`, { args: [ windowsApplyScriptPath, targetImage.path ], stdout: "piped" } ) + command.spawn() } console.info(`Success! Enjoy :)`) diff --git a/src/utils.ts b/src/utils.ts index 785ef4f..00b066f 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -39,29 +39,26 @@ export const windowsApplyScriptPath = path.fromFileUrl(`file:///${getConfigPath( if (!fs.statSync(windowsApplyScriptPath, { throwIfNoEntry: false })) { // C# in TypeScript. who would've thought - // uiAction: SPI_SetDeskWallpaper = 0x0014 = 20 - // uiParam: unused = 0 - // pvParam: path to the image - // fWinIni: SPIF_SENDCHANGE (update user profile + broadcast setting change) = 0x02 = 2 - const command = ` - $imagePath = $args[0] - $code = @' - using System.Runtime.InteropServices; - namespace Win32 { - public class Wallpaper { - [DllImport("user32.dll", CharSet=CharSet.Auto)] - static extern int SystemParametersInfoA(int uiAction, int uiParam, string pvParam, int fWinIni); - - public static void SetWallpaper(string imagePath){ - SystemParametersInfo(20, 0, imagePath, 2); - } - } - } - '@ - - add-type $code - [Win32.Wallpaper]::SetWallpaper($imagePath) +$imagePath = $args[0] +$setwallpapersrc = @" +using System.Runtime.InteropServices; +public class wallpaper { + public const int SetDesktopWallpaper = 20; + public const int UpdateIniFile = 0x01; + public const int SendWinIniChange = 0x02; + + [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)] + private static extern int SystemParametersInfo (int uAction, int uParam, string lpvParam, int fuWinIni); + + public static void SetWallpaper (string path) { + SystemParametersInfo(SetDesktopWallpaper, 0, path, UpdateIniFile | SendWinIniChange); + } +} +"@ +Add-Type -TypeDefinition $setwallpapersrc + +[wallpaper]::SetWallpaper("$imagePath") ` await fs.writeFile(windowsApplyScriptPath, command) console.info(`Wrote Windows wallpaper apply script at ${windowsApplyScriptPath}!`) -- 2.51.2