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

Scripting constructor definition work-around.

Valorous Hero ,
Oct 31, 2019 Oct 31, 2019

I have experienced some new errors I have not seen before in scripting and have come back here to share my quick solution.

First, the problem was occurring when using an Illustrator javascript type in an instanceof comparison. The exact issue was (item instanceof Swatch) to check if the input to a function was really a Swatch object or say, a string, etc.

While this did work before, now I got the surprising error that "Swatch" is undefined - and not only that but the entire script stops at that point.

 

Here is a result when a try-catch is used:

 

	try {
		alert(Swatch);
	} catch (e) {
		alert(e);
	}

 

Screen Shot 2019-10-31 at 4.59.41 AM.png

Now how could an Illustrator constructor be undefined? After all, it's right there in the PDF documentation still. I went back and checked and it's still there - so what gives? I then ran a piece of code 

 

	var doc = app.activeDocument;
	try {
		alert(doc.swatches[0].constructor.name);
		alert(Swatch);
	} catch (e) {
		alert(e);
	}

 

Screen Shot 2019-10-31 at 5.02.13 AM.pngScreen Shot 2019-10-31 at 5.02.21 AM.png

Aha! It works!

So the solution, in case you run into the same issue with other constructors, etc - just reference an instance of such an object anywhere in the script before using the instanceof operator.. and it seems to nudge the scripting API to act properly.

 

	var doc = app.activeDocument;
	alert("Is the first document swatch an instance of Swatch? : " + (doc.swatches[0] instanceof Swatch) + "\nIs this string 'Hello World' and instance of Swatch? : " + ("Hello World" instanceof Swatch));

 

Screen Shot 2019-10-31 at 5.04.31 AM.png 

TOPICS
Scripting
642
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
no replies

Have something to add?

Join the conversation
Adobe