Is there a way of coloring a font with a RGB, Lab or CMYK color without adding the color to the document swatches.
The only way I know is to add a color to the swatches or use one that already exists.
like
app.selection[0].characters[0].fillColor=document.colors.add({colorVa lue: [255, 53, 160], space: ColorSpace.RGB});}
This has the undesired effect of cluttering up the swatches when using a lot of colors.
any ideas? ![]()
Using standard methods, the only way to add colors is by adding a swatch.
If you have an unnamed color in your document, you can apply it to another object using obj1.fillColor = obj2.fillColor.
So, if you can get the unnamed color in your document somehow, you can apply it via scripting.
One way to do that would be to write a snippet programmatically with the desired color, import that, apply the color and remove the placed snippet.
You can check if the color exists already in the doc before doing that...
Harbs
Sounds to me like a good idea as well but unlike Harbs I don't have a clue how to apply XML attributes to text in the document. ![]()
Please can you show me how to do this.
Take for example I want to make the second character or word in my current selection the color CMYK\:Process\:0.75\,0.5\,0.53\,0.10
how would I script that? ![]()
Okay...
Here's a multi-purpose function which takes an array of three or four values and returns an unnamed color (either RGB or CMYK) using the methods we discussed here:
function GetUnnamedColor(values){
if( !(values instanceof Array) ){
throw("Values must be an array of three or four values");
}
var fileString = "<ASCII-MAC>\r" +
"<pstyle:><cc:COLOR\:";
if(values.length == 3){
fileString = fileString + "RGB\:Process\:";
for(var i=0;i<values.length;i++){
values[i] = values[i]/255 * 100;
}
}
else if(values.length == 4){
fileString = fileString + "CMYK\:Process\:";
}
else {
throw("Values must be an array of three or four values");
}
for(var i=0;i<values.length;i++){
if(values[i] >= 100){
fileString = fileString + "1";
} else {
values[i] = values[i].toString();
var tempSplit = values[i].split(".");
while(tempSplit[0].length<2){
tempSplit[0] = "0" + tempSplit[0];
}
values[i] = tempSplit.join("");
fileString = fileString + "0."+values[i];
}
if(i<values.length-1){
fileString = fileString + "\,";
}
}
fileString = fileString + ">Just Some Text<cc:>";
var file = File(Folder.temp+"color_import.txt");
file.open('w');
file.write(fileString);
file.close();
var story = app.documents[0].pages[0].place(file)[0];
var color = story.fillColor;
story.textContainers[0].remove();
return color;
}
alert(GetUnnamedColor([75,0.5,53,10]).colorValue);
Harbs
[Edit] It assumes a document to be open. I have real work to do, so I can't make it more robust right now... ![]()
Wow, thanks a lot as I wrote "unlike Harbs I don't have a clue how to apply XML attributes to text in the document" !
I was expecting between 1 and 2 lines of script! (what an Am HaAretz!)
I shall go over the the script to analyse the methods.
Much appreciated, please get on with some real work and have a Kesiva VeHaSima Tova.
Gosh I have no idea what prompted me to visit this thread again ![]()
... Here is a one-liner to set the color of a text selection to any RGB, Lab, or CMYK color without adding it to the Swatches list:
app.selection[0].fillColor.properties = {space:ColorSpace.RGB,colorValue:[255,255,0]};
Impressed? Well, if you try it you'll notice it won't work straight away. There Is A Catch! (Two of 'em, actually.)
.
.
.
.
.
(spoiler space)
.
.
.
.
.
Catch #1: You can only change the color of text if it already has an 'undefined' color!
Now usually, you'll find your text already comes with a swatch applied to it, even though it's simply "Black", and so you cannot use this trick. If you do want to use it, you have to change it first to the fillColor of a text that already has an undefined color applied to it; then you can change it to anything you want. But you cannot create a piece of text to act as model to begin with, using a script (well, other than Harbs' workaround with a temporary snippet file) -- a real Catch-22.
To get it to work, draw a new text frame somewhere on the pasteboard in your document and enter this text into it: "A Jongware Solution". (Use exactly this text.) Change the color to something random, using the Color panel. Use the Script Label panel to add a label to the text frame -- use "color placeholder". Then, just before the line that sets other text to any color, insert this:
app.selection[0].fillColor = app.activeDocument.textFrames.item("color placeholder").characters[0].fillColor;
At first glance, this appears to work, but I noticed The Weirdest Thing. If you apply this color to your newly selected text, you'll find it initially is set to the placeholder color. That's to be expected. The second line (the one at the top of this post) also works, as you will see the color change to the new settings.
However. Catch #2
Check what happens to the placeholder text ... It also changes its color! Curioser & curioser, if you used this trick a couple of times in the same document, you'll find that every item with the same color also changes! It's kinda like ... you have a swatch ... but not really ...
Thanks Jongware
I'm afraid I agree with the spoiler lines you put it ![]()
I think the catches are quite big,
Oh well, but as it happens I was also thinking of this post lately and thought of you because I had seen on another post that you wrote that one can use menu actions
So I know this is a cheat because my question was how to add color with no swatch but I think (based on Harbs and your replys) that its better to add and then remove the swatches and replace the removed swatches with unnamed ones.
But It's easier said than done
Please see the script below and see if you can correct the problem line ![]()
to run the script a letter ... which is colored with a swatch must be selected.
var myMenu = app.menuActions.item('$ID/Swatches_WinMenu');
myMenu.invoke();
var mySwatchRemoveMenuItem = app.menuActions.item('$ID/Delete Swatch...');
mySwatchRemoveMenuItem.invoke();
var makeUnnamedSwatch = app.menuActions.item('$ID/Unnamed Swatch');
// The above line is the problem line I don't know what to put instead of '$ID/Unnamed Swatch'
// to get it to choose replace with unnamed swatch as there dosn't seem to be a valid $ID for it.
makeUnnamedSwatch.invoke();
Once again
Thanks
Hi all, I am trying to accomplish the same goal (except color a textFrame not text) and I am using Harbs method but am only getting black boxes.
var r=0;
var g=102;
var b=255;
var elemBox = currentPage.textFrames.add(); //Add box
var addedColor = GetUnnamedColor([r,g,b]);
elemBox.fillColor = addedColor;
The filestring that gets written to the temp. file is:
<ASCII-MAC>
<pstyle:><cc:COLOR:RGB:Process:0.00,0.40,1>Just Some Text<cc:>
the addedColor.colorValue is 0,0,0,100
Is anyone else experiencing the same results?
You saw this same problem before, without using Harbs's function, right? (Your other thread?) So I don't think it has anything to do with the tagged text. I know it seems like grasping at straws, but, in the absence of any other ideas, can you try clearing your defaults and starting with a new document?
Well, my other thread is a similiar problem and I'm hoping that this solution is a work-around for that (since I was grasping at straws over there as well). I'm actually using an entirely new document (created by script) and am adding rectangles that each have a color assigned to them. So it works using:
var addedColor = myDoc.colors.add({space:ColorSpace.RGB,colorValue:[r,g,b]});
elemBox.fillColor = addedColor;
It just adds them all to the swatches fly-out which I need to avoid as well. So I'm hoping by finding a solution to this problem, it will also solve my other threads problem which is slightly different. Fingers crossed.
Ah the solution! After researching tagged text, I found that on a windows platform you must have <ASCII-WIN>. The colors come through as expected. I will post on my other thread if this solution works to solve that as well. Except I wont be able to test that until tomorrow.
Just on more question. Harbs says above that "You can check if the color exists already in the doc before doing that..." and by that he means executing that method. But how do you check for the unnamed color?
Message was edited by: bduffy323
North America
Europe, Middle East and Africa
Asia Pacific