Created
April 21, 2020 14:47
-
-
Save axeII/234e45a77b870b6eecaeeb7bb3036da8 to your computer and use it in GitHub Desktop.
Change terminal profile based on dark mode in macOS Catalina
This file contains 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
#!/usr/bin/python3 | |
import asyncio | |
import sys | |
SCRIPT = """ | |
tell application "Terminal" | |
set default settings to settings set "{}" | |
end tell | |
tell application "Terminal" | |
set current settings of tabs of windows to settings set "{}" | |
end tell | |
""" | |
THEMES = { | |
True: "One Dark", | |
False: "One Light", | |
} | |
async def get_mode(): | |
proc = await asyncio.create_subprocess_exec( | |
*["defaults", "read", "-g", "AppleInterfaceStyle"], | |
stdout=asyncio.subprocess.PIPE, | |
stderr=asyncio.subprocess.PIPE, | |
) | |
out_data = await proc.stdout.readline() | |
return out_data.decode() | |
async def set_terminal_theme(output): | |
osascript = SCRIPT.format(THEMES["Dark" in output]) | |
await asyncio.create_subprocess_exec(*["osascript", "-e", osascript]) | |
async def main(input): | |
while len(input)>1: | |
out = await get_mode() | |
await set_terminal_theme(out) | |
await asyncio.sleep(5) | |
if __name__ == "__main__": | |
loop = asyncio.get_event_loop() | |
try: | |
loop.run_until_complete(main(sys.argv)) | |
except KeyboardInterrupt: | |
pass | |
finally: | |
loop.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment