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

Insert Paragraph Style name at the beginning of each instance

Explorer ,
Jan 05, 2024 Jan 05, 2024

Copy link to clipboard

Copied

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?

TOPICS
Scripting

Views

195

Translate

Translate

Report

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 1 Correct answer

Community Expert , Jan 07, 2024 Jan 07, 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.
...

Votes

Translate

Translate
Community Expert ,
Jan 06, 2024 Jan 06, 2024

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

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 ,
Jan 06, 2024 Jan 06, 2024

Copy link to clipboard

Copied

@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?

 

Votes

Translate

Translate

Report

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 ,
Jan 07, 2024 Jan 07, 2024

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

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
Explorer ,
Jan 07, 2024 Jan 07, 2024

Copy link to clipboard

Copied

LATEST

Holy cow, thank you. This is EXACTLY what I was looking for. Thank you both @Manan Joshi and @Robert at ID-Tasker for your thoughtful support.

Votes

Translate

Translate

Report

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 ,
Jan 06, 2024 Jan 06, 2024

Copy link to clipboard

Copied

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!

Votes

Translate

Translate

Report

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