Skip to main content
Inspiring
January 5, 2024
Answered

Insert Paragraph Style name at the beginning of each instance

  • January 5, 2024
  • 2 replies
  • 700 views

There are plenty of scripts that automatically make and name styles, I'm looking for something almost the reverse.

 

I'd like to be able to check every text box and at the start of every new paragraph style instance insert the actual name of the paragraph style as a string. Further, would be useful on subsequent runs of the script to identify if the name exists and update it accordingly.

 

Is this possible?

This topic has been closed for replies.
Correct answer Manan Joshi

Good idea @Robert at ID-Tasker this was so simple and here I was thinking of indexes, labels etc. @culver-king the following code would now handle the updation as well.

 

if(!app.documents[0].characterStyles.itemByName("temp").isValid){
    app.documents[0].characterStyles.add({name:"temp"})
}
app.findTextPreferences.appliedCharacterStyle = "temp"
app.changeTextPreferences.changeTo = ""
app.documents[0].changeText()
app.changeTextPreferences = null
app.findTextPreferences = null
var paras = app.documents[0].stories.everyItem().paragraphs.everyItem().getElements()
for(var i = paras.length - 1; i >= 0; i--){
    paras[i].insertionPoints[0].appliedCharacterStyle = "temp"
    paras[i].insertionPoints[0].contents = paras[i].appliedParagraphStyle.name
}

 

-Manan

2 replies

Colin Flashman
Community Expert
January 6, 2024

Perhaps. I've taken your query and had this script prepared via ChatGPT - let me know if this is suitable:

// Function to add or update paragraph style name at the start of each new paragraph style instance
function addParagraphStyleName(doc) {
  var stories = doc.stories;

  for (var i = 0; i < stories.length; i++) {
    var paragraphs = stories[i].paragraphs;
    var lastStyleName = null;

    for (var j = 0; j < paragraphs.length; j++) {
      var para = paragraphs[j];

      // Check if the paragraph has a different style than the previous one
      if (para.appliedParagraphStyle.name !== lastStyleName) {
        // Insert or update the paragraph style name at the start of the paragraph
        para.insertionPoints[0].contents = para.appliedParagraphStyle.name + ": ";
        lastStyleName = para.appliedParagraphStyle.name;
      }
    }
  }
}

// Main function to run the script
function main() {
  // Check if a document is open
  if (app.documents.length > 0) {
    var doc = app.activeDocument;

    // Call the function to add or update paragraph style name
    addParagraphStyleName(doc);
  } else {
    alert("Open a document before running this script.");
  }
}

// Run the main function
main();
If the answer wasn't in my post, perhaps it might be on my blog at colecandoo!
Community Expert
January 6, 2024

To start with the following code would add the style names at the start of the paragraph. It does not have the dynamic update feature as that would be a bit more complex

var paras = app.documents[0].stories.everyItem().paragraphs.everyItem().getElements()
for(var i = paras.length - 1; i >= 0; i--){
    paras[i].insertionPoints[0].contents = paras[i].appliedParagraphStyle.name
}

-Manan

Robert at ID-Tasker
Brainiac
January 6, 2024

@Manan Joshi, I think if you'll first insert some kind of a whitespace - the next run could either remove everything up to this space - or just replace.

 

Or, better yet - you could apply CharStyle to the inserted ParaStyle name.

 

And your current code won't work on Cells in Tables - so another everyItem() for all Tables in all Stories?

 

Manan JoshiCorrect answer
Community Expert
January 7, 2024

Good idea @Robert at ID-Tasker this was so simple and here I was thinking of indexes, labels etc. @culver-king the following code would now handle the updation as well.

 

if(!app.documents[0].characterStyles.itemByName("temp").isValid){
    app.documents[0].characterStyles.add({name:"temp"})
}
app.findTextPreferences.appliedCharacterStyle = "temp"
app.changeTextPreferences.changeTo = ""
app.documents[0].changeText()
app.changeTextPreferences = null
app.findTextPreferences = null
var paras = app.documents[0].stories.everyItem().paragraphs.everyItem().getElements()
for(var i = paras.length - 1; i >= 0; i--){
    paras[i].insertionPoints[0].appliedCharacterStyle = "temp"
    paras[i].insertionPoints[0].contents = paras[i].appliedParagraphStyle.name
}

 

-Manan