如何用脚本批量修改壁纸?

wen 实用脚本 3

本文目录导读:

如何用脚本批量修改壁纸?

  1. Windows 系统
  2. Linux 系统
  3. macOS 系统
  4. 跨平台 Python 方案
  5. 使用说明
  6. 注意事项

我来介绍几种批量修改壁纸的方法,根据不同操作系统和需求选择:

Windows 系统

PowerShell 脚本方法

# 批量设置壁纸脚本
$wallpaperFolder = "C:\Wallpapers\"  # 壁纸文件夹路径
$wallpapers = Get-ChildItem -Path $wallpaperFolder -Filter "*.jpg"  # 获取所有jpg图片
foreach ($wallpaper in $wallpapers) {
    # 设置壁纸
    Add-Type -TypeDefinition @"
    using System;
    using System.Runtime.InteropServices;
    public class Wallpaper {
        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        public static extern int SystemParametersInfo(
            int uAction, int uParam, string lpvParam, int fuWinIni);
    }
"@
    [Wallpaper]::SystemParametersInfo(20, 0, $wallpaper.FullName, 2)
    # 等待一段时间(可选)
    Start-Sleep -Seconds 300  # 5分钟换一次
}

批处理文件 (.bat)

@echo off
setlocal enabledelayedexpansion
set "wallpaper_folder=C:\Wallpapers\"
set "index=0"
:loop
for /r "%wallpaper_folder%" %%f in (*.jpg *.png *.bmp) do (
    set /a "index+=1"
    reg add "HKCU\Control Panel\Desktop" /v Wallpaper /t REG_SZ /d "%%f" /f
    RUNDLL32.EXE user32.dll,UpdatePerUserSystemParameters
    timeout /t 300  REM 5分钟换一次
)
goto loop

Linux 系统

使用 feh 工具

#!/bin/bash
# 壁纸文件夹
WALLPAPER_DIR="$HOME/Pictures/wallpapers"
INTERVAL=300  # 5分钟
while true; do
    for img in "$WALLPAPER_DIR"/*.{jpg,png,jpeg}; do
        feh --bg-scale "$img"
        sleep $INTERVAL
    done
done

使用 sway/niri 等 Wayland 合成器

#!/bin/bash
# 安装: sudo apt install swaybg
WALLPAPER_DIR="$HOME/Pictures/wallpapers"
INTERVAL=300
while true; do
    for img in "$WALLPAPER_DIR"/*.{jpg,png,jpeg}; do
        swaybg -i "$img" -m fill &
        sleep $INTERVAL
    done
done

macOS 系统

AppleScript 方法

-- 批量切换壁纸脚本
set wallpaperFolder to "/Users/username/Pictures/Wallpapers/"
tell application "Finder"
    set wallpaperFiles to files of folder wallpaperFolder whose name extension is in {"jpg", "png", "jpeg"}
end tell
repeat with wallFile in wallpaperFiles
    tell application "System Events"
        set the desktop picture to (POSIX file (wallpaperFolder & wallFile))
    end tell
    delay 300  -- 5分钟切换
end repeat

跨平台 Python 方案

import os
import time
import random
import subprocess
import platform
class WallpaperManager:
    def __init__(self, wallpaper_dir, interval=300):
        self.wallpaper_dir = wallpaper_dir
        self.interval = interval
        self.system = platform.system()
    def get_wallpapers(self):
        """获取所有壁纸文件"""
        extensions = ['.jpg', '.png', '.jpeg', '.bmp']
        wallpapers = []
        for ext in extensions:
            wallpapers.extend([
                os.path.join(self.wallpaper_dir, f) 
                for f in os.listdir(self.wallpaper_dir) 
                if f.lower().endswith(ext)
            ])
        return wallpapers
    def set_wallpaper_windows(self, image_path):
        """Windows设置壁纸"""
        import ctypes
        SPI_SETDESKWALLPAPER = 20
        ctypes.windll.user32.SystemParametersInfoW(
            SPI_SETDESKWALLPAPER, 0, image_path, 0
        )
    def set_wallpaper_linux(self, image_path):
        """Linux设置壁纸"""
        # 尝试多种方法
        try:
            subprocess.run(['gsettings', 'set', 'org.gnome.desktop.background', 
                          'picture-uri', f'file://{image_path}'], check=True)
        except:
            try:
                subprocess.run(['feh', '--bg-scale', image_path], check=True)
            except:
                print(f"无法设置壁纸: {image_path}")
    def set_wallpaper_mac(self, image_path):
        """macOS设置壁纸"""
        script = f'''
        tell application "System Events"
            set the desktop picture to POSIX file "{image_path}"
        end tell
        '''
        subprocess.run(['osascript', '-e', script])
    def change_wallpaper(self, image_path):
        """根据操作系统切换壁纸"""
        if self.system == "Windows":
            self.set_wallpaper_windows(image_path)
        elif self.system == "Linux":
            self.set_wallpaper_linux(image_path)
        elif self.system == "Darwin":
            self.set_wallpaper_mac(image_path)
        print(f"已切换壁纸: {image_path}")
    def run_random(self):
        """随机切换壁纸"""
        wallpapers = self.get_wallpapers()
        if not wallpapers:
            print("未找到壁纸文件")
            return
        while True:
            random.shuffle(wallpapers)
            for wallpaper in wallpapers:
                self.change_wallpaper(wallpaper)
                time.sleep(self.interval)
    def run_sequential(self):
        """顺序切换壁纸"""
        wallpapers = self.get_wallpapers()
        if not wallpapers:
            print("未找到壁纸文件")
            return
        while True:
            for wallpaper in wallpapers:
                self.change_wallpaper(wallpaper)
                time.sleep(self.interval)
# 使用示例
if __name__ == "__main__":
    manager = WallpaperManager(
        wallpaper_dir="/path/to/your/wallpapers",  # 修改为你的壁纸文件夹路径
        interval=300  # 5分钟切换一次
    )
    # 随机切换
    manager.run_random()

使用说明

  1. Windows 用户:以管理员身份运行脚本
  2. Linux 用户:可能需要安装 fehswaybg
  3. macOS 用户:需要允许辅助功能权限

注意事项

  • 确保壁纸文件夹路径正确
  • 图片格式支持:jpg、png、jpeg等
  • 切换间隔不要设置太短(建议至少30秒)
  • 某些系统可能需要管理员权限

选择适合你操作系统的方法即可!

抱歉,评论功能暂时关闭!