Skip to content

Instantly share code, notes, and snippets.

@readpan
Last active March 6, 2023 06:31
Show Gist options
  • Save readpan/82925befc6a54dd0e732900edab720f6 to your computer and use it in GitHub Desktop.
Save readpan/82925befc6a54dd0e732900edab720f6 to your computer and use it in GitHub Desktop.
MacOS利用displayplacer命令行工具设置分辨率
#!/usr/bin/env python3
import subprocess
import sys
import json
# Run the command and capture the output
output_bytes = subprocess.check_output(
["/opt/homebrew/bin/displayplacer", "list"])
# Decode the output bytes into a string
output_str = output_bytes.decode("utf-8")
class ScreenConfig:
def __init__(self, config_string):
config_list = config_string.split("\n")
self.screen_id = config_list[0].split(":")[1].strip()
self.items = {
"items": []
}
for line in config_list:
# example-> mode 47: res:1280x960 hz:60 color_depth:8 scaling:on
line = line.strip()
if line.startswith("mode"):
info_items = line.split(" ")
mode_num = int(info_items[1].replace(":",""))
scaling = "on" if len(info_items) > 5 else "off"
mode_dict = {
"title": line,
"arg": f'id:{self.screen_id} mode:{mode_num}'
}
self.items['items'].append(mode_dict)
screen_config = ScreenConfig(output_str)
print(json.dumps(screen_config.items))
# todo 根据id不同, 给出一些预设值.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment