Get Selected Color PresetΒΆ

This script prints to stdout the name of the color preset the current sesssion is using, or None if none of them matches.

To see the output you can either run pip3 install iterm2 and then execute this script from the command line or run it from the Scripts menu and view the output in the Script Console.

#!/usr/bin/env python3

import iterm2

def ColorsUnequal(profile_color, preset_color):
    return (round( != round( or
            round( != round( or
            round( != round( or
            round(profile_color.alpha) != round(preset_color.alpha) or
            profile_color.color_space != preset_color.color_space)

def ProfileUsesPreset(profile, preset):
    for preset_color in preset.values:
        key = preset_color.key
        profile_color = profile.get_color_with_key(key)
        if ColorsUnequal(profile_color, preset_color):
            return False
    return True

async def PresetForProfile(connection, profile):
    presets=await iterm2.ColorPreset.async_get_list(connection)
    for preset_name in presets:
        preset=await iterm2.ColorPreset.async_get(connection, preset_name)
        if ProfileUsesPreset(profile, preset):
          return preset_name
    return None

async def main(connection):
    app = await iterm2.async_get_app(connection)
    profile=await session.async_get_profile()
    print(await PresetForProfile(connection, profile))