Auto Dark Mode 脚本功能小试牛刀,实现日出日落自动切换自定义幻灯片组

Auto Dark Mode之前只能切换图片,主题,但是如果采用幻灯片模式,是没有办法自动切换幻灯片组的。

现在的版本更新了脚本功能

 文档如下:

可以设置

1. 执行命令

2. 执行参数

3. 执行情景

# If you want to make use of the scripts feature, set this to true
Enabled: false
Component:
  Scripts:
  - Name: MyApp
    # Command is the path to the executable, it can be something in your PATH, or a full file path
    Command: C:\Users\Mypath\MyExecutable.exe
    # The working directory is optional.
    # You can omit it if you don't care about where your script is executed
    WorkingDirectory: C:\Users\YourUserName\AppData\Roaming\AutoDarkMode
    # The arguments that are passed when the script is called for light and dark mode.
    # They can be omitted as well if not needed.
    # You can either use flow style which is comma separated...
    ArgsLight: [--light, I am another light command]
    # ...or the normal syntax
    ArgsDark: 
      - --dark
      - I am another dark command
    # Specifies the allowed switch sources for better control when to run scripts
    AllowedSources: [Any, TimeSwitchModule, BatteryStatusChanged, SystemResume, Manual]
    # the maximum allowed time each script is allowed to run in milliseconds. Default is 10000 if omitted
    TimeoutMillis: 10000

  - Name: Powershell Example
    Command: powershell 
    ArgsLight: [C:\test.ps1, -message light -emotion 'happy']
    ArgsDark: [C:\test.ps1, -message dark -emotion 'happy']
    AllowedSources: [Any]

  - Name: MyOtherApp
    # Example for calling cmd silently
    # You can replace the "echo I am a dark command" with a patch to a batch file
    # This will call the batch file without displaying a console window
    Command: cmd
    ArgsLight: 
      - /c
      - echo I am a light command
    ArgsDark: [/c, echo I am a dark command]

  - Name: MyMinimalAppNoParameters
    Command: C:\Users\MyUsername\test.exe

 下面实现一个简单的白天夜间切换

先自己设置一个主题,他会被记录在“%APPDATA%\Microsoft\Windows\Themes”文件夹中

 保存slideshow.ini文件即可复现当前桌面幻灯片文件夹状态

在我的实现中,生成了白天黑夜两个文件,分别保存在了对应的文件夹中

然后写一个powershell脚本实现如下功能:

输入 -mode day 和 -mode night 分别切换到对应的主题:

param(
    [string]$mode # 接受一个参数,用于判断是切换到白天模式还是夜间模式
)

$themePath = Join-Path $env:APPDATA "Microsoft\Windows\Themes"
$dayPath = "$themePath\day"
$nightPath = "$themePath\night"
$fileName = "slideshow.ini"
$modeFilePath = "$themePath\mode.txt"

function Clear-Files($path) {
    Get-ChildItem -Path $path -File | Remove-Item -Force
}

function Copy-FileIfExist($source, $destination, $file) {
    $sourceFile = Join-Path $source $file
    $destinationFile = Join-Path $destination $file
    if (Test-Path $sourceFile) {
        Copy-Item $sourceFile -Destination $destinationFile -Force
    }
}

function Update-ModeFile($path, $mode) {
    Set-Content -Path $path -Value $mode
}

# 读取当前模式
$currentMode = "day"
if (Test-Path $modeFilePath) {
    $currentMode = Get-Content $modeFilePath
}

# 根据当前模式和目标模式决定文件复制方向
if ($currentMode -ne $mode) {
    if ($currentMode -eq "day") {
        Clear-Files $dayPath
        Copy-FileIfExist $themePath $dayPath $fileName
    } elseif ($currentMode -eq "night") {
        Clear-Files $nightPath
        Copy-FileIfExist $themePath $nightPath $fileName
    } else {
        Write-Host "无效的模式参数,应为 'day' 或 'night'."
        exit
    }

    if ($mode -eq "day") {
        $sourcePath = $dayPath
    } elseif ($mode -eq "night") {
        $sourcePath = $nightPath
    } else {
        Write-Host "无效的模式参数,应为 'day' 或 'night'."
        exit
    }
    # 清空 theme 文件夹中的所有文件
    Clear-Files $themePath

    # 更新模式文件
    Update-ModeFile $modeFilePath $mode

    # 复制新模式的文件到 theme 文件夹
    Copy-FileIfExist $sourcePath $themePath $fileName

    # 等待一秒以确保文件操作完成
    Start-Sleep -Seconds 1

    # 重启 Windows Explorer
    Stop-Process -Name "explorer" -Force
}

最后设定script执行模式为日出日落切换

 命令为powershell,参数为脚本位置和脚本参数,allowedsources设为TimeSwitchModule意思是时间切换的时候触发

Enabled: true
Component:
  Scripts:
  - Name: mySwitch
    Command: powershell
    ArgsLight: ['%APPDATA%\Microsoft\Windows\Themes\script\SwitchTheme.ps1', -mode day]
    ArgsDark: ['%APPDATA%\Microsoft\Windows\Themes\script\SwitchTheme.ps1', -mode night]
    AllowedSources: [TimeSwitchModule]

完成

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值