Skip to main content
Inspiring
June 8, 2023
Answered

getFiles() not acting like an array

  • June 8, 2023
  • 3 replies
  • 938 views

I am having an odd issue with the getFiles() function: It is returning the list of folders I want, but won't let me interact with them as an array.

 

Here is my code:

const basePath = "C:/Users/O146962/OneDrive - Kaiser Permanente/Documents/GitHub/kp_pd_xml_automation";
const mainFolder = basePath + "/Indesign Automation POC";
const templateFolder = Folder(mainFolder + "/Templates");
const rgnTemplateFolders = templateFolder.getFiles("*_inddTemplates");
alert(rgnTemplateFolders.length());

Note that the const templateFolder is declared as a Folder object.

If I set the alert to

alert(rgnTemplateFolders);

 The script produces the following:

If I set the alert to:

alert(rgnTemplateFolders[1]);

I get:

BUT, if I set the alert to:

alert(rgnTemplateFolders.length());

I get:

 

I have been hunting through these forums and found the following posts, but none of them seem to specifically deal with this issue.

 

What am I missing here?

This topic has been closed for replies.
Correct answer Ben Ross - KP

InDesign scripting is based on ECMAScript 3. foreach didn't exist back then...

 


Thanks @TᴀW .

 

For anyone else that comes across this, here is a reference for ECMA Script 3:

https://www-archive.mozilla.org/js/language/E262-3.pdf

3 replies

Peter Kahrel
Community Expert
Community Expert
June 8, 2023

Seems to me that all's well, you get what you asked for. The list of folders shown by

 

alert(rgnTemplateFolders);

 

is an array. And

 

alert(rgnTemplateFolders[1]);

 

shows the first element in the array.

When you run this:

 

 

alert(rgnTemplateFolders).join('\r');

 

 

the files are printed as a list, as you'd expect, using the array function ,join().

 

alert(rgnTemplateFolders.length());

 

returns an error because .length is a property, not a function, as your error message tells you.

Why do you think that alert(rgnTemplateFolders); does not return an array?

 

(In the meantime Ben Ross figured it out.)

Inspiring
June 8, 2023

Thanks @Peter Kahrel ! Since the results of getFiles() IS an array, why does using array.forEach() say that forEach is not a function?

 

https://www.w3schools.com/jsref/jsref_foreach.asp

TᴀW
Legend
June 8, 2023

InDesign scripting is based on ECMAScript 3. foreach didn't exist back then...

 

Inspiring
June 8, 2023

Well now I feel a bit stupid. A colleague pointed out that I needed to use 

 

alert(rgnTemplateFolders.length);

 

since length is a property and not a method. 🤦‍

Legend
June 17, 2023

That error is understandable as you are at the same time learning Extendscript's XML, where almost everything – including "length()" – is a function rather than a property.

Inspiring
June 8, 2023