From f2ad6231407a8e41574a6241c3eb8b7e09e9db11 Mon Sep 17 00:00:00 2001 From: Angel Wang Date: Tue, 23 Jun 2026 00:05:39 -0600 Subject: [PATCH] fix(apply): write Windows apply wallpaper script to a file --- src/apply/index.ts | 27 ++------------------------- src/utils.ts | 43 +++++++++++++++++++++++++++++++++++++------ 2 files changed, 39 insertions(+), 31 deletions(-) diff --git a/src/apply/index.ts b/src/apply/index.ts index 453a3c6..7f8275a 100644 --- a/src/apply/index.ts +++ b/src/apply/index.ts @@ -1,5 +1,5 @@ import type { ImageAnalysisData } from "../analyze/analysis.ts" -import { applyConfigPath, cachePath, chalkDebug, loadConfig, map, mapEaseOutExp, mapEaseOutQuint } 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,29 +83,6 @@ console.info(`Setting wallpaper...`) if (Deno.build.os != "windows") { await $`eval ${config.applyWallpaperCommand.replace("%s", targetImage.path)}` } else { - // 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 = ` - $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(${targetImage.path}) - ` - await $`pwsh ${command}` + await $`pwsh ${windowsApplyScriptPath}` } console.info(`Success! Enjoy :)`) diff --git a/src/utils.ts b/src/utils.ts index a2113dd..785ef4f 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -23,19 +23,50 @@ function getCachePath() { } } -export const applyConfigPath = path.fromFileUrl(`file:///${getConfigPath()}/apply-config.json`) -export const analyzeConfigPath = path.fromFileUrl(`file:///${getConfigPath()}/analyze-config.json`) -export const cachePath = path.fromFileUrl(`file:///${getCachePath()}/analysis.json`) - // ensure the config and cache directories exist -if (!fs.statSync(path.fromFileUrl(`file:///${getConfigPath()}`))) { +if (!fs.statSync(path.fromFileUrl(`file:///${getConfigPath()}`), { throwIfNoEntry: false })) { fs.mkdirSync(path.fromFileUrl(`file:///${getConfigPath()}`), { recursive: true }) } -if (!fs.statSync(path.fromFileUrl(`file:///${getCachePath()}`))) { +if (!fs.statSync(path.fromFileUrl(`file:///${getCachePath()}`), { throwIfNoEntry: false })) { fs.mkdirSync(path.fromFileUrl(`file:///${getCachePath()}`), { recursive: true }) } +export const applyConfigPath = path.fromFileUrl(`file:///${getConfigPath()}/apply-config.json`) +export const analyzeConfigPath = path.fromFileUrl(`file:///${getConfigPath()}/analyze-config.json`) +export const cachePath = path.fromFileUrl(`file:///${getCachePath()}/analysis.json`) +export const windowsApplyScriptPath = path.fromFileUrl(`file:///${getConfigPath()}/setWallpaper.ps1`) + +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) + ` + await fs.writeFile(windowsApplyScriptPath, command) + console.info(`Wrote Windows wallpaper apply script at ${windowsApplyScriptPath}!`) +} + export async function loadConfig(pathToConfig: string, defaultConfig: object) { let config: object | undefined try { -- 2.51.2