2

I'm looking for a way to generate a swatch library from a JSON file in Adobe Photoshop CS6. Unfortunately, I can't seem to find anything on the web about this, and I don't know how to write my own plugin/script that will do this for me.

If there is a free script/plugin out there that will do this can you please send me the link. If nothing exists can you direct me to a place where I can find a template for a Photoshop CS6 script and to some sample code that can make a swatch from a given name and RGB values?

  • 1
    What's the JSON look like? – Cai Mar 31 '17 at 9:55
0

the simplest method to import a swatch from an external file is to have an HTML with all the colors - HEX or RGB format.

  1. Click the Menu on the Swatches panel.
  2. Select in the menu "Load swatches".
  3. Find your HTML file with all the colors and import it.

<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>Untitled Document</title>
</head>

<body>

#112233, rgb(1,2,3)
</body>
</html>

In your swatches you will have the new colors - #112233 and #010203.

0

An old question but maybe this will help to someone. Scripting on a basic level is very simple in Phootshop and usually comes down to several steps:

  • If you have no idea about Javascript you go to code academy, udemy or whatever site you like and learn basic things like variables, arrays and functions — this will take only several hours;
  • you download and install ScriptingListener plug-in. This plugin records most of the things that happen in Photoshop as javascript code that looks ugly but who cares;
  • you google the things you don't understand or don't know (like how to read a json file) — javascript is very popular, if you want to do something there were thousands people who tried to do the same;
  • you wrap this all to a script.

For instance. If I create a new Swatch, ScriptingListener will record this code to a log file on Desktop:

// =======================================================
var idMk = charIDToTypeID( "Mk  " );
    var desc4 = new ActionDescriptor();
    var idnull = charIDToTypeID( "null" );
        var ref1 = new ActionReference();
        var idClrs = charIDToTypeID( "Clrs" );
        ref1.putClass( idClrs );
    desc4.putReference( idnull, ref1 );
    var idUsng = charIDToTypeID( "Usng" );
        var desc5 = new ActionDescriptor();
        var idNm = charIDToTypeID( "Nm  " );
        desc5.putString( idNm, "my swatch" );
        var idClr = charIDToTypeID( "Clr " );
            var desc6 = new ActionDescriptor();
            var idRd = charIDToTypeID( "Rd  " );
            desc6.putDouble( idRd, 62.000000 );
            var idGrn = charIDToTypeID( "Grn " );
            desc6.putDouble( idGrn, 62.000000 );
            var idBl = charIDToTypeID( "Bl  " );
            desc6.putDouble( idBl, 62.000000 );
        var idRGBC = charIDToTypeID( "RGBC" );
        desc5.putObject( idClr, idRGBC, desc6 );
    var idClrs = charIDToTypeID( "Clrs" );
    desc4.putObject( idUsng, idClrs, desc5 );
    var idpushToDesignLibraries = stringIDToTypeID( "pushToDesignLibraries" );
    desc4.putBoolean( idpushToDesignLibraries, false );
executeAction( idMk, desc4, DialogModes.NO );

while it's very difficult to read you can see that there're three values for RGB colors and there's a name field that has a my swatch string. So I can wrap this to a function and forget about it:

function createNewSwatch(name, red, green, blue)
{
  // =======================================================
  var idMk = charIDToTypeID("Mk  ");
  var desc4 = new ActionDescriptor();
  var idnull = charIDToTypeID("null");
  var ref1 = new ActionReference();
  var idClrs = charIDToTypeID("Clrs");
  ref1.putClass(idClrs);
  desc4.putReference(idnull, ref1);
  var idUsng = charIDToTypeID("Usng");
  var desc5 = new ActionDescriptor();
  var idNm = charIDToTypeID("Nm  ");
  desc5.putString(idNm, name); // here goes a name property
  var idClr = charIDToTypeID("Clr ");
  var desc6 = new ActionDescriptor();
  var idRd = charIDToTypeID("Rd  ");
  desc6.putDouble(idRd, red); // red goes here
  var idGrn = charIDToTypeID("Grn ");
  desc6.putDouble(idGrn, green); // green goes here
  var idBl = charIDToTypeID("Bl  ");
  desc6.putDouble(idBl, blue); // blue goes here
  var idRGBC = charIDToTypeID("RGBC");
  desc5.putObject(idClr, idRGBC, desc6);
  var idClrs = charIDToTypeID("Clrs");
  desc4.putObject(idUsng, idClrs, desc5);
  var idpushToDesignLibraries = stringIDToTypeID("pushToDesignLibraries");
  desc4.putBoolean(idpushToDesignLibraries, false);
  executeAction(idMk, desc4, DialogModes.NO);
}

so later we can do something like createNewSwatch('cool swatch', 255, 0, 0); to create a new red swatch.

Reading a json depends on how the json looks like. If it's a plain json like this:

var data = {
  colors: [
  {
    name: "calming grey",
    color: [92, 94, 104]
  },{
    name: "alarming grey",
    color: [54, 42, 42]
  },]
}

we can simply eval this file:

$.evalFile('/E/temp/temp_js/myjson.json')
alert(data); // will alert [Object]

if it's stringified like this:

{"colors":[{"name":"calming grey","color":[92,94,104]},{"name":"alarming grey","color":[54,42,42]}]}

we'll need to include a json library that you can find on the internets to read and parse the file. But how do we read a file? We google that and wrap in a function, something like that:

function readFile(link) {
    var b = new File(link);
    b.open('r');
    var str = "";
    while(!b.eof)
    str += b.readln();
    b.close();
    return str
}

Now we have everything we need, so we just make a loop to go through all the colors of the json:

// to include json library
//@include "json2.js"

function main()
{

  try
  {
    var data = readFile('/E/temp/temp_js/myjson-string.json');
  }
  catch (e)
  {
    alert(e);
    return false; // if file isn't found, terminate
  }

  var dataObject = JSON.parse(data); // parse string to an object

  //for each element of my dataObject.color array:
  for (var i = 0; i < dataObject.colors.length; i++)
  {
    // create a new swatch with element name and element color
    createNewSwatch(dataObject.colors[i].name,
      dataObject.colors[i].color[0],
      dataObject.colors[i].color[1],
      dataObject.colors[i].color[2])
  }

  /////////////////////////////////////////////////////////////////////////////////////
  /////////////////////////////////////////////////////////////////////////////////////
  /////////////////////////////////////////////////////////////////////////////////////

  // function to read a file
  function readFile(link)
  {
    var b = new File(link);
    if (!b.exists) throw 'File ' + link + ' not found';
    b.open('r');
    var str = "";
    while (!b.eof)
      str += b.readln();
    b.close();
    return str
  }

  //function to create a swatch
  function createNewSwatch(name, red, green, blue)
  {
    // =======================================================
    var idMk = charIDToTypeID("Mk  ");
    var desc4 = new ActionDescriptor();
    var idnull = charIDToTypeID("null");
    var ref1 = new ActionReference();
    var idClrs = charIDToTypeID("Clrs");
    ref1.putClass(idClrs);
    desc4.putReference(idnull, ref1);
    var idUsng = charIDToTypeID("Usng");
    var desc5 = new ActionDescriptor();
    var idNm = charIDToTypeID("Nm  ");
    desc5.putString(idNm, name);
    var idClr = charIDToTypeID("Clr ");
    var desc6 = new ActionDescriptor();
    var idRd = charIDToTypeID("Rd  ");
    desc6.putDouble(idRd, red);
    var idGrn = charIDToTypeID("Grn ");
    desc6.putDouble(idGrn, green);
    var idBl = charIDToTypeID("Bl  ");
    desc6.putDouble(idBl, blue);
    var idRGBC = charIDToTypeID("RGBC");
    desc5.putObject(idClr, idRGBC, desc6);
    var idClrs = charIDToTypeID("Clrs");
    desc4.putObject(idUsng, idClrs, desc5);
    var idpushToDesignLibraries = stringIDToTypeID("pushToDesignLibraries");
    desc4.putBoolean(idpushToDesignLibraries, false);
    executeAction(idMk, desc4, DialogModes.NO);
  }

}
main()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.