Script to export any or all stories that use a certain swatch.
Hey good afternoon!
So i'm trying to create a script that will export any and all stories where the text is colored with a swatch the user can pick. So for example, if I have some text that's black and some text that's colored with a global swatch called "Bright Orange" or a spot color swatch called "PMS 186", and I run the script, it will ask me what swatch, and I will fill in a field. The the script will export all the stories in that document that use that swatch to a text file.
I asked Chat GPT to do this rather than bother a human. Unfortunately, I keep getting an error.
I will provide the script, then the error.
I am not a coder, so I am very out of my depth here.
Any light that can be shed is welcome.
// Prompt the user to input the swatch name
var swatchName = prompt("Enter the name of the swatch for the exported text:", "French Translation");
// Set the default swatch name if no input provided
if (!swatchName) {
swatchName = "French Translation";
}
// Get a reference to the active document
var doc = app.activeDocument;
// Get the CMYK spot color swatch by name
var colorSwatch = doc.colors.itemByName(swatchName);
// Check if the swatch exists
if (!colorSwatch.isValid) {
alert('The color swatch "' + swatchName + '" does not exist in the document.');
exit();
}
// Array to store the selected stories
var selectedStories = [];
// Loop through each story in the document
for (var i = 0; i < doc.stories.length; i++) {
var story = doc.stories[i];
// Loop through each text frame in the story
for (var j = 0; j < story.textContainers.length; j++) {
var textFrame = story.textContainers[j];
// Check if the text frame contains the requested swatch color and the word "colored"
if (hasSwatchColor(textFrame, colorSwatch) && textFrame.contents.toLowerCase().includes("colored")) {
selectedStories.push(story);
break;
}
}
}
// Create a new File object
var exportFile = new File("/Path/To/Export/Directory/output.txt"); // Set the desired output path and file name
// Open the file in write mode
exportFile.open("w");
// Loop through each selected story
for (var m = 0; m < selectedStories.length; m++) {
var selectedStory = selectedStories[m];
// Loop through each text frame in the story
for (var n = 0; n < selectedStory.textContainers.length; n++) {
var textFrame = selectedStory.textContainers[n];
// Write the text content to the file
exportFile.writeln(textFrame.contents);
}
}
// Close the file
exportFile.close();
alert("Stories with the word 'colored' using the swatch '" + swatchName + "' exported to a text file!");
// Function to check if a text frame contains the specified swatch color
function hasSwatchColor(textFrame, swatch) {
// Loop through each character in the text frame
for (var i = 0; i < textFrame.characters.length; i++) {
var character = textFrame.characters[i];
// Check if the character's fill color matches the specified swatch
if (character.fillColor === swatch) {
return true;
}
}
return false;
}and here the the error, on run, after it asks for the swatch:
JavaScript Error!
Error Number: 24
Error String: textFrame.contents.toLowerCase).includes is not a function
Engine: main
File: /Users/ myname/Library/Preferences / Adobe InDesign/Version
18.0/en US/Scripts/Scripts Panel/Export French.is
Line: 33
Source:
if (hasSwatchColor(textFrame, colorSwatch) &&
textFrame.contents.toLowerCase).includes("colored")) {
