-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwindows_settings.py
More file actions
122 lines (93 loc) · 4.42 KB
/
Copy pathwindows_settings.py
File metadata and controls
122 lines (93 loc) · 4.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# (c) Andrew Chen (https://github.com/achen1296)
import math
import os
import re
import subprocess
import time
from ctypes import cdll
from pathlib import Path
import files
def kill_settings():
MAX_KILLS = 2
MAX_WAIT = 30
kills = 0
wait = 0
print("Waiting to close settings app...")
while True:
if subprocess.run("taskkill /im SystemSettings.exe /f", creationflags=subprocess.CREATE_NO_WINDOW).returncode != 0:
kills += 1
if kills >= MAX_KILLS:
break
time.sleep(1)
wait += 1
if wait >= MAX_WAIT:
break
def current_theme():
output = subprocess.run(
"C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe \"Get-ItemProperty -path HKCU:\\Software\\Microsoft\\Windows\\CurrentVersion\\Themes -Name 'CurrentTheme'\"", capture_output=True, creationflags=subprocess.CREATE_NO_WINDOW).stdout.decode()
if match := re.search(r"CurrentTheme\s*:.*?\\([^\\]*?)\.theme", output):
return match.group(1)
else:
raise Exception("Theme not found\n"+output)
def change_theme(theme: str):
""" Changes the theme, but not if the current theme was already set. """
curr_theme = current_theme()
if theme != curr_theme:
print(f"Changing theme to \"{theme}\" from \"{curr_theme}\"")
os.startfile(files.USER_PROFILE.joinpath("AppData", "Local", "Microsoft",
"Windows", "Themes", theme + ".theme"))
# allow time for theme to change
time.sleep(5)
kill_settings()
def set_brightness(b: float):
""" Argument should be float between 0.0 and 1.0 inclusive """
print(f"Setting brightness to {b}")
# command takes ints only
subprocess.run(["powershell", f"(Get-WmiObject -Namespace root/WMI -Class WmiMonitorBrightnessMethods).WmiSetBrightness(1,{math.floor(b*100)})"],
creationflags=subprocess.CREATE_NO_WINDOW)
def set_contrast(c: float):
""" Argument should be float between 0.0 and 1.0 inclusive """
print(f"Setting contrast to {c}")
subprocess.run(["monitorian", "/set", "contrast", "all", str(math.floor(c*100))],
creationflags=subprocess.CREATE_NO_WINDOW)
def set_volume(volume: float):
""" Argument should be float between 0.0 and 1.0 inclusive """
subprocess.run(["nircmd", "setsysvolume", str(volume * 65535)],
creationflags=subprocess.CREATE_NO_WINDOW)
def set_system_sounds_volume(volume: float):
""" Argument should be float between 0.0 and 1.0 inclusive """
subprocess.run(["nircmd", "setappvolume", "systemsounds", str(volume)],
creationflags=subprocess.CREATE_NO_WINDOW)
# open the SoundVolumeView GUI and find the "Command-Line Friendly ID"
SOUND_DEVICE_ID = r"High Definition Audio Device\Device\Headphones\Render"
def set_volume_balance(left: float, right: float):
subprocess.run(["soundvolumeview ", "/setvolumechannels",
SOUND_DEVICE_ID, str(math.floor(left*100)), str(math.floor(right*100))])
# def set_wallpaper(image: files.PathLike):
# this works but sets for all monitors
# # https://c-nergy.be/blog/?p=15291
# image = Path(image).absolute()
# # this Powershell program looks like it does some unnecessary things, but I don't know enough about the syntax to improve it
# subprocess.run(["powershell", f"""$code = @'
# using System.Runtime.InteropServices;
# namespace Win32 {{
# public class Wallpaper {{
# [DllImport("user32.dll", CharSet=CharSet.Auto)]
# static extern int SystemParametersInfo (int uAction, int uParam, string lpvParam, int fuWinIni);
#
# public static void SetWallpaper(string thePath) {{
# SystemParametersInfo(20, 0, thePath, 3);
# }}
# }}
# }}
# '@
# add-type $code
# [Win32.Wallpaper]::SetWallpaper("{str(image)}")"""])
# this does not work consistently
# subprocess.run(["reg", "add", r"HKEY_CURRENT_USER\Control Panel\Desktop","/v", "Wallpaper", "/t", "REG_SZ", "/d", image, "/f"])
# subprocess.run("RUNDLL32.EXE user32.dll, UpdatePerUserSystemParameters", shell=True)
# based on https://stackoverflow.com/questions/66375014/is-it-possible-to-use-idesktopwallpaper-in-python
WALLPAPER_DLL = cdll.LoadLibrary(
str(Path(__file__).parent.joinpath("dll/wallpaper.dll").absolute()))
def set_wallpaper(monitor: int, image: files.PathLike):
WALLPAPER_DLL.SetWallpaper(monitor, str(Path(image).absolute()))