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

getFiles() not acting like an array

Participant ,
Jun 08, 2023 Jun 08, 2023

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:

BenRossKP_0-1686249187126.png

If I set the alert to:

alert(rgnTemplateFolders[1]);

I get:

BenRossKP_1-1686249243167.png

BUT, if I set the alert to:

alert(rgnTemplateFolders.length());

I get:

BenRossKP_2-1686249283515.png

 

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?

TOPICS
Bug , How to , Scripting , SDK
1.1K
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

correct answers 3 Correct answers

Participant , Jun 08, 2023 Jun 08, 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. 🤦‍

Translate
People's Champ , Jun 08, 2023 Jun 08, 2023

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

 

Translate
Participant , Jun 09, 2023 Jun 09, 2023

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

Translate
Participant ,
Jun 08, 2023 Jun 08, 2023
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
Participant ,
Jun 08, 2023 Jun 08, 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. 🤦‍

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
Mentor ,
Jun 17, 2023 Jun 17, 2023
LATEST

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.

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
Community Expert ,
Jun 08, 2023 Jun 08, 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.)

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
Participant ,
Jun 08, 2023 Jun 08, 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

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
People's Champ ,
Jun 08, 2023 Jun 08, 2023

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

 

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
Participant ,
Jun 09, 2023 Jun 09, 2023

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

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