‘require’ keyword to import modules in Illustrator JavaScript ?

New Here ,
Dec 27, 2020

Copy link to clipboard

Copied

Hi, I’ve written a JavaScript for Adobe Illustrator to automatically add text labels to shapes. The problem is that the shapes have sometimes odd forms (like U f.e.) it’s therefore hard to find the visual center of the shapes lying inside the shape to place the text label. I found this algorithm: A new algorithm for finding a visual center of a polygon | by Mapbox | maps for developers which seems to do the job so I tried to use it in my Illustrator JavaScript. The problem is that using this algo I need to import a module called ‘tinyqueue’.

I’m not experienced in writing JavaScript. I’ve installed node.js on my Window10 PC and installed ‘tinyqueue’ module in the directory where my Illustrator script resides (npm init -y; npm install tinyqueue). This has created a package.json file locally (+ a node_modules/ directory) . Then, as a starting point, I just added the  following line at top of my script:

var Queue = require('tinyqueue');

When trying to execute my script in Illustrator I get the following Illustrator error message:

Error 24: require is not a function. Line: 3 -> var Queue = require('tinyqueue');

I wonder if there is a problem with my package.json file? Is the ‘require’ (and f.e. ‘class’) keyword supported at all for Illustrator scripting? Do I need to add a #include line to my script to read the package.json file?

I would be grateful if someone could instruct me in solving this error!

Adobe Community Professional
Correct answer by Silly-V | Adobe Community Professional

Illustrator JavaScript is an older ES3 style so there's no modules and no require available. So this code would have to be "ported" to the old sort of JavaScript that can just run in Illustrator. 

TOPICS
Scripting

Views

92

Likes

Translate

Translate

Report

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

‘require’ keyword to import modules in Illustrator JavaScript ?

New Here ,
Dec 27, 2020

Copy link to clipboard

Copied

Hi, I’ve written a JavaScript for Adobe Illustrator to automatically add text labels to shapes. The problem is that the shapes have sometimes odd forms (like U f.e.) it’s therefore hard to find the visual center of the shapes lying inside the shape to place the text label. I found this algorithm: A new algorithm for finding a visual center of a polygon | by Mapbox | maps for developers which seems to do the job so I tried to use it in my Illustrator JavaScript. The problem is that using this algo I need to import a module called ‘tinyqueue’.

I’m not experienced in writing JavaScript. I’ve installed node.js on my Window10 PC and installed ‘tinyqueue’ module in the directory where my Illustrator script resides (npm init -y; npm install tinyqueue). This has created a package.json file locally (+ a node_modules/ directory) . Then, as a starting point, I just added the  following line at top of my script:

var Queue = require('tinyqueue');

When trying to execute my script in Illustrator I get the following Illustrator error message:

Error 24: require is not a function. Line: 3 -> var Queue = require('tinyqueue');

I wonder if there is a problem with my package.json file? Is the ‘require’ (and f.e. ‘class’) keyword supported at all for Illustrator scripting? Do I need to add a #include line to my script to read the package.json file?

I would be grateful if someone could instruct me in solving this error!

Adobe Community Professional
Correct answer by Silly-V | Adobe Community Professional

Illustrator JavaScript is an older ES3 style so there's no modules and no require available. So this code would have to be "ported" to the old sort of JavaScript that can just run in Illustrator. 

TOPICS
Scripting

Views

93

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Dec 27, 2020 0
Adobe Community Professional ,
Dec 27, 2020

Copy link to clipboard

Copied

Illustrator JavaScript is an older ES3 style so there's no modules and no require available. So this code would have to be "ported" to the old sort of JavaScript that can just run in Illustrator. 

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Dec 27, 2020 0
New Here ,
Jan 02, 2021

Copy link to clipboard

Copied

Hi Silly-V, thank you for your reply! I succeeded to “port” the code  A new algorithm for finding a visual center of a polygon | by Mapbox | maps for developers  for usage in Illustrator (‘tinyqueue’ module isn’t really required for this code, a simple ‘new Array()’ does the job). I succeeded to use the algo to place the labels and result is better than using the ‘center of gravity’ method but is still not satisfying in case of  'nested' shapes. I’ll post another request to see if somebody can help. Thanks again!

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jan 02, 2021 0
Community Beginner ,
Jan 04, 2021

Copy link to clipboard

Copied

The only method I have found for "importing" external code is to use an #include e.g.

#include csv.js

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jan 04, 2021 0
Resources