Skip to main content
Rocky Berlier
Known Participant
August 29, 2013
Answered

Decrementing a date

  • August 29, 2013
  • 1 reply
  • 4104 views

I do a monthly newsletter for a small business that hosts about 30 classes each month and they require the customers to prepay by the day before the class to get a discounted price, otherwise the price increases if you pay on the day of the class. For example:

Sunday, Oct 6, 1:00 - 3:00 PM

Instructor: Professor Smith

1 Class - $30 by Oct 5 or $45 at the door.

What I'd like to do (via a script) is to find and copy the "6" (from Oct 6) then decrement it by 1 and place it in the "by Oct 5" day place.

Is there a way to do this? It would save a HUGE amount of time each month.

Any help is appreciated.

⁓Rocky

This topic has been closed for replies.
Correct answer DaveSofTypefi

Okay, so I made a serious mistake. I kept running an older script. DOH! I hate when that happens!! My script file is very full of experiments for this and it can get a little confusing sometimes.

OMG! This script does exactly what I was trying to do all along!!!! You are a GENIUS!!! This is brilliant! WOW!

Thank you a million times!

Warmest regards,

⁓Rocky


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.index + 2;

                              var endIndex = myFinds.characters[-1].index;

                              myFinds.parent.characters.itemByRange(startIndex, endIndex).texts[0].contents = decString;

                    }

          }

          function getTextFromSelection(sel) {

                    if (sel.hasOwnProperty("baseline")) {// sel is text

                              return sel.parent.texts[0];

                    } else if (sel.hasOwnProperty("texts")) { // sel owns a text object

                              return sel.texts[0];

                    }

                    return null;

          }

          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

1 reply

Inspiring
September 1, 2013

This can be done by script fairly easily if your document is set-up with paragraph styles to make it easy to find the text in question.

Dave

Rocky Berlier
Known Participant
September 1, 2013

Hello Dave,

Thanks for responding.

The three lines of the class info (Date/time, Instructor, and Price) are assigned a different style each (named "classInfo1, classInfo2, and classInfo3"). What I want to do is to grab the date number of "classInfo1",  decrement it then place it in the proper date place of "classInfo3".

Where I need help is in the scripting part. The month is actually a defined Text Variable that changes with each month for the newletter. Using a newsletter  template with the variables defined and placed, I simply redefine the month Text Variable each time I create a new newsletter. Then all classes that have the month defined (as well as elsewhere in the newsletter) are changed. This works great and is a real time saver.

However, regarding setting the day-before-date, I've tried doing a search and replace but this doesn't help with decrementing. It's the decrementing part that I am really struggling with. I can copy the day to the clipboard but getting it out of the clipboard and decremented is where I am having problems.

If you have some practical scripting ideas, Dave, I'd greatly appreciate the assistance.

Warmest regards,

⁓Rocky

**_Rocky Berlier_** __________________________ Photoshop Educator • Freelance Technical Writer/Editor • Adobe Press© / Peach Pit Press© • [Technical Editing and Book Projects](https://www.amazon.com/gp/registry/wishlist/13FPVO0PB9V13/ref=cm_wl_huc_view "Technical Editing and Book Projects") Mac Pro5,1 (mid-2010) • OS Mojave 10.14.6 • Radeon RX580 8192 MB • 32 GB RAM • LG 38" Display • OWC Mercury SSD 1TB • Seagate 4TB HD (external)
Inspiring
September 2, 2013

Decrementing a number is easy, assuming you've found it in text:

myNumber = Number(myFoundText);

myDecNum = myNumber - 1;

myFoundText.contents = myDecNum;

But what if the number is 1? You don't want zero? So extra logic is needed to handle month boundaries.

Dave