What app are you dealing with?
Try this (untested):
var color:HostObject = new LabColor().hostObjectDelegate; color.l = colorObj.L; color.a = colorObj.a; color.b = colorObj.b; var swatch:Swatch = app.activeDocument.swatches.add(); var hostSwatch:HostObject = swatch.hostObjectDelegate; hostSwatch.name = colorObj.name; hostSwatch.color = color; var swatchgroup:SwatchGroup = app.activeDocument.swatchGroups.add(); swatchgroup.addSwatch(swatch);
It does not work. The color:HostObject is null, so it crashes.
Then I just tried with :
var color:LabColor = new LabColor();
color.l = colorObj.L;
color.a = colorObj.a;
color.b = colorObj.b;
var swatch:Swatch = app.activeDocument.swatches.add();
var hostSwatch:HostObject = swatch.hostObjectDelegate;
hostSwatch.name = colorObj.name;
hostSwatch.color = color;
var swatchgroup:SwatchGroup = app.activeDocument.swatchGroups.add();
And this crashes on the line hostSwatch.color = color with Error: InternalError
I used flash.external.HostObject, maybe that's the wrong package in this causes the error. What package providing HostObject did you mean?
Thank you very much for your answer.
flash.external.HostObject is correct.
A bit of background:
hostObjectDelegate is a way of getting a reference to the ExtendScript DOM object used behind the scenes. All the ActionScript classes are a lot of smoke and mirror. Everything is in fact delegated to the standard scripting engine behind the scenes. If something fails in ActionScript, yo'd generally try to do it in ExtendScript. Therer's two ways of doing that: You can use hostObjectDelegates, but you cannot assign ActionScript classes to hostObjectDelegates. You'd need to assign hostObjectDelegates to other hostObjectDelegates (because you are in fact dealing with ExtendScript "directly").
The other way of handling it would be to write ExtendScript directly. You can either try to just get a reference to an ExtendScript LabColor object, or add the whol swatch in ExtendScript. For more info on that, read this:
Ok, thank you very much.
But if LabColor().hostObjectDelegate allready returns null this would mean this object does not exist in the ExtendScript DOM. Am I correct?
I tried to add a LabColor with an Illustrator script. (First I used an embedded script, and then tried the script directly)
var color = new LabColor();
color.l = L;
color.a = a;
color.b = b;
var swatchgroup = app.activeDocument.swatchGroups.add();
swatchgroup.name = "test swatchgroup from jsx script";
var swatch = app.activeDocument.swatches.add();
swatch.name = swatchName;
swatch.color = color;
The script crashes on the Line 'swatch.color = color;' with "Error 1202: Not implemented"
Do there remain any other option to add a Lab based color to the swatch.
It looks to me like this is a pretty huge bug. File it here: https://www.adobe.com/cfusion/mmform/index.cfm?name=wishform
I'd ask on the Illustrator forum if anyone has any workarounds. http://forums.adobe.com/community/illustrator/illustrator_scripting
Everytime I deal with Illustrator scripting, I'm reminded of how spoiled I am in InDesign...