Copy link to clipboard
Copied
Hi,
I know how find a paragraph by paragraph style, but I need to detect the paragraph style of the paragraph above and below of the paragraph found.
If a paragraph style above is "A", I will apply a "B1" style for the paragraph found, if the paragraph style above is the same of the paragraph found and the paragraph style below is the same too, I will apply "B2" style. If the paragraph style above is the same of the paragraph found and the paragraph style below is different I will apply "B3" style.
I'm trying with index ID, but no success.
So, any idea how can I Know the paragraph style below and above of the paragraph style found?
Thank you.
Jareks description in code, slightly amended:
// before
alert (app.selection[0].parentStory.characters[app.selection[0].paragraphs[0].index-1].appliedParagraphStyle.name);
// after
alert (app.selection[0].parentStory.characters[app.selection[0].paragraphs[0].characters[-1].index+1].appliedParagraphStyle.name);
In other words, just hop up and down the object model. This way is faster than the built-in methods 'lastItem', 'previousItem' and 'nextItem' too.
Depending on your ultimate purpose, it may even
...Copy link to clipboard
Copied
Hi,
In case of 'text' object's indexes (word, paragraph, line, etc) - this object.index is equal to this object.characters[0].index.
So real (useful) index need to be calculated. For example:
so
Jarek
PS. You need to test if current para IS NOT 1st or last before reffering to prev or next part of story - of cource.
Copy link to clipboard
Copied
Jareks description in code, slightly amended:
// before
alert (app.selection[0].parentStory.characters[app.selection[0].paragraphs[0].index-1].appliedParagraphStyle.name);
// after
alert (app.selection[0].parentStory.characters[app.selection[0].paragraphs[0].characters[-1].index+1].appliedParagraphStyle.name);
In other words, just hop up and down the object model. This way is faster than the built-in methods 'lastItem', 'previousItem' and 'nextItem' too.
Depending on your ultimate purpose, it may even be faster yet to first gather all paragraph style names for the entire document and keep them in memory.
Copy link to clipboard
Copied
// after
alert (app.selection[0].parentStory.characters[app.selection[0].paragraphs[0].characters[-1].index+1].appliedParagraphStyle.name);
For the after, you can use this shortcut:
app.selection[0].paragraphs[-1].insertionPoints[-1].appliedParagraphStyle.name;
... because the last insertionPoint of any paragraph is the first of the next.
Also be aware that some check is needed in case the selection is the last paragraph of the story.
Ariel
Copy link to clipboard
Copied
Ariel, then would that same trick also work for the "before"? If it does you deserve the Green Tick of Approval!
Copy link to clipboard
Copied
Alas, it doesn't... last time I checked.
So, the is yours ... for now...
Copy link to clipboard
Copied
Hello
Here is a beginning of track that I hope will help you:
Bonjour
Voici un début de piste qui j'espère pourra vous aidez :
//----------------- Recheche global ----------------------------------
// Application des som(ex: som0, som1, som2 et som3)
// Philippe Ruelle
//
//
//----------------- Recheche global ----------------------------------
//----------------- a Faire ----------------------------------
// Message placer le style som02 ou (voir version 02) placer le style
// menu avec les style
//----------------- A Faire ----------------------------------
//---------- Le main -------------------------------
main2();
//---------- Fin du main ---------------------------
//-------------- function Main Bis ---------------
// -------------- fin du main CS5
function main2() {
app.scriptPreferences.userInteractionLevel = UserInteractionLevels.interactWithAll;
if(app.documents.length > 0) { // if 01
if(app.selection.length != 0 && app.selection[0].constructor.name == 'InsertionPoint'){ // if 02
//alert(document.name);
// faire dialogue
lesStyles();
return (true);
} else {
alert("Mettre le curseur au point de départ.");
} // fin du if 02
}
else{
alert("Ouvrir un document et mettre le curseur au point de départ.");
}// fin du if 01
} // fin de la function MAIN
//
function lesStyles() {
// contruit un tableau de tous les para
var lesStyles = diaSelectionStyle();
// $.writeln ("TestRetour : " +lesStyles);
if (lesStyles != null) { // if test Les Tyles //01
// $.writeln ("if OK");
var mesPara = app.selection[0].parentStory.paragraphs;
for (i=0; i < mesPara.length; i++){ // FOR 01
// $.writeln (mesPara.appliedParagraphStyle.name);
if (mesPara.appliedParagraphStyle.name == lesStyles[2]) { // if 01
i = lesSom(mesPara,i,lesStyles);
}// fin du if 01
} // fin du FOR 01
} // fin du If // 01
}// fin de la function lesStyles
//
function lesSom(mesPara,lePara, lesStyles){
var conteurLocal = 0;
if (mesPara[lePara].appliedParagraphStyle.name == lesStyles[2]) { // if 01 // Som2
do {
conteurLocal++;
//$.writeln ("oui + conteurLocal : " +conteurLocal);
} while (mesPara[lePara].appliedParagraphStyle.name == mesPara[lePara+ conteurLocal].appliedParagraphStyle.name)
if (conteurLocal == 1){
mesPara[lePara].appliedParagraphStyle = app.activeDocument.paragraphStyles.item(lesStyles[0]); // 0
} else if (conteurLocal == 2) {
mesPara[lePara].appliedParagraphStyle = app.activeDocument.paragraphStyles.item(lesStyles[1]); // 1
mesPara[lePara+1].appliedParagraphStyle = app.activeDocument.paragraphStyles.item(lesStyles[3]); //3
} else if (conteurLocal > 2) {
mesPara[lePara].appliedParagraphStyle = app.activeDocument.paragraphStyles.item(lesStyles[1]); // 1
mesPara[lePara+(conteurLocal-1)].appliedParagraphStyle = app.activeDocument.paragraphStyles.item(lesStyles[3]); // 3
}
}// fin if 01
return (lePara+(conteurLocal-1));
}// fin de lafunction lesSom
function diaSelectionStyle(){
var lesStyle00;
var leTest = 0;
var listStrings = styleDoc();
var maFenettre = new Window('dialog', 'Drop-down List');
maFenettre.orientation = 'column';
maFenettre.alignment = 'right';
var monInfo = maFenettre.add ("group");
monInfo.alignment = "left";
monInfo.add('statictext', undefined, "ATTENTION : d'abord appliquer Som02 Ã tous les sommaires!");
maFenettre.pnl = maFenettre.add('panel', undefined, "Vos Style : ");
var som00 = maFenettre.pnl.add('group');
som00.orientation = 'row';
som00.add('statictext', undefined, "Style (som00) :");
som00.laListSom00 = som00.add('dropdownlist', undefined, undefined, {items:listStrings});
var som01 = maFenettre.pnl.add('group');
som01.orientation = 'row';
som01.add('statictext', undefined, "Style (som01) :");
som01.laListSom01 = som01.add('dropdownlist', undefined, undefined, {items:listStrings});
var som02 = maFenettre.pnl.add('group');
som02.orientation = 'row';
som02.add('statictext', undefined, "Style (som02) :");
som02.laListSom02 = som02.add('dropdownlist', undefined, undefined, {items:listStrings});
var som03 = maFenettre.pnl.add('group');
som03.orientation = 'row';
som03.add('statictext', undefined, "Style (som03) :");
som03.laListSom03 = som03.add('dropdownlist', undefined, undefined, {items:listStrings});
var buttons = maFenettre.add ("group")
buttons.alignment = "right";
var convertion = buttons.add ("button", undefined, "Appliquer", {name: "ok"});
var annulation = buttons.add ("button", undefined, "Annul", {name: "cancel"});
// test Dialogue som00.laListSom00.selection = 4;
// test Dialogue ssom01.laListSom01.selection = 5;
// test Dialogue ssom02.laListSom02.selection = 6;
convertion.onClick = function () {
if (som00.laListSom00.selection != null) {
if (som01.laListSom01.selection != null) {
if (som02.laListSom02.selection != null) {
if (som03.laListSom03.selection != null) {
// $.writeln ('OK : ' + String(som00.laListSom00.selection));
lesStyle00 = [String(som00.laListSom00.selection),String(som01.laListSom01.selection),String(som02.laListSom02.selection),String(som03.laListSom03.selection)];
this.window.close( 1 );
} else {
alert ("Sélectionner votre som03 !", "Pas de sélection", );
}
} else {
alert ("Sélectionner votre som02 !", "Pas de sélection", );
}
} else {
alert ("Sélectionner votre som01 !", "Pas de sélection", );
}
} else {
alert ("Sélectionner votre som00 !", "Pas de sélection", );
}
} // fin de la function
if (maFenettre.show() == 1 ) {
return (lesStyle00);
} else {
return (false);
}
} // fin de la function
//
function styleDoc() {
// Backup var variableLocal = "Par defaut\n" + app.activeDocument.paragraphStyles.everyItem().name.join("\n");
var variableLocal = app.activeDocument.paragraphStyles.everyItem().name.join("\n");
return variableLocal.split("\n");
}// fin de la function