Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

getting different type coercion errors

Explorer ,
May 16, 2013 May 16, 2013

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

TOPICS
Scripting
2.6K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Explorer , May 20, 2013 May 20, 2013

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

...
Translate
Adobe
Guru ,
May 16, 2013 May 16, 2013

…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…?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 16, 2013 May 16, 2013

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);

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
May 16, 2013 May 16, 2013

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guru ,
May 16, 2013 May 16, 2013

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…?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
May 16, 2013 May 16, 2013

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 16, 2013 May 16, 2013

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 ???
...
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
May 17, 2013 May 17, 2013

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guru ,
May 17, 2013 May 17, 2013

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);

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
May 17, 2013 May 17, 2013

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!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guru ,
May 17, 2013 May 17, 2013

Did you try my methods of not changing the properties afterwards and allow the app to create with these colours…?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 17, 2013 May 17, 2013

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
May 17, 2013 May 17, 2013

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 17, 2013 May 17, 2013

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,

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
May 17, 2013 May 17, 2013

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!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
May 20, 2013 May 20, 2013

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
May 20, 2013 May 20, 2013

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?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
May 20, 2013 May 20, 2013
LATEST

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!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines