0

I've made a script that goes trough all tracked changes:

var allChanges =app.activeDocument.stories.everyItem().changes.everyItem().getElements();
var nChanges = allChanges.length;

for (var i = nChanges-1; i >= 0; i--) {
    if (allChanges[i].changeType == ChangeTypes.INSERTED_TEXT) {
        /*Do stuff to inserted text*/
    } else if(allChanges[i].changeType == ChangeTypes.DELETED_TEXT) {
        /*Do stuff to deleted text*/
    }
    /*What I need help with would go here*/
}

Now I want to make a table of content of the articles wich contains modifications. For that I will need to change the paragraph style of the first preceding title (marked by paragraph style "Title") to "Title with changes".

I'm a complete new-be in the scripting world for indesign, but I have good javascript basics.

I think that the way to do this woudl be to go on a while loop selecting current paragraph (containing the modification) and check if it has "Title" style applied and if so, applie "Title with changes" style instead and end the loop. Else, if the current paragraph has "Title with changes" style applied, end the while loop. Else move one paragraph back and check the same condition untill.

I just dont know how to write the script itself or if it even is possible.

Thank you for your help!

1 Answer 1

2

I needed a function to select the previous paragraph like this:

function getPreviousPara(text){  
  return text.parent.characters.item(text.paragraphs[0].index-1).paragraphs[0];  
}

Then, I need to loop trough and find the previous paragraph before manipulating the change since I have to accept de changes wich makes the allChanges[i] == null after accepting them.

Then, I need to save the story containing the active change as var changeStory = allChanges[i].parent;. Then I manipulated the changes, appling corresponding character style and accepting added text and refusing deleted text. I then loop through the var everyStyleRange = changeStory.textStyleRanges; looking of the range containing the character style applied to any modification, or the modified Title style. When found, get the var currentPara = everyStyleRange[i].paragraphs[0]; and check for it's paragraph style name. if it matches Title, apply the Title modif. If it matches Title modif then end the loop, otherwise, currentPara = getPreviousPara(currentPara); and loop.

Update: I added comments in so it's clear the functioning of the script.

/*Function to select the previous paragraph*/
function getPreviousPara(text){
    var nextPara = text.parent.characters.item(text.paragraphs[0].index-1).paragraphs[0];
    return nextPara
}

/*Global variables declaration*/
var myDoc = app.activeDocument;
var allChanges =myDoc.stories.everyItem().changes.everyItem().getElements();
var nChanges = allChanges.length;

/*Loop through all changes*/
for (var i = 0; i <= nChanges-1; i++) {
    /*Get story containing change*/
    var changeStory =  allChanges[i].parent;

    /*Apply character style to Added text and Deleted text*/
    if (allChanges[i].changeType == ChangeTypes.INSERTED_TEXT) {
        var cName = "Added";
        var mCstyle = app.activeDocument.characterStyles.item(cName);
        allChanges[i].characters.everyItem().applyCharacterStyle(mCstyle);
        allChanges[i].accept();
    } else if(allChanges[i].changeType == ChangeTypes.DELETED_TEXT) {
        var cName = "Deleted";
        var mCstyle = app.activeDocument.characterStyles.item(cName);
        var changeParent = allChanges[i].parent;
        allChanges[i].characters.everyItem().applyCharacterStyle(mCstyle);
        allChanges[i].reject();
    }

    /*Get the textStyleRanges of the story*/
    var everyStyleRange = changeStory.textStyleRanges;
    nItems = everyStyleRange.length;

    /*1st while loop variables declaration*/
    var found = 0;
    var i = 0;

    /*Loop through all textStyleRanges*/
    while(found==0 && i<=nItems-1){

        /*Look for a style range that has the Deleted or Added character style or the Title modif style*/
        if (everyStyleRange[i].appliedCharacterStyle.name == "Deleted" || everyStyleRange[i].appliedCharacterStyle.name == "Added" || everyStyleRange[i].appliedCharacterStyle.name == "Title modif"){

            /*When found, kill the loop*/
            found = 1;

            /*2nd while loop variables declaration*/
            var currentPara = everyStyleRange[i].paragraphs[0];
            var corrected = 0;
            var maxLoopLimit = 0;

            /*Loop through all paragraphs of the story going backward from the found style range*/
            while(corrected==0 && maxLoopLimit < changeStory.paragraphs.length){
                maxLoopLimit++;

                /*Get the paragraph style name*/
                var currentParaStyleName= currentPara.appliedParagraphStyle.name;

                /*Look for the Title paragraph style applied*/
                if(currentParaStyleName == "Title"){

                    /*Title found, apply new paragraph style and kill 2nd while loop*/
                    currentPara.appliedParagraphStyle = currentParaStyleName+" modif";
                    corrected++; 
                }
                else if(currentParaStyleName == "Title modif"){

                    /*Title already changed, kill 2nd while loop*/
                    corrected++;
                }
                else{

                    /*Get previous paragraph and start loop again, acts as counter*/
                     currentPara = getPreviousPara(currentPara);
                }

            /*2nd while loop end*/
            }

        /*if end*/
        }

        /*If no modification were found in the story, alert the user that an error occured*/
        if(i>=nItems){alert("No changes character style was found so no Title change occured")}

        /*increment 1st loop counter*/
        i++;

    /*1st loop end*/
    }
}

Heavy stuff for a new-be coder, lots of research and testing!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.