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?
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.
...
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
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?
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
Copy link to clipboard
Copied
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.
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();