Copy link to clipboard
Copied
Hi Community hope you all are doing great,
can somebody help me creating a script that takes all the swatches in the document and turn them into text with color, like the sample attach? If is possible to make a setting section in which I can change if I cant it horizontal or vertical I will appreciate it, thank you all and have a nice day.
There are few options (each with pros and cons): you could do trial and error, manually changing width (+/- height) in line 3; you could switch to point type and not be bound by width; you could make width and height proportional to font size; or you could vary width according to content. I've used a combination of the latter two below.
var orientation = "vertical"; // or "horizontal"
var size = 10;
var w = size * 2, h = size, x = 0, y = 0; // width, height, left and top
var doc = app.activ
...
Copy link to clipboard
Copied
var orientation = "horizontal"; // or "vertical"
var size = 10;
var w = 20, h = 10, x = 0, y = 0;
var doc = app.activeDocument;
var swatches = doc.swatches;
var group = doc.groupItems.add();
for (var i = 2; i < swatches.length; i++) {
var rect = doc.pathItems.rectangle(y, x, w, h);
if (orientation == "horizontal") x += w;
if (orientation == "vertical") y -= h;
var text = doc.textFrames.areaText(rect);
text.contents = "text";
text.textRange.size = size;
text.textRange.fillColor = swatches[i].color;
text.move(group, ElementPlacement.PLACEATEND);
}
Copy link to clipboard
Copied
thank you @femkeblanco !! Can I ask you for one correction? When I change the size, or if the text is too long, it appears like this.
Is there a way to make the text frame adaptable? Because otherwise I need to make the frame bigger manually with the mouse, thank you!
Copy link to clipboard
Copied
There are few options (each with pros and cons): you could do trial and error, manually changing width (+/- height) in line 3; you could switch to point type and not be bound by width; you could make width and height proportional to font size; or you could vary width according to content. I've used a combination of the latter two below.
var orientation = "vertical"; // or "horizontal"
var size = 10;
var w = size * 2, h = size, x = 0, y = 0; // width, height, left and top
var doc = app.activeDocument;
var swatches = doc.swatches;
var group = doc.groupItems.add();
for (var i = 2; i < swatches.length; i++) {
var rect = doc.pathItems.rectangle(y, x, w, h);
var text = doc.textFrames.areaText(rect);
text.contents = swatches[i].name;
text.textRange.size = size;
text.textRange.fillColor = swatches[i].color;
while (text.lines[0].characters.length < text.characters.length) {
text.textPath.width += size;
}
if (orientation == "horizontal") x += text.textPath.width;
if (orientation == "vertical") y -= h;
text.move(group, ElementPlacement.PLACEATEND);
}
Copy link to clipboard
Copied
Thanks a lot!
Copy link to clipboard
Copied