Site logo

ExtendScript Toolkit (ESTK) Tips

You can download ESTK both for Mac and Windows from here

How to get rid of the pesky ‘undefined’ message in ESTK’s console

Use regular expressions to replace code quickly in ESTK

ExtendScript (plus InDesign) is slow to the point of not working

ExtendScript Memory script by Marc Autret

ExtendScript libraries and frameworks

Cannot execute script in target engine 'main'! (#1116) Can't start debug session

Unable to install ESTK on Mojave (version 10.14.3)

Studying the ESTK JavaScript by Kris Coppieters

What to do when Mac OS X becomes 64 bit only by Kris Coppieters

Visualize special characters

How to execute .jsx script from .jsx script?


Strangely unknown fact: ExtendScript supports a special for each syntax (from its CS4 version, I guess) that allows you to loop straight into the *values* of any object. This includes basic arrays, so:

var a=[3,1,4,1,5,9], s=0, x;
for each( x in a ){ s+=x }
alert( s ); // 23

This relates to the core objects, results are indeed very different with DOM objects or specifiers.

It doesn't seem to iterate over collections, even when you are using everyItem().getElements(). YouI just get one iteration with the length of the array as a value.
This also reveals a surprising bias: myColl.everyItem().getElements() doesn't return a pure Array -- despite instanceof & checks.

In vscode, it doesn't exactly work with Arrays. It iterates through the elements and then some extra functions like "search". Had to change it to something like this:

var a=[3,1], s=0, x;
for each( x in a ) {
alert( s ); //4

To sum up:

Javascript String.replace(): undocumented feature

ESTK object model viewer describes string.replace() function as following:
String.replace (what: any, with: string):
string Core JavaScript Classes
what: Data Type: any
with: Data Type: string

But as you probably know, this function doesn't replace all occurences in a string.

var s = 'old old old'; var ss = s.replace('old', 'new'); $.writeln(ss);

Result: new old old

Therefore I have always used a regular expression with 'g' flag as 1st argument to replace all occurences in a string:

var s = 'old old old'; var sss = s.replace(/old/g, 'new'); $.writeln(sss);

Result: new new new

But as I discovered recently, we can still use the default syntax if we use regular expression flag(s) as third argument:

var s = 'old old old'; var ssss = s.replace('old', 'new', 'g'); $.writeln(ssss);

Result: new new new