Skip to main content
Participating Frequently
May 24, 2022
Answered

Error 24: String().startsWith is not a function

  • May 24, 2022
  • 2 replies
  • 3697 views

I'm getting this error when running a javascript file in Illustrator using C#: "Exception has been thrown by the target of an invocation. Error 24: String().startsWith is not a function.\rLine: 2\r-> alert(String(test).startsWith(\"PANTONE\"));"

 

var test = 'PANTONE Test';
alert(String(test).startsWith("PANTONE"));
var xCoord = 643.9;
var yCoord = -286.2;
var boxSpace = 10;
var textOffset = 10;
var textYStart = yCoord - textOffset - 7.5;
var boxYStart = yCoord - boxSpace;
//var excludeSwatches = ["[None]", "[Registration]"];
var embColorGroupItem = activeDocument.groupItems['EMB GROUP'];
var swatches = activeDocument.swatches;

for (var i = 0; i < swatches.length; i++) {
    var swatch = swatches[i];
    var name = String(swatch.Name);

    if (name != "[None]" && name != "[Registration]" && !name.startsWith("PANTONE")) {
        var rect = embColorGroupItem.pathItems.rectangle(boxYStart, xCoord, 23.61328125, 8.34423828125);
        rect.Stroked = true;
        rect.StrokeWidth = 1;

        var text = embColorGroupItem.textFrames.Add();
        text.contents = name;
        text.translate(xCoord + 30, textYStart);
        boxYStart -= boxSpace;
        textYStart -= textOffset;
    }
}

 

Is this an illustrator limitation or a JavaScript issue on our server?

This topic has been closed for replies.
Correct answer CarlosCanto

Illustrator's version of javascript (ES3) does not support startsWith, use indexOf instead

 

 

var test = 'PANTONE Test';
alert(test.indexOf("PANTONE"));

 

 

this is javascript though, not C#

2 replies

CarlosCanto
Community Expert
CarlosCantoCommunity ExpertCorrect answer
Community Expert
May 24, 2022

Illustrator's version of javascript (ES3) does not support startsWith, use indexOf instead

 

 

var test = 'PANTONE Test';
alert(test.indexOf("PANTONE"));

 

 

this is javascript though, not C#

Participating Frequently
May 24, 2022

Thanks! That works, I'll write older javascript. That must also explain why I can't set the swatch to a variable and access it's properties.

CarlosCanto
Community Expert
Community Expert
May 24, 2022

I'm not sure how C# drives Illustrator, there's no issues setting swatches to variables in javascript, vbs, vba 

femkeblanco
Legend
May 24, 2022

Illustrator uses ES3.  startsWith() was introduced in ES5 or ES6.  Try a polyfill.  

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/startsWith 

Participating Frequently
May 24, 2022

Thank you, that's extremely outdated but will help me to not try advanced features. Not sure what a polyfill would do for me though?

Inventsable
Legend
May 25, 2022

A polyfill is code that will add some missing function to older versions of JS, i.e. you could add a polyfill so that startsWith does exist and can be used:

 

String.prototype.startsWith = function (searchStr) {
    return new RegExp("^" + searchStr).test(this);
}

var test1 = "foo bar".startsWith("foo"),  // true
    test2 = "foo bar".startsWith("bar"),  // false
    test3 = "^$^$foobar".startsWith("^$"); // false, unless polyfill escapes special chars