Skip to main content
New Participant
June 13, 2024
Question

Search for Text in document and export

  • June 13, 2024
  • 1 reply
  • 156 views
I get ewrror on the findtext, not sure how to fix
 
 
// Define the file path where you want to save the exported text
var exportFilePath = "C:\Users\270007378.RXLUSA\Documents\ExportedText.txt";

// Get the active document
var doc = app.activeDocument;

// Get the character style
var characterStyle = doc.characterStyles.itemByName(characterStyleName);

// Check if the character style exists
if (!characterStyle.isValid) {
    alert("Character style not found: " + characterStyleName);
    exit();
}

// Find all text with the specified character style
var foundTexts = doc.findText({
    appliedCharacterStyle: characterStyle
}, false);


// Collect the text content
var collectedText = "";
for (var i = 0; i < foundTexts.length; i++) {
    collectedText += foundTexts[i].contents + "\n";
}

// Write the collected text to a file
var file = new File(exportFilePath);
file.encoding = "UTF-8";
file.open("w");
file.write(collectedText);
file.close();

alert("Text exported to: " + exportFilePath);
This topic has been closed for replies.

1 reply

Adobe Expert
June 13, 2024

Seems the issue is with the findText method you have. That takes just a boolean argument. See

https://www.indesignjs.de/extendscriptAPI/indesign-latest/#Document.html%23d1e49551__d1e54325

You would need to do something like the following

app.findTextPreferences = NothingEnum.NOTHING;
app.findTextPreferences.appliedCharacterStyle = characterStyle
var foundTexts = doc.findText()
app.findTextPreferences = NothingEnum.NOTHING;

-Manan