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

Need help with script

Engaged ,
Jul 27, 2025 Jul 27, 2025

Hi, Dear Friends!

I need a script that will scan all the text frames in a section and select a list of text strings formatted with the character style "sty1". And then dump those words at the present location with commas between them. But it is important that the script also includes the footnote area on each page after having scanned the main text area and formatted the strings to the list as well.

So if we have on page 1 a text frame with this text"

"If you eat with a fork, you will be health,y" and the words "fork" and "healthy" are formatted with char style "sty1"

And then in the FN area, we have the text:

"because you will eat slower and you will not be considered a slob," and the words "eat slower" and "considered a slob" are formatted with chat style "sty1"

and repeat for the next page till the end of the section. On each page, scan first the main text area and then the FN area

it will dump at the current location

fork, healthy, eat slower, considered a slob.

I asked ChatGPT to write this, and it gave me:

------>

#target indesign

(function () {
var doc = app.activeDocument;
var styleName = "sty1"; // Name of the character style you're collecting

// Ensure there's a selection and it's text-based
if (!app.selection.length || !(app.selection[0] instanceof Text)) {
alert("Place your text cursor somewhere in the section you want to scan.");
return;
}

var sel = app.selection[0];
var page = sel.parentTextFrames[0].parentPage;
var section = page.appliedSection;

if (!section.isValid) {
alert("No valid section found.");
return;
}

var startPageIndex = section.pageStart.documentOffset;
var numPages = section.length;
var collected = [];

// Loop through pages in the section
for (var i = 0; i < numPages; i++) {
var currentPage = doc.pages[startPageIndex + i];
var textFrames = currentPage.textFrames;

// ====== 1. MAIN TEXT on this page ======
for (var j = 0; j < textFrames.length; j++) {
var tf = textFrames[j];

try {
var mainFound = tf.texts[0].findGrep({
appliedCharacterStyle: doc.characterStyles.itemByName(styleName),
findWhat: ".+?"
});

for (var m = 0; m < mainFound.length; m++) {
collected.push(mainFound[m].contents);
}
} catch (e) {
// Do nothing if frame is empty or invalid
}
}

// ====== 2. FOOTNOTES on this page ======
for (var j = 0; j < textFrames.length; j++) {
var tf = textFrames[j];

try {
var footnotes = tf.texts[0].footnotes;

for (var f = 0; f < footnotes.length; f++) {
var footnoteFound = footnotes[f].texts[0].findGrep({
appliedCharacterStyle: doc.characterStyles.itemByName(styleName),
findWhat: ".+?"
});

for (var g = 0; g < footnoteFound.length; g++) {
collected.push(footnoteFound[g].contents);
}
}
} catch (e) {
// Skip any empty or invalid footnote frames
}
}
}

if (collected.length === 0) {
alert("No styled text found with style: " + styleName);
return;
}

var result = collected.join(", ");

// Insert result into end of last frame on last page of section
var lastPage = doc.pages[startPageIndex + numPages - 1];
var lastFrame = lastPage.textFrames[-1];

lastFrame.texts[0].insertionPoints[-1].contents = "\r" + result;

alert("Done. List created:\n" + result);
})();

 

<-----

But it keeps giving me an error message to place the cursor in the section I want scanned, but it is already there.

 

Can anyone kindly assist (but remember I could not write a script if my life depended on it:)

 

Thank you, and have a good day!
Susan Flamingo

TOPICS
How to , Scripting , Type
214
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 ,
Jul 28, 2025 Jul 28, 2025

idk if the rest of the script will work as you intend, but to solve the immediate issue, take out the "Text" check on your selection early on: if (!app.selection.length) { ...  

 

 


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
Engaged ,
Jul 28, 2025 Jul 28, 2025
LATEST

Did that and now failing with some system error. Sigh...

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