Need help with another script and general question about scripting
Hi, Dear Friends!
I am trying a different function this time. To scan the text frame and any string formatted with the char style "sidenote", it extracts that text and creates an anchored text box in the margin with that text (different locations for even and odd pages).
Chat GPT gave me this:
--------------------->
#target indesign
(function () {
var doc = app.activeDocument;
var styleName = "sideNote"; // The character style for the sidenote text
// Ensure the cursor is in a valid text frame or text story
if (!app.selection.length || !(app.selection[0] instanceof TextFrame) && !(app.selection[0] instanceof Text)) {
alert("Please place your cursor in the story to start scanning.");
//return;
}
var sel = app.selection[0];
// If the selection is a TextFrame, use its parentStory, otherwise use the selected text's parentStory
var story = (sel instanceof TextFrame) ? sel.parentStory : sel.parentStory;
// Loop through the story to find all text with the "sidenote" character style
var sideNotes = [];
for (var i = 0; i < story.texts.length; i++) {
var currentText = story.texts[i];
if (currentText.appliedCharacterStyle.name === styleName) {
sideNotes.push(currentText.contents); // Extract "sideNote" text
}
}
if (sideNotes.length === 0) {
alert("No text with the 'sideNote' character style found.");
return;
}
var page = sel.parentTextFrames[0].parentPage;
var isOddPage = (page.name % 2 !== 0); // Check if it's an odd or even page
var sideNoteText = sideNotes.join(", "); // Combine side notes with commas
// Create the anchored text frame in the appropriate margin (left or right)
var marginSide = isOddPage ? "left" : "right"; // Odd pages: left margin; Even pages: right margin
var marginTextFrame = createMarginTextFrame(page, marginSide, sideNoteText);
// Anchor the text frame in the main text flow
var anchorPoint = page.textFrames[0].insertionPoints[-1]; // Anchoring at the end of the page text
marginTextFrame.anchor(anchorPoint);
alert("Side notes placed successfully!");
// Function to create a text frame in the margin based on page type (odd/even)
function createMarginTextFrame(page, margin, text) {
var textFrame;
var marginWidth = 50; // Adjust this for margin width
var textHeight = 100; // Adjust for height of the side note box
var marginX = (margin === "left") ? 0 : page.bounds[3] - marginWidth; // Left or right side
// Create text frame in margin
textFrame = page.textFrames.add();
textFrame.geometricBounds = [0, marginX, textHeight, marginX + marginWidth];
textFrame.contents = text;
// Optional: Style the text frame
var textStyle = textFrame.texts[0].appliedCharacterStyle = doc.characterStyles.itemByName("sideNote");
textFrame.texts[0].pointSize = 8; // Adjust text size
textFrame.texts[0].leading = 10; // Adjust line height
return textFrame;
}
})();
<------------------
And it is telling me that there is no text formatted with that style. BUT THERE ABSOLUTELY IS!!! ( I am getting really angry at ChatGPT for wasting my time!)
Is there a way to debug scripts step by step, similar to the macro editor in MS Word?
Thank you, and have a good day!
Susan Flamingo
