Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save tjarksaul/994f9a5ed5a262b2b6cdbe177a6902cc to your computer and use it in GitHub Desktop.
Save tjarksaul/994f9a5ed5a262b2b6cdbe177a6902cc to your computer and use it in GitHub Desktop.
Automatically setup external monitors
#!/usr/bin/env python3
"""
Automatically setup external monitors at MOIA so that it's to the right of the
internal screen
Prerequisite: [displayplacer](https://github.com/jakehilborn/displayplacer)
```bash
brew tap jakehilborn/jakehilborn && brew install displayplacer
```
Usage
-----
python autosetup_external_monitor.py above
python autosetup_external_monitor.py right
"""
import subprocess
import sys
if __name__ == "__main__":
if len(sys.argv) > 1 and sys.argv[1] == "above":
location_internal_monitor = (0, 1440)
else: # assume to the right
location_internal_monitor = (-2056, 1440-1329)
internal_screen = subprocess.run(
"""displayplacer list | grep -B 2 "MacBook built in screen" | grep "Contextual screen id" | awk -F": " '{print $NF}'""",
shell=True,
capture_output=True,
text=True,
).stdout.strip()
external_screen = subprocess.run(
"""displayplacer list | grep -B 2 "27 inch external" | grep "Contextual screen id" | awk -F": " '{print $NF}'""",
shell=True,
capture_output=True,
text=True,
).stdout.strip()
x, y = location_internal_monitor
displayplacer_command = f"""displayplacer "id:{internal_screen} res:2056x1329 scaling:on origin:({x},{y}) degree:0" """\
f""" "id:{external_screen} res:2560x1440 scaling:on origin:(0,0) degree:0" """
subprocess.run(
displayplacer_command, shell=True, capture_output=True, text=True, check=True
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment