Last active
May 8, 2025 03:07
-
-
Save Bryan2333/a212c9460ec8e8dd3b3296ad43da6939 to your computer and use it in GitHub Desktop.
mpv控制KDE夜间颜色脚本
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- Global to remember if night color was on before mpv | |
local night_color_was_enabled = false | |
-- Run a shell command and capture its stdout | |
local function execute_command(cmd) | |
local handle = io.popen(cmd) | |
local result = handle:read("*a") | |
handle:close() | |
return result | |
end | |
-- Check if Night Light is enabled by querying currentTemperature | |
local function is_night_color_enabled() | |
-- Query the color temperature (6500 K = off/daylight) | |
local raw = execute_command( | |
"qdbus6 org.kde.KWin /org/kde/KWin/NightLight " .. | |
"org.kde.KWin.NightLight.currentTemperature" | |
) | |
-- Trim whitespace and convert to number | |
local temp = tonumber(raw:match("%d+")) or 6500 | |
-- If temp < 6500, night‑color is active | |
return temp < 6500 | |
end | |
-- Toggle via the KDE global‑accelerator shortcut | |
local function toggle_night_color() | |
os.execute( | |
'qdbus6 org.kde.kglobalaccel /component/kwin ' .. | |
'org.kde.kglobalaccel.Component.invokeShortcut "Toggle Night Color"' | |
) | |
end | |
-- On file start: disable if currently on | |
local function manage_night_color_on_start() | |
if is_night_color_enabled() then | |
night_color_was_enabled = true | |
toggle_night_color() | |
mp.osd_message("Night color disabled") | |
else | |
night_color_was_enabled = false | |
end | |
end | |
-- On file end/idle: restore previous state | |
local function restore_night_color_on_end() | |
if night_color_was_enabled then | |
toggle_night_color() | |
mp.osd_message("Night color re‑enabled") | |
end | |
end | |
-- Hook into mpv events | |
mp.register_event("start-file", manage_night_color_on_start) | |
mp.register_event("end-file", restore_night_color_on_end) | |
mp.register_event("idle", restore_night_color_on_end) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment