Copy link to clipboard
Copied
Sorry for a noob question, but I've just started working with Javascript yesturday, and I need to solve my problem fast.
I'm writing an Illustrator plug-in, and some of the code (creating Illustrator object) is done in Javascript. when debugging with ExtendScript Toolkit, the script works fine, however, when debuging with Extension builder, I get some errors, which i don;t know how to solve. Below is a part of my script. Comented are the error which i get.
var fColor = new CMYKColor();
fColor.cyan = 0;
fColor.magenta = 0;
fColor.yellow = 0;
fColor.black = 100;
var sColor = new NoColor();
function makeQI(origin, xDim, bHeight, mFactor, bGroup)
{
var position = new Array ( origin[0] + toPoints(xDim*11 + xDim*3 + xDim*5 + xDim*3 + xDim*7*12 + xDim*7), origin[1] - toPoints(bHeight) - toPoints(kXDim*1.5) );
var pItem = bGroup.pathItems.add();
pItem.fillColor = fColor; // Error #1034: Type Coercion failed: cannot convert flash.external::HostObject@2137fca1 to com.adobe.illustrator.Color.
pItem.filled = true;
pItem.strokeWidth = 0;
pItem.strokeColor = sColor; // Error #1034: Type Coercion failed: cannot convert flash.external::HostObject@2137fca1 to com.adobe.illustrator.Color.
pItem.setEntirePath(cq);
position[0] = position[0] - pItem.width;
pItem.position = position; // Error #1034: Type Coercion failed: cannot convert flash.external::HostObject@2182eca1 to Array.
pItem.resize(mFactor,mFactor,true,false,false,false,100, Transformation.TOPLEFT); // Error #1034: Type Coercion failed: cannot convert flash.external::HostObject@2162dca1 to com.adobe.illustrator.Transformation.
}
Any help would be appreciated
okay, so i've found the reason i got all these errors. it is somewhere in between
...
[ Embed (source= "controllers/testEAN13.js" , mimeType= "application/octet-stream" )]
var myScriptClass:Class;
var jsxInterface:HostObject = HostObject.getRoot(HostObject.extensions[0]);
jsxInterface.eval( new myScriptClass().toString() );
jsxInterface.createB(doc, position, bCode, mFactor, bwReductio
Copy link to clipboard
Copied
…Im not sure about what you are doing… or if you have posted enough of your code… You have not called your function or passed it the colours… Try moving the making of colours inside the function may be…?
Copy link to clipboard
Copied
Muppet Mark wrote:
…Im not sure about what you are doing… or if you have posted enough of your code… You have not called your function or passed it the colours…
I also wonder
Have you tried something like this:
var fColor;
makeQI (/* ... */) //show us your variables can be helpful
function makeQI(origin, xDim, bHeight, mFactor, bGroup)
{
fColor = new CMYKColor();
fColor.cyan = 0;
fColor.magenta = 0;
fColor.yellow = 0;
fColor.black = 100;
alert (origin); // tells you if the right variables are used
alert (xDim);
alert (bHeight);
alert (mFactor);
alert (bGroup);
var position = new Array ( origin[0] + toPoints(xDim*11 + xDim*3 + xDim*5 + xDim*3 + xDim*7*12 + xDim*7), origin[1] - toPoints(bHeight) - toPoints(kXDim*1.5) );
alert (position) // shows you the position - correct or not
// but there is no
kXDim yet ???
var pItem = bGroup.pathItems.add();
pItem.fillColor = fColor; // should work now
pItem.filled = true;
pItem.strokeWidth = 0;
//pItem.strokeColor = sColor;
pItem.setEntirePath(cq);
position[0] = position[0] - pItem.width;
pItem.position = position;
pItem.resize(mFactor,mFactor,true,false,false,false,100, Transformation.TOPLEFT);
}
Copy link to clipboard
Copied
I apologise for the lack of information.
vars fColor and sColor are global (i do hope variables defined outside of functions are considered global in Javascript). I need to use them in other function, so I moved the declaration. I've tried declaring them direclty in the function like you sugessted, but it did not help.
xDim is also global variable, sorry that I failed to include it in the sample code.
Didn't know about the alert function 😃 was wondering how to output variables, to test them, I'll try at work tomorrow, however, i'm quite sure that all the variable that I use have correct values.
The function makeQI is called from main function, which in turn called from actionscript. I can provide more code, if needed, but the makeQI functions fails to work on its own, not depending on other code.
Copy link to clipboard
Copied
There were a couple of other things I would do different… I would always put filled to true before passing a colour… I could not understand colouring a stroke of no width what would be the point…? My first guess as to why you get errors is you function does not know your variables they are out of scope…?
Copy link to clipboard
Copied
the position variable is in the scope and it still gets the error. and once again, i've tried declraring the color variables inside the function, it didn't help.
the stroke is set as no color as a precaution 😃 in case the user accidentally increases stroke width.
good point about fillied=true before the color. i'll change that
Copy link to clipboard
Copied
Something like this works correctly:
var fColor;
fColor = new CMYKColor();
fColor.cyan = 0;
fColor.magenta = 0;
fColor.yellow = 0;
fColor.black = 100;
var left = 100;
var top = 100;
var WidthRect = 200;
var HeightRect = 200;
var mFactor = 50;
makeQI (left, top, WidthRect, HeightRect, mFactor, fColor)
function makeQI(left, top, WidthRect, HeightRect, mFactor, fColor)
{
alert (left); // try this to see the correct variables
//alert (top);
//alert (WidthRect);
//alert (HeightRect);
//alert (mFactor);
var pItem = app.activeDocument.pathItems.rectangle (top, left, WidthRect, WidthRect, false)
pItem.filled = true;
pItem.fillColor = fColor;
pItem.resize(mFactor,mFactor,true,false,false,false,100, Transformation.TOPLEFT);
}
But you don't read carefully. xDim is available, but not kxDim
... var position = new Array ( origin[0] + toPoints(xDim*11 + xDim*3 + xDim*5 + xDim*3 + xDim*7*12 + xDim*7),
origin[1] - toPoints(bHeight) - toPoints(kXDim*1.5) ); alert (position) // shows you the position - correct or not // but there is no kXDim yet ???
...
Copy link to clipboard
Copied
First of all, i tried alert() all my variables, everything seems fine ( the only question is what should a groupItem be alerting? for me it says just "[GroupItem ]" ).
This code:
var fColor;
fColor = new CMYKColor();
fColor.cyan = 0;
fColor.magenta = 0;
fColor.yellow = 0;
fColor.black = 100;
var left = 100;
var top = 100;
var WidthRect = 200;
var HeightRect = 200;
var mFactor = 50;
makeQI (left, top, WidthRect, HeightRect, mFactor, fColor)
function makeQI(left, top, WidthRect, HeightRect, mFactor, fColor)
{
alert (left); // try this to see the correct variables
//alert (top);
//alert (WidthRect);
//alert (HeightRect);
//alert (mFactor);
var pItem = app.activeDocument.pathItems.rectangle (top, left, WidthRect, WidthRect, false)
pItem.filled = true;
pItem.fillColor = fColor;
pItem.resize(mFactor,mFactor,true,false,false,false,100, Transformation.TOPLEFT);
}
does not give me any errors, but the rectangle it creates has black stroke and white fill.
But you don't read carefully. xDim is available, but not kxDim
sorry, I made a typo. xDim was passed as an argument, but kXDim is a global.
Copy link to clipboard
Copied
Your code filled with the right colour when I tried it here… Anyhows you can make use of defaults to do the colouring too…
#target illustrator
var left = 100;
var top = 100;
var WidthRect = 200;
var HeightRect = 200;
var mFactor = 50;
makeQI (left, top, WidthRect, HeightRect, mFactor);
function makeQI(left, top, WidthRect, HeightRect, mFactor)
{
var fColor = new CMYKColor();
fColor.cyan = 100;
fColor.magenta = 0;
fColor.yellow = 0;
fColor.black = 0;
app.activeDocument.defaultStroked = false;
app.activeDocument.defaultFilled = true;
app.activeDocument.defaultFillColor = fColor;
var pItem = app.activeDocument.pathItems.rectangle (top, left, WidthRect, WidthRect, false)
pItem.resize(mFactor,mFactor,true,false,false,false,100, Transformation.TOPLEFT);
}
Copy link to clipboard
Copied
I've simplified my code to the bone. Thats what i've got atm:
in my ActionScript - the code that invokes javascript:
[ Embed (source= "controllers/testEAN13.js" , mimeType= "application/octet-stream" )]
var myScriptClass:Class;
var jsxInterface:HostObject = HostObject.getRoot(HostObject.extensions[0]);
jsxInterface.eval( new myScriptClass().toString() );
jsxInterface.createB(doc, position, bCode, mFactor, bwReduction, bHeight, quietzone, hrInterp, frame);
And thats my entire javascript document:
var point_mm= 2.834645; // 1 mm is 2.834645 points
var kXDim = 0.33;
function getOriginPoint(doc) { // find the center of the screen
var origin = new Array;
try {
origin.push(doc.activeView.centerPoint[0], doc.activeView.centerPoint[1]);
}
catch (error) {
}
return origin == null ? new Array(300, 300) : origin;
}
function toPoints(milimeters) // translate milimeters to points
{
return milimeters * point_mm;
}
function toMilimeters(points) // translate points to milimeters
{
return points / point_mm;
}
function createB(doc, position, bCode, mFactor, bwReduction, bHeight, quietzone, hri, frame)
{
var xDim = kXDim * (mFactor/100);
if (doc == null)
{
doc = app.documents.add();
}
var bGroup = doc.groupItems.add();
var origin = position;
if ( origin == null ) origin = getOriginPoint(doc);
var prevStop = new Array (origin[0], origin[1]);
alert("xDim " + xDim);
alert("doc " + doc);
alert("bGroup " + bGroup);
alert("origin " + origin);
alert("prevStop " + prevStop);
prevStop = makeGuards(prevStop, "normal", xDim, bwReduction, bHeight, bGroup, hri);
app.redraw();
}
function makeGuards(prevStop, arg, xDim, bwReduction, bHeight, bGroup, hri) { // this function creates 1 bar
var fColor = new CMYKColor();
fColor.cyan = 0;
fColor.magenta = 0;
fColor.yellow = 0;
fColor.black = 100;
var sColor = new NoColor();
alert("fColor " + fColor);
alert("sColor " + sColor);
alert("prevStop " + prevStop);
alert("arg " + arg);
alert("xDim " + xDim);
alert("bwReduction " + bwReduction);
alert("bHeight " + bHeight);
alert("bGroup " + bGroup);
alert("hri " + hri);
if ( arg == "center" ) {
// add 1 module space if it is a center guard bar pattern
prevStop[0] = prevStop[0] + toPoints(xDim + bwReduction);
}
// create 1 module bar
var bar1 = bGroup.pathItems.add();
var b1Width = toPoints(xDim - bwReduction);
var b1Height = toPoints(bHeight);
if (hri) b1Height += toPoints(xDim*5);
var b1Array = new Array;
var x1 = prevStop[0];
var y1 = prevStop[1];
var x2 = prevStop[0] + b1Width;
var y2 = prevStop[1] - b1Height;
b1Array.push(new Array(x1, y1), new Array(x1, y2), new Array(x2, y2), new Array(x2, y1));
bar1.closed = true;
bar1.filled = true;
bar1.fillColor = fColor; //Error: ActionScript error: TypeError: Error #1034: Type Coercion failed: cannot convert flash.external::HostObject@17c08161 to com.adobe.illustrator.Color.
bar1.strokeWidth = 0;
bar1.strokeColor = sColor; //Error: ActionScript error: TypeError: Error #1034: Type Coercion failed: cannot convert flash.external::HostObject@17c08161 to com.adobe.illustrator.Color.
bar1.setEntirePath(b1Array);
prevStop[0] = prevStop[0] + b1Width;
alert("bar1 " + bar1);
alert("b1Width " + b1Width);
alert("b1Height " + b1Height);
return prevStop;
}
I still get these !@#$% coercion erros when setting .fillColor and .strokeColor!!! Spent two days trying to figure it out, someone please halp me!
Copy link to clipboard
Copied
Did you try my methods of not changing the properties afterwards and allow the app to create with these colours…?
Copy link to clipboard
Copied
Without your defined variables and without any function-calls we haven't a chance to find the mistake.
Please give us a working (part of your) script.
Copy link to clipboard
Copied
pixxxel schubser wrote:
Without your defined variables and without any function-calls we haven't a chance to find the mistake.
Please give us a working (part of your) script.
the code i wrote in my previous message is all the javascript code I have! and it still does not work! I also showed you part of my actionscript code, that envokes the javascript. There are no other calls to javascript from any parts of my plugin.
Copy link to clipboard
Copied
everything seems to be fine, try
changing this line
bar1.fillColor = fColor;
to this
bar1.fillColor = app.activeDocument.swatches[5].color;
to see if it makes any difference,
Copy link to clipboard
Copied
CarlosCanto wrote:
everything seems to be fine, try
changing this line
bar1.fillColor = fColor;
to this
bar1.fillColor = app.activeDocument.swatches[5].color;
to see if it makes any difference,
good point, migh help us determine of the error is in the fColor variable. I will try it tomorrow at work, thx!
Copy link to clipboard
Copied
sorry, for the delay, couldn't get to work this weekend.
I've tried your suggestion to change fColor to app.activeDocument.swatches[5].color, no luck at all, still the same error
Copy link to clipboard
Copied
Muppet Mark wrote:
Your code filled with the right colour when I tried it here… Anyhows you can make use of defaults to do the colouring too…
#target illustrator var left = 100; var top = 100; var WidthRect = 200; var HeightRect = 200; var mFactor = 50; makeQI (left, top, WidthRect, HeightRect, mFactor); function makeQI(left, top, WidthRect, HeightRect, mFactor) { var fColor = new CMYKColor(); fColor.cyan = 100; fColor.magenta = 0; fColor.yellow = 0; fColor.black = 0; app.activeDocument.defaultStroked = false; app.activeDocument.defaultFilled = true; app.activeDocument.defaultFillColor = fColor; var pItem = app.activeDocument.pathItems.rectangle (top, left, WidthRect, WidthRect, false) pItem.resize(mFactor,mFactor,true,false,false,false,100, Transformation.TOPLEFT); }
That works great! So i think i'll be using that for now. But its realy weird that pathItem.fillColor, pathItem.position and pathItem.resize give me some weird errors.
Also, I have to make some complicated paths, with moer than a 100 pathPoints. If I will be using pathItem.position, i'm afraid i will get the coercion error. Any suggestion, how to avoid that?
Copy link to clipboard
Copied
okay, so i've found the reason i got all these errors. it is somewhere in between
[ Embed (source= "controllers/testEAN13.js" , mimeType= "application/octet-stream" )]
var myScriptClass:Class;
var jsxInterface:HostObject = HostObject.getRoot(HostObject.extensions[0]);
jsxInterface.eval( new myScriptClass().toString() );
jsxInterface.createB(doc, position, bCode, mFactor, bwReduction, bHeight, quietzone, hrInterp, frame);
and
function createB(doc, position, bCode, mFactor, bwReduction, bHeight, quietzone, hri, frame)
{
var xDim = kXDim * (mFactor/100);
if (doc == null)
{
doc = app.documents.add();
}
var bGroup = doc.groupItems.add();
var origin = position;
if ( origin == null ) origin = getOriginPoint(doc);
var prevStop = new Array (origin[0], origin[1]);
alert("xDim " + xDim);
alert("doc " + doc);
alert("bGroup " + bGroup);
alert("origin " + origin);
alert("prevStop " + prevStop);
prevStop = makeGuards(prevStop, "normal", xDim, bwReduction, bHeight, bGroup, hri);
app.redraw();
}
for some reason the doc:Document that I passed down to javascript works totally fine in Actionscript function, but the javascript function that recieves it can not use it correctly. Hence all the errors when with pathItems (they all were created using groupItem.pathItems.add() while groupItem was created using doc.groupItems.add() )
thats not an ideal fix, but i can use app.aciveDocument to declare a new doc variable inside javascript. It will do for now.
If anyone has any idea as to why the doc argument is not passed down correctly, or how to fix it, I would be very greatfull!
Otherwise, thanks everybody!
Find more inspiration, events, and resources on the new Adobe Community
Explore Now