Skip to main content
Electric_Marvel5FEC
Known Participant
March 26, 2021
Answered

Reading the label colors from preferences file

  • March 26, 2021
  • 2 replies
  • 4830 views

Hey,

I am trying to read the label colors from the "Adobe After Effects 18.0 Prefs-indep-general.txt" file. This is how it looks in the file:

["Label Preference Color Section 5"]
   "Label Color ID 2 # 1" = FFB5"88"
   "Label Color ID 2 # 10" = FF8E","9A
   "Label Color ID 2 # 11" = FFE8920D
   "Label Color ID 2 # 12" = FF7F"E*"
   "Label Color ID 2 # 13" = FFF4"m"D6
   "Label Color ID 2 # 14" = FF"="A2A5
   "Label Color ID 2 # 15" = FFA896"w"
   "Label Color ID 2 # 16" = FF1E"@"1E
   "Label Color ID 2 # 2" = FFE4D8"L"
   "Label Color ID 2 # 3" = FFA9CBC7
   "Label Color ID 2 # 4" = FFE5BCC9
   "Label Color ID 2 # 5" = FFA9A9CA
   "Label Color ID 2 # 6" = FFE7C19E
   "Label Color ID 2 # 7" = FFB3C7B3
   "Label Color ID 2 # 8" = FF"g}"E0
   "Label Color ID 2 # 9" = FF"J"A4"L"

 

I tried accessing the first color by using this code:

 

var sectionName = "Label Preference Color Section 5", keyName = "Label Color ID 2 # 1";
var prefFile = PREFType.PREF_Type_MACHINE_INDEPENDENT;
var mypref = app.preferences.getPrefAsString(sectionName, keyName, prefFile);
alert(mypref); // empty string

 

I also tried:

 

var mypref = app.preferences.getPrefAsLong(sectionName, keyName, prefFile);
alert(mypref); // 0
var mypref = app.preferences.getPrefAsFloat(sectionName, keyName, prefFile);
alert(mypref); // 0
var mypref = app.preferences.getPrefAsBool(sectionName, keyName, prefFile);
alert(mypref); // true

 

 Trying to get it from the settings object also returned an empty string: 

app.settings.getSetting(sectionName, keyName, prefFile).
 
I was able to access other preferences (the label name, for example) by doing that, but it doesn't work with the colors. Also, the hex codes in the prefs.txt file look a little weird, does anyone know why they are written that way?
 
Thanks!
Correct answer Justin Taylor-Hyper Brew

Hmm, okay so this is trickier than I thought. So turns out converting with charCodeAt() gets the ASCII value, but while the prefs file is UTF-8 encoded, the color strings are in CP1252 or Windows-1252 encoding.

 

For example the character "ž" in ASCII is 382, which is out of scope for 0-255, while the Windows-1252 value is 158, the correct value.

 

You can test this by going to https://www.rapidtables.com/convert/number/ascii-to-hex.html

and selecting "Windows-1252" in character encoding, pasting the value for "Label Color ID 2 # 6" "ÿçÁž" and the result it will give you is correct: "ff e7 c1 9e".

 

The issue now is that I'm not aware of a built-in way to convert that with extendScript. I found a library online that works with node.js, so we would either need to find an ES3/ExtendScript compatible library for this conversion, or roll our own.

 

Also, looks like you're not the first one to run into this issue, found a few other similar threads without a definitive result:

 

https://community.adobe.com/t5/after-effects/color-labels/td-p/5644065

https://community.adobe.com/t5/after-effects/get-label-colors-from-pref/td-p/7716338

https://community.adobe.com/t5/after-effects/convert-the-ascii-color-code-to-hex/m-p/11197128

 

One alternative solution for values that are beyond 255, is to read the txt file and build your own parser, but I think finding or building a converter table of some sort would be the best approach.

 


Okay, this table seems to work for ASCII-extended characters. No garuntees, but looks like it checks out with the current colors. Built the table from this table https://theascii.com/extended-ascii-table/

 

var table1252 = {
"€":128,
"‚":130,
"ƒ":131,
"„":132,
"…":133,
"†":134,
"‡":135,
"ˆ":136,
"‰":137,
"Š":138,
"‹":139,
"Œ":140,
"Ž":142,
"‘":145,
"’":146,
"“":147,
"”":148,
"•":149,
"–":150,
"—":151,
"˜":152,
"™":153,
"š":154,
"›":155,
"œ":156,
"ž":158,
"Ÿ":159,
"¡":161,
"¢":162,
"£":163,
"¤":164,
"¥":165,
"¦":166,
"§":167,
"¨":168,
"©":169,
"ª":170,
"«":171,
"¬":172,
"�­":173,
"®":174,
"¯":175,
"°":176,
"±":177,
"²":178,
"³":179,
"´":180,
"µ":181,
"¶":182,
"·":183,
"¸":184,
"¹":185,
"º":186,
"»":187,
"¼":188,
"½":189,
"¾":190,
"¿":191,
"À":192,
"Á":193,
"Â":194,
"Ã":195,
"Ä":196,
"Å":197,
"Æ":198,
"Ç":199,
"È":200,
"É":201,
"Ê":202,
"Ë":203,
"Ì":204,
"Í":205,
"Î":206,
"Ï":207,
"Ð":208,
"Ñ":209,
"Ò":210,
"Ó":211,
"Ô":212,
"Õ":213,
"Ö":214,
"×":215,
"Ø":216,
"Ù":217,
"Ú":218,
"Û":219,
"Ü":220,
"Ý":221,
"Þ":222,
"ß":223,
"à":224,
"á":225,
"â":226,
"ã":227,
"ä":228,
"å":229,
"æ":230,
"ç":231,
"è":232,
"é":233,
"ê":234,
"ë":235,
"ì":236,
"í":237,
"î":238,
"ï":239,
"ð":240,
"ñ":241,
"ò":242,
"ó":243,
"ô":244,
"õ":245,
"ö":246,
"÷":247,
"ø":248,
"ù":249,
"ú":250,
"û":251,
"ü":252,
"ý":253,
"þ":254,
"ÿ":255
};

function getLabelsFromPrefs(){
  $.appEncoding = 'CP1252';

  var sectionName = "Label Preference Color Section 5";
  var prefFile = PREFType.PREF_Type_MACHINE_INDEPENDENT;
  var keyName;
  var mypref;
  var resArray = [];

  for(var i = 1; i <= 16; i++){
    keyName = "Label Color ID 2 # " + i.toString();
    mypref = app.preferences.getPrefAsString(sectionName, keyName, prefFile);

    var res = '';
    for(var j = 1; j < mypref.length; j++) {
            var charCode = mypref.charCodeAt(j);
    if(charCode > 254){
        charCode = table1252[mypref[j]]
    }
    var newCode = charCode.toString(16).toUpperCase();
    if(newCode.toString().length === 1){
        newCode = '0'+newCode;
    }
    // alert(mypref[i] +' is '+charCode)
        res += newCode;
    }
    resArray.push(res);
  }
    return resArray;
};

// B53838, E4D84C, A9CBC7, E5BCC9, A9A9CA, E7C19E, B3C7B3, 677DE0, 4AA44C, 8E2C9A, E8920D, 7F452A, F46DD6, 3DA2A5, A89677, 1E401E

alert(
"B53838 - " + getLabelsFromPrefs()[0] + " - " + (getLabelsFromPrefs()[0] === "B53838") + "\n"+
"E4D84C - " + getLabelsFromPrefs()[1] + " - " + (getLabelsFromPrefs()[1] === "E4D84C") + "\n"+
"A9CBC7 - " + getLabelsFromPrefs()[2] + " - " + (getLabelsFromPrefs()[2] === "A9CBC7") + "\n"+
"E5BCC9 - " + getLabelsFromPrefs()[3] + " - " + (getLabelsFromPrefs()[3] === "E5BCC9") + "\n"+
"A9A9CA - " + getLabelsFromPrefs()[4] + " - " + (getLabelsFromPrefs()[4] === "A9A9CA") + "\n"+
"E7C19E - " + getLabelsFromPrefs()[5] + " - " + (getLabelsFromPrefs()[5] === "E7C19E") + "\n"+ 
"B3C7B3 - " + getLabelsFromPrefs()[6] + " - " + (getLabelsFromPrefs()[6] === "B3C7B3") + "\n"+
"677DE0 - " + getLabelsFromPrefs()[7] + " - " + (getLabelsFromPrefs()[7] === "677DE0") + "\n"+
"4AA44C - " + getLabelsFromPrefs()[8] + " - " + (getLabelsFromPrefs()[8] === "4AA44C") + "\n"+
"8E2C9A - " + getLabelsFromPrefs()[9] + " - " + (getLabelsFromPrefs()[9] === "8E2C9A") + "\n"+ 
"E8920D - " + getLabelsFromPrefs()[10] + " - " + (getLabelsFromPrefs()[10] === "E8920D") + "\n"+ 
"7F452A - " + getLabelsFromPrefs()[11] + " - " + (getLabelsFromPrefs()[11] === "7F452A") + "\n"+
"F46DD6 - " + getLabelsFromPrefs()[12] + " - " + (getLabelsFromPrefs()[12] === "F46DD6") + "\n"+
"3DA2A5 - " + getLabelsFromPrefs()[13] + " - " + (getLabelsFromPrefs()[13] === "3DA2A5") + "\n"+
"A89677 - " + getLabelsFromPrefs()[14] + " - " + (getLabelsFromPrefs()[14] === "A89677") + "\n"+ 
"1E401E - " + getLabelsFromPrefs()[15] + " - " + (getLabelsFromPrefs()[15] === "1E401E")
);

 

2 replies

zsaaro
Inspiring
April 21, 2022

Hey! Here's how I deal with it in my CEP Panel:

https://github.com/GoodBoyNinja/Get-Layers-Label-Colors-using-CEP

Justin Taylor-Hyper Brew
Community Expert
Community Expert
April 2, 2021

A few steps are required.  (updated)

 

// Encoding must be default
$.appEncoding = 'CP1252'

var sectionName = "Label Preference Color Section 5", keyName = "Label Color ID 2 # 1";
var prefFile = PREFType.PREF_Type_MACHINE_INDEPENDENT;
var mypref = app.preferences.getPrefAsString(sectionName, keyName, prefFile);

// mypref is now ÿµ88 
var srcStr = mypref.toSource(); // (new String("\u00FF\u00B588"))
var res = srcStr.match(/\"(.*)\"/)[0].replace(/\"/g, '').replace(/\\\u00/g, '')

alert(res); // FFB588

 

 

Electric_Marvel5FEC
Known Participant
April 2, 2021

Thank you so much, it works perfectly! But do you know how can I get the hex code from Preferences > Labels > Label Colors (in your example it would be B53838)? Thanks for the help!

Justin Taylor-Hyper Brew
Community Expert
Community Expert
April 2, 2021

The result is FFB588, but what I'm looking for is the hex code you see in the preferences (not the preferences .txt file, the ones in the AE interface). In the .txt file the hex code is FFB588, but in the preferences menu in AE it's B53838. It shouldn't be that complicated to find the right hex code, but I just can't figure out how to do it.


Yea, the issue is there are 4 characters resulting ARGB (alpha, red, green, blue). We can disregard the first Alpha since it'll always be 100% or FF. So as you see the 2nd example I posted above, jumps to the 2nd character through the 4th character to get the RGB values and converts them to hex.