Skip to content

Instantly share code, notes, and snippets.

@axeII
Created April 21, 2020 14:47
Show Gist options
  • Save axeII/234e45a77b870b6eecaeeb7bb3036da8 to your computer and use it in GitHub Desktop.
Save axeII/234e45a77b870b6eecaeeb7bb3036da8 to your computer and use it in GitHub Desktop.
Change terminal profile based on dark mode in macOS Catalina
#!/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