macosterminal

Change macOS Terminal theme background colour programatically


How can I change the background colour of a macOS Terminal theme using the command line?

I can see all the themes using:

defaults read com.apple.Terminal "Window Settings"

The output lists each installed theme. Here is the part for the Pro theme, which I am interested in changing.

{
...
    Pro =     {
        BackgroundBlur = 0;
        BackgroundColor = {length = 248, bytes = 0x62706c69 73743030 d4010203 04050607 ... 00000000 000000bf };
        CursorColor = {length = 246, bytes = 0x62706c69 73743030 d4010203 04050607 ... 00000000 000000bd };
        Font = {length = 259, bytes = 0x62706c69 73743030 d4010203 04050607 ... 00000000 000000c7 };
        FontAntialias = 0;
        FontWidthSpacing = "0.9959677419354839";
        ProfileCurrentVersion = "2.07";
        SelectionColor = {length = 246, bytes = 0x62706c69 73743030 d4010203 04050607 ... 00000000 000000bd };
        ShowWindowSettingsNameInTitle = 0;
        TextBoldColor = {length = 237, bytes = 0x62706c69 73743030 d4010203 04050607 ... 00000000 000000b4 };
        TextColor = {length = 246, bytes = 0x62706c69 73743030 d4010203 04050607 ... 00000000 000000bd };
        name = Pro;
        shellExitAction = 2;
        type = "Window Settings";
    };
...
}

I don't think that I can set a subkey so I would be happy to write the entire profile. Here is a command that I have started building up. The text and number fields seem to work but the BackgroundColor field does not work. I can see that it is being treated as a string, when it needs to be data, and I can see that there is data missing from it.

defaults write com.apple.Terminal "Window Settings" -dict-add Pro "{\
  BackgroundBlur = 0;\
  BackgroundColor = {length = 248; bytes = '0x62706c69 73743030 d4010203 04050607 ... 00000000 000000b4';};\
  FontAntialias = 0;\
  name = Pro2;\
}"

Solution

  • This bash script changes the background color of the Pro profile to opaque black:

    property_file=~/Library/Preferences/com.apple.Terminal.plist
    color="YnBsaXN0MDDUAQIDBAUGBwpYJHZlcnNpb25ZJGFyY2hpdmVyVCR0b3BYJG9iamVjdHMSAAGGoF8QD05TS2V5ZWRBcmNoaXZlctEICVRyb290gAGjCwwTVSRudWxs0w0ODxAREldOU1doaXRlXE5TQ29sb3JTcGFjZVYkY2xhc3NCMAAQA4AC0hQVFhdaJGNsYXNzbmFtZVgkY2xhc3Nlc1dOU0NvbG9yohYYWE5TT2JqZWN0CBEaJCkyN0lMUVNXXWRseYCDhYeMl6CoqwAAAAAAAAEBAAAAAAAAABkAAAAAAAAAAAAAAAAAAAC0"
    old=`plutil -extract "Window Settings.Pro" xml1 $property_file -o -`
    new=`plutil -replace "BackgroundColor" -data $color - -o - <<< "$old"`
    plutil -replace "Window Settings.Pro" -xml "$new" $property_file
    

    I don't understand the format of the colors. I got this base64 color by editing a profile and inspecting the XML with this command:

    plutil -extract "Window Settings.Pro" xml1 ~/Library/Preferences/com.apple.Terminal.plist -o -
    

    For the rest of the commands, man plutil does a good job of explaining what the options do.

    At a higher level, this script extracts the entire Pro profile from the property file and stores the XML in a variable. It then replaces the background colour and stores the resulting XML in another variable. Finally, it writes the entire edited profile back the the property file.

    The terminal needs to be restarted to see the changes.