Quantcast
Channel: Adobe Community: Message List - InDesign Scripting
Viewing all articles
Browse latest Browse all 37788

Re: Decrementing a date

$
0
0

In your original request, you had a source and a target, so the question is how to change the script to handle that?

 

Off the top of my head, the easy way to do this is use two different searches. Assuming both source and target are in range of the original selection, this should work (You'll need to change the paragraph style names.)

 

//DESCRIPTION: Processing the text of the selection or of the selection's parent  
(function() {// use anonymous function to avoid namespace leakage          if (app.documents.length > 0) {// must be a document before there can be a selection                    if (app.selection.length > 0) {//selection is always an array                              var myText = getTextFromSelection(app.selection[0]);                              if (myText != null) {                                        var mySourceStyle = app.activeDocument.paragraphStyles.item("SourceStyleName");                                        var myTargetStyle = app.activeDocument.paragraphStyles.item("TargetStyleName");                                        decrementDates(mySourceStyle, myTargetStyle, myText)                              }                    }          }            function decrementDates(sourceStyle, targetStyle, myText) {                    setupFindGrep("~v \\d+","");                    app.findGrepPreferences.appliedParagraphStyle = sourceStyle;                    var myFinds = myText.findGrep();                    if (myFinds.length > 1) {                              alert("More than one source found; ignoring all but first");                    }                    var source = myFinds[0];                    var numString = source.contents.slice(2);                    var decString = decrementDateNum(numString);                    app.findGrepPreferences.appliedParagraphStyle = targetStyle;                    var myFinds = myText.findGrep();                    for (var j = myFinds.length - 1; j >= 0; j--) {                              var startIndex = myFinds[j].index + 2;                              var endIndex = myFinds[j].characters[-1].index;                              myFinds[j].parent.characters.itemByRange(startIndex, endIndex).texts[0].contents = decString;                    }          }            function getTextFromSelection(sel) {                    if (sel.hasOwnProperty("baseline")) {// sel is text                              return sel.parent.texts[0];                    }elseif (sel.hasOwnProperty("texts")) {// sel owns a text object                              return sel.texts[0];                    }                    returnnull;          }            function decrementDateNum(aString) {                    return String(Number(aString) - 1);          }            function setupFindGrep(find, change, foots, hidLayers, lockedLayers, lockedStories, masters) {                    app.findGrepPreferences = null;                    app.changeGrepPreferences = null;                    try{ app.findGrepPreferences.findWhat = find }catch(e) {};                    try{app.changeGrepPreferences.changeTo = change }catch(e) {};                    app.findChangeGrepOptions.properties = {                              includeFootnotes:(foots == null ? false : foots),                              includeHiddenLayers:(hidLayers == null ? false : hidLayers),                              includeLockedLayersForFind:(lockedLayers == null ? false : lockedLayers),                              includeLockedStoriesForFind:(lockedStories == null ? false : lockedStories),                              includeMasterPages:(masters == null ? false : masters)                    }          }// end setupFindGrep}())

 

I think that does the job. It allows for multiple targets but processes only the first source in the selection.

 

One way of extending it to handle multiple sources (for example, each in its own cell of a table with all the associated targets in the same cell) would be to use the (badly named) getTextFromSelection function to get the associated text for each source and then use that as the range for the corresponding targets.

 

Dave


Viewing all articles
Browse latest Browse all 37788

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>