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

Need help with another script and general question about scripting

Contributor ,
Jul 29, 2025 Jul 29, 2025

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

TOPICS
Scripting , Type
434
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 2 Correct answers

Community Expert , Jul 29, 2025 Jul 29, 2025

As Rob mentioned, chatGPT sometimes hallicinates (some euphemism!), though it often get very good results. It just depends on what it finds on the web. By the looks of it in your case it found various bits and pieces and cobbled them together.  Many times though there are much better solutions than are available on the web. What you're after appears to be a case in point.

 

Below is a pretty simple script. It can be simple because InDesign itself does most of the work through an object style. Th

...
Translate
Community Expert , Jul 30, 2025 Jul 30, 2025

Ah, ok, I see. Here you go:

(function () {
  
  var frame,
      sidenotes,
      odd = app.activeDocument.objectStyles.item ('ATBOdd'),
      even = app.activeDocument.objectStyles.item ('ATBEven');

  if (app.selection.length === 0 
      || !(app.selection[0] instanceof TextFrame)) {
    alert ('Select a text frame');
    exit();
  }

  app.findGrepPreferences = null;
  app.findGrepPreferences.appliedCharacterStyle = 
    app.activeDocument.characterStyles.item('sidenote');
    
  sidenotes =
...
Translate
Community Expert ,
Jul 29, 2025 Jul 29, 2025

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?

 

Hi @SuzzyFlamingo , You would need an application like Visual Studio Code with the Extendscript plugin to debug—the learning curve is steep.

 

ChatGPF is hallucinating. There are a number of problems with the script. It doesn't do anything unless all the selected test is assigned your character style and then throws an error if it is:

 

Screen Shot 18.png

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 29, 2025 Jul 29, 2025

As Rob mentioned, chatGPT sometimes hallicinates (some euphemism!), though it often get very good results. It just depends on what it finds on the web. By the looks of it in your case it found various bits and pieces and cobbled them together.  Many times though there are much better solutions than are available on the web. What you're after appears to be a case in point.

 

Below is a pretty simple script. It can be simple because InDesign itself does most of the work through an object style. That style defines a spine-relative achored text frame with its width and vertical auto size set. So the script doesn't need to bother with figuring out whether you're on a left- or right-hand page or the size of the frame.

 

The script assumes the presence of an object style 'sidenote'. Select a frame and run the script. The attached InDesign file illustrates the object style.

 

(function () {

  var frame,
      ostyle = app.activeDocument.objectStyles.item ('sidenote');

  if (app.selection.length === 0 
      || !(app.selection[0] instanceof TextFrame)) {
    alert ('Select a text frame');
    exit();
  }

  app.findGrepPreferences = null;
  app.findGrepPreferences.appliedCharacterStyle = 
    app.activeDocument.characterStyles.item('sidenote');
    
  var sidenotes = app.selection[0].findGrep();
  for (var i = sidenotes.length-1; i >= 0; i--) {
    frame = sidenotes[i].insertionPoints[0].textFrames.add ({
      appliedObjectStyle: ostyle
    });
    sidenotes[i].move (LocationOptions.AT_BEGINNING, frame);
  }
}());
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
Contributor ,
Jul 29, 2025 Jul 29, 2025

Wow! Actually works! Thank you!

Since you have been so kind, I will try my luck at another request:

I have two abchored text box style, one for (styATBOdd) odd pages (places it in the left margin) and one for even pages(styATBEven) (places it in the right margin) 

Could you add that functionality that if it is on an odd page, it places it in the odd page text box and on an even page in the even page TB

Again thank you

Thank you, and have a good day!
Susan Flamingo

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 29, 2025 Jul 29, 2025

That's certainly possible, but how do those styles differ? is it not possible to change the one style such that it works they way you want?

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
Contributor ,
Jul 29, 2025 Jul 29, 2025

on odd page the anchored text box gets inserted in the left (outside) margin and on an even page in the right (outside) margin. Yes always same size but different positions. Can it be done?

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 29, 2025 Jul 29, 2025

Sure. In the object style, go to the Anchored Object Options tab. There you'll see two panels where you set a reference point. The style shown in the screen places a box relative to the text frame, in the outside margin. To change that, select different reference points. It's useful to enable the preview so you can see what's happening. 

 

PeterKahrel_1-1753819226481.png

 

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
Contributor ,
Jul 30, 2025 Jul 30, 2025

Yes, I knew how to do that. However, my question was how to automate this process using your script, specifically inserting into the correct table, for which I have set up two object styles. ATBEven, ATBOdd

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 30, 2025 Jul 30, 2025

I don't understand what you mean by 'the table'. Can you show (or send) your document so I can see what you're after?

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
Contributor ,
Jul 30, 2025 Jul 30, 2025

Sorry for that. I meant the correct anchored text box which are different for odd and even pages as i said before. Hope that clears things up

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 30, 2025 Jul 30, 2025

Ah, ok, I see. Here you go:

(function () {
  
  var frame,
      sidenotes,
      odd = app.activeDocument.objectStyles.item ('ATBOdd'),
      even = app.activeDocument.objectStyles.item ('ATBEven');

  if (app.selection.length === 0 
      || !(app.selection[0] instanceof TextFrame)) {
    alert ('Select a text frame');
    exit();
  }

  app.findGrepPreferences = null;
  app.findGrepPreferences.appliedCharacterStyle = 
    app.activeDocument.characterStyles.item('sidenote');
    
  sidenotes = app.selection[0].findGrep();
  for (var i = sidenotes.length-1; i >= 0; i--) {
    frame = sidenotes[i].insertionPoints[0].textFrames.add();
    
    if (sidenotes[i].parentTextFrames[0].parentPage.side === 
        PageSideOptions.LEFT_HAND) {
      frame.appliedObjectStyle = even;
    } else {
      frame.appliedObjectStyle = odd;
    }
  
    sidenotes[i].move (LocationOptions.AT_BEGINNING, frame);
  }
}());

 

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
Contributor ,
Jul 31, 2025 Jul 31, 2025
LATEST

I will have to try this and play around with it a little. Thank you very much!

Thank you, and have a good day!
Susan Flamingo

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