1 2 Previous Next 42 Replies Latest reply on Jan 22, 2013 3:54 PM by Peter Kahrel

    Auto Subtract Time Zone Values

    MadMac55

      I'd like to automate changing all EST times to PST.  And this tool   —   http://indesignsecrets.com/free-script-to-change-all-numbers-in-a-document-using-math.php  —  comes close.  There is a custom RegEx search param, but I cannot make it look at 1:45 and subtract the value to 10:45.  Maybe this is imposssible.

        • 1. Re: Auto Subtract Time Zone Values
          Kasyan Servetsky Level 5

          I tried to find one or two digits followed by a colon and two digits but this resulted in negative numbers. I think this script simply can't work with date/time format.

          dialog.png

          Before

          before.png

          After

          after.png

          • 2. Re: Auto Subtract Time Zone Values
            Peter Kahrel Adobe Community Professional & MVP

            Here's a time changer:

             

            function change_time (n, clocktype)
                {
                app.findGrepPreferences = null;
                app.findGrepPreferences.findWhat = '\\d\\d?(?=:\\d\\d)';
                var hr, hours = app.documents[0].findGrep();
                for (var i = hours.length-1; i > -1; i--)
                    {
                    hr = eval ("Number(hours[i].contents)" + n);
                    if (hr >= clocktype)
                        hr = hr - clocktype;
                    else
                        if (hr < 0)
                            hr = clocktype-Math.abs(hr);
                    hours[i].contents = String (hr);
                    }
                }
            

            The function's first parameter is a string specifying the number of hours to add or subtract, the second parameter indicates the clock type, 12 or 24 hrs. Sample calls:

             

            change_time ("-1", 24);  // subtract 1 hour, using a 24-hr clock

            change_time ("+3", 12);  // add 3 hours using a 12-hr clock

             

            A fancy addition would be something to toggle between "am" and "pm" strings in 12-hr clocks: "0:15 am" minus an hour is "11:15 pm".

             

            Peter

            1 person found this helpful
            • 3. Re: Auto Subtract Time Zone Values
              Kasyan Servetsky Level 5

              It’s cool, Peter. I put it into my receptacle of “useful snippets”.

              • 4. Re: Auto Subtract Time Zone Values
                MadMac55 Level 1

                Peter!!!   Wooooooo-hooooooo!   You rule, man!

                 

                That was outstanding.  And quite a lesson to be learned within the code.  I'll have to spread the good word on this post.  What a way to start the morning.  :-)

                • 5. Re: Auto Subtract Time Zone Values
                  MadMac55 Level 1

                  I noticed that the 12-hour clock will return "0:45"  rather than 12:45 when subtracting 2 hours from 2:45. 

                   

                  Is that a Java limitation?

                  • 6. Re: Auto Subtract Time Zone Values
                    Peter Kahrel Adobe Community Professional & MVP

                    No, that's my limitation, nothing to do with javaScript! The fix is below.

                     

                    Peter

                     

                    function change_time (n, clocktype)
                        {
                        app.findGrepPreferences = null;
                        app.findGrepPreferences.findWhat = '\\d\\d?(?=:\\d\\d)';
                        var hr, hours = app.documents[0].findGrep();
                        for (var i = hours.length-1; i > -1; i--)
                            {
                            hr = eval ("Number(hours[i].contents)" + n);
                            if (hr > clocktype)
                                hr = hr - clocktype;
                            else
                                if (hr < 1)
                                    hr = clocktype-Math.abs(hr);
                            hours[i].contents = String (hr);
                            }
                        }

                    • 7. Re: Auto Subtract Time Zone Values
                      MadMac55 Level 1

                      A thing of beauty, a stroke of genius.   Works awesome

                       

                      What should we call it?  Clockwork or TimeChanger?  

                      • 8. Re: Auto Subtract Time Zone Values
                        Peter Kahrel Adobe Community Professional & MVP

                        "Time for a change"?

                         

                        P.

                        • 10. Re: Auto Subtract Time Zone Values
                          tkrisztian92

                          could you please help me ? where i should write this "expression" ?

                          • 11. Re: Auto Subtract Time Zone Values
                            Peter Kahrel Adobe Community Professional & MVP

                            Could you explain a bit further what you want to do?

                            • 12. Re: Auto Subtract Time Zone Values
                              tkrisztian92 Level 1

                              i have a long text with american time format (such as: 02:40 pm) and i want to convert them to european format (14:40). can i use it for this ?

                              • 13. Re: Auto Subtract Time Zone Values
                                tkrisztian92 Level 1

                                okey, so there isn't.

                                • 14. Re: Auto Subtract Time Zone Values
                                  Peter Kahrel Adobe Community Professional & MVP

                                  You could use that script after some changes. But you could do it in the interface with a series of find-replace operations.

                                   

                                  1. Find two-digit numbers followed by a colon, a two-digit number, a space, and 'pm', and replace that first two-digit number by itself plus 12:

                                   

                                  Find what: 02(?=:\d\d\spm)

                                  Replace with: 14

                                   

                                  Do that for 00, 01, 02, etc.

                                   

                                  Then delete am and pm when they're preceded by \d\d:\d\d. Here's a script that does all that:

                                   

                                   

                                  app.findGrepPreferences = app.changeGrepPreferences = null;
                                  
                                  us_eu ('00(?=:\\d\\d\\spm)', '12');
                                  us_eu ('01(?=:\\d\\d\\spm)', '13');
                                  us_eu ('02(?=:\\d\\d\\spm)', '14');
                                  us_eu ('03(?=:\\d\\d\\spm)', '15');
                                  us_eu ('04(?=:\\d\\d\\spm)', '16');
                                  us_eu ('05(?=:\\d\\d\\spm)', '17');
                                  us_eu ('06(?=:\\d\\d\\spm)', '18');
                                  us_eu ('07(?=:\\d\\d\\spm)', '19');
                                  us_eu ('08(?=:\\d\\d\\spm)', '20');
                                  us_eu ('09(?=:\\d\\d\\spm)', '21');
                                  us_eu ('10(?=:\\d\\d\\spm)', '22');
                                  us_eu ('11(?=:\\d\\d\\spm)', '23');
                                  
                                  us_eu ('(?<=\\d\\d:\\d\\d)\\s[ap]m', "");
                                  
                                  function us_eu (f, r)
                                      {
                                      app.findGrepPreferences.findWhat = f;
                                      app.changeGrepPreferences.changeTo = r;
                                      app.activeDocument.changeGrep();
                                      }
                                  

                                   

                                  Peter

                                  • 15. Re: Auto Subtract Time Zone Values
                                    tkrisztian92 Level 1

                                    thanks, this works for a while. but i have time in format: 3:54 not in 03:54. so when i search for 3(?=:\d\d\spm) it finds number 3 from (previously changed) 13:00 also and makes 115:00.

                                    • 16. Re: Auto Subtract Time Zone Values
                                      tkrisztian92 Level 1

                                      i know i've written two digits in my previous post. that was my fault. sorry for that.

                                      • 17. Re: Auto Subtract Time Zone Values
                                        Peter Kahrel Adobe Community Professional & MVP

                                        In that case search for \b3(?=:\d\d\spm) (where \b stands for 'word boundary'). It's worth your while to read up on grep.

                                         

                                        Peter

                                        • 18. Re: Auto Subtract Time Zone Values
                                          tkrisztian92 Level 1

                                          thanks. one more question: your first srcipt. can it change the times like f.e.g. 14:26 +1 15:26 ? how can i use it ? (i tried to make a .jsx file of it, but it did nothing)

                                          • 19. Re: Auto Subtract Time Zone Values
                                            Peter Kahrel Adobe Community Professional & MVP

                                            Add this line:

                                             

                                            change_time ("+1", 24);

                                             

                                            See the second post in this thread.

                                            • 20. Re: Auto Subtract Time Zone Values
                                              tkrisztian92 Level 1

                                              yes, but how can i apply your script ? i've never done similar things before. i'm absolutely beginner in ID.

                                              • 22. Re: Auto Subtract Time Zone Values
                                                MadMac55 Level 1

                                                Hey Peter, very cool followup to our original script discussion.  And when I think about that, I'm surprised I didn't think to ask the following:

                                                 

                                                You easily solved the lookup of a time such as 4:30.  Search was based on the digit + colon followed by 2 digits.
                                                What if someone had written 4:30 in there, but also a 4 in there and with variable p.m. or a.m. or pm or am? 

                                                 

                                                Is there a tight search string (without having to list every single time 1 thru 12 etc, or every am or a.m.) that would knock that variability out of the park?

                                                 

                                                Also, are you getting any closer to your update on your book?  I'm subscribed to O'Reilly's newsletter so I'm likely the first to order it.  :-)

                                                • 23. Re: Auto Subtract Time Zone Values
                                                  Peter Kahrel Adobe Community Professional & MVP

                                                  > Is there a tight search string  that would knock that variability out of the park?

                                                   

                                                  Not sure that I get what you mean. Should things like 4pm and 10am be ignored or should they be included? You're right that now they're ignored because they have no :\d\d.

                                                   

                                                  > Also, are you getting any closer to your update on your book?

                                                   

                                                  Working on it. . .

                                                   

                                                  Peter

                                                  • 24. Re: Auto Subtract Time Zone Values
                                                    MadMac55 Level 1

                                                    Exactly.

                                                     

                                                    If in the same paragraph we had references that were 4:30 pm, 4 pm, could 4 pm (or am) be changed to subtract as we've told in the case of 4:30 pm?

                                                    • 25. Re: Auto Subtract Time Zone Values
                                                      Peter Kahrel Adobe Community Professional & MVP

                                                      I'm sure it could, but that requires more tinkering. In the meantime, you could add : 00 (without the space between the colon and the first zero; damn the bloody Jive double guesser) to digits not followed by colon+digit+digit, run the time changer, then remove : 00 when it occurs between : and [ap]m

                                                       

                                                      Peter

                                                      1 person found this helpful
                                                      • 26. Re: Auto Subtract Time Zone Values
                                                        MadMac55 Level 1

                                                        Hey Peter, I am close!!

                                                         

                                                        Just one thing left to understand -- how do I run 2 findGREP statements to catch them all?

                                                        You'll see that it only runs the last findWhat, but I dont know what syntax (im sure it's a simple statement) to include to run both.

                                                         

                                                         

                                                        function change_time (n, clocktype)

                                                            {

                                                            app.findGrepPreferences = null;

                                                           app.findGrepPreferences.findWhat = '\\d\\d?(?=:\\d\\d)';

                                                           app.findGrepPreferences.findWhat = '\\d\\d?(?=\\s[ap]\\.m\\.)';

                                                            var hr, hours = app.documents[0].findGrep();

                                                            for (var i = hours.length-1; i > -1; i--)

                                                                {

                                                                hr = eval ("Number(hours[i].contents)" + n);

                                                                if (hr > clocktype)

                                                                    hr = hr - clocktype;

                                                                else

                                                                    if (hr < 1)

                                                                        hr = clocktype-Math.abs(hr);

                                                                hours[i].contents = String (hr);

                                                                }

                                                            }

                                                        change_time ("-1", 12);

                                                        • 27. Re: Auto Subtract Time Zone Values
                                                          [Jongware] Most Valuable Participant

                                                          MadMac55 wrote:

                                                           

                                                          Just one thing left to understand -- how do I run 2 findGREP statements to catch them all?

                                                          You'll see that it only runs the last findWhat, but I dont know what syntax (im sure it's a simple statement) to include to run both.

                                                           

                                                          Funny enough, it's not a simple statement. You could do the first findGrep, change the numbers with the for..loop, then repeat it with the second findGrep. Alternatively, you could change the two GREP statements and combine them into one (it's possible using the OR operator, I guess; I didn't try).

                                                           

                                                          But this also works: since "findGrep" returns an array, you can manipulate it just like all arrays. That means you can run the first FindGrep, store the 'return array', then concatenate the result of the second FindGrep!

                                                           

                                                          app.findGrepPreferences = null;

                                                          app.findGrepPreferences.findWhat = '\\d\\d?(?=:\\d\\d)';

                                                          var hr, hours = app.documents[0].findGrep();

                                                          app.findGrepPreferences.findWhat = '\\d\\d?(?=\\s[ap]\\.m\\.)';

                                                          hours = hours.concat(app.documents[0].findGrep());

                                                           

                                                          So is this a smart way? Nope

                                                          The first GREP can change the number of digits in your times, from 2 to 1 (f.i., from "10:00" to "9:00"). Then the second GREP is off ...

                                                          1 person found this helpful
                                                          • 28. Re: Auto Subtract Time Zone Values
                                                            MadMac55 Level 1

                                                            Theunis, that works like a charm!   But you don't seem to like it that much? 

                                                             

                                                            Really, I think it's a creative working solution.  Duvel's all around, I say !!! 

                                                            • 29. Re: Auto Subtract Time Zone Values
                                                              Peter Kahrel Adobe Community Professional & MVP

                                                              Nice idea, Theunis. The two greps can be combined, see below. But MadMac's \d\d?(?=\s[ap]\.m\.) catches times like 8:45 am twice: the first match is 8, the second match is 45. No amount of negative lookbehinding for : seems to prevent the double match. I don't understand why that is -- you'd think that something like \\d\\d?(?=:\\d\\d|\\s[ap]\\.m\\.) would do it, but it doesn't. It looks as if you have to allow for leading colons, then exclude them later:

                                                               

                                                              function change_time (n, clocktype)
                                                                  {
                                                                  app.findGrepPreferences = null;
                                                                  app.findGrepPreferences.findWhat = ':?\\d\\d?(?=:\\d\\d|\\s[ap]\\.m\\.)';
                                                                  var hr, hours = app.documents[0].findGrep();
                                                                  for (var i = hours.length-1; i > -1; i--)
                                                                      {
                                                                      if (hours[i].contents[0] != ':')
                                                                          {
                                                                          hr = eval ("Number(hours[i].contents)" + n);
                                                                          if (hr > clocktype)
                                                                              hr = hr - clocktype;
                                                                          else
                                                                              if (hr < 1)
                                                                                  hr = clocktype-Math.abs(hr);
                                                                          hours[i].contents = String (hr);
                                                                          }
                                                                      }
                                                                  }
                                                              

                                                               

                                                              Not particularly elegant! There's bound to be a better solution, but i don't see it.

                                                               

                                                              Peter

                                                              • 30. Re: Auto Subtract Time Zone Values
                                                                MadMac55 Level 1

                                                                Killing me with laughter, Peter:   "lookbehinding."   Great one.  I'll substitute the change and throw redundancy in there.

                                                                • 31. Re: Auto Subtract Time Zone Values
                                                                  [Jongware] Most Valuable Participant

                                                                  Have Cake, Eat Too. I feel like there may be an easier way, but this will do for now:

                                                                   

                                                                  (?<!:)\b\d\d?(?=(:\d\d)?\s[ap]m)

                                                                   

                                                                  It works right out of the box in ID, but remember when inserting it into your script to double-up all backslashes.

                                                                  • 32. Re: Auto Subtract Time Zone Values
                                                                    Peter Kahrel Adobe Community Professional & MVP

                                                                    Aha -- it's the \b that does it. I thought about that, but then dismissed it thinking it wasn't going to work. Should try everything!

                                                                     

                                                                    Peter

                                                                    • 33. Re: Auto Subtract Time Zone Values
                                                                      MadMac55 Level 1

                                                                      OK, guys, get ready to laugh at me.  I pasted this in and did't get it to move at all.   Did I do something wrong?

                                                                       

                                                                      function change_time (n, clocktype)

                                                                          {

                                                                          app.findGrepPreferences = null;

                                                                          app.findGrepPreferences.findWhat = '(?<!:)\\b\\d\\d?(?=(:\\d\\d)?\\s[ap]m)';

                                                                          var hr, hours = app.documents[0].findGrep();

                                                                          for (var i = hours.length-1; i > -1; i--)

                                                                              {

                                                                              if (hours[i].contents[0] != ':')

                                                                                  {

                                                                                  hr = eval ("Number(hours[i].contents)" + n);

                                                                                  if (hr > clocktype)

                                                                                      hr = hr - clocktype;

                                                                                  else

                                                                                      if (hr < 1)

                                                                                          hr = clocktype-Math.abs(hr);

                                                                                  hours[i].contents = String (hr);

                                                                                  }

                                                                              }

                                                                          }

                                                                      change_time ("-1", 12);

                                                                      • 34. Re: Auto Subtract Time Zone Values
                                                                        Peter Kahrel Adobe Community Professional & MVP

                                                                        We got you confused, sorry about that. What you do is take the code you showed in post no. 29, then replace these two lines:

                                                                         

                                                                        app.findGrepPreferences.findWhat = '\\d\\d?(?=:\\d\\d)';

                                                                        app.findGrepPreferences.findWhat = '\\d\\d?(?=\\s[ap]\\.m\\.)';

                                                                         

                                                                        with this single one:

                                                                         

                                                                        app.findGrepPreferences.findWhat = '(?<!:)\\b\\d\\d?(?=(:\\d\\d)?\\s[ap]m)';

                                                                         

                                                                        Peter

                                                                        • 35. Re: Auto Subtract Time Zone Values
                                                                          MadMac55 Level 1

                                                                          That is what I did.  See post 33 for the entire code again.  Could I have done something wrong there?

                                                                           

                                                                          Somehow, you & Theunis have it working.  But nothing happens on mine.  No movement, not even an error.

                                                                           

                                                                          Whereas the code in Post 29 -- as is -- works just fine.   Maybe you could paste your complete code and let me compare?

                                                                          • 36. Re: Auto Subtract Time Zone Values
                                                                            Peter Kahrel Adobe Community Professional & MVP

                                                                            Here it is. Jongware's grep needed a small tweak as it doesn't match times not followed by am/pm. Also note the different format of the findWhat string. Using the /. . ./.source format there's no need to double-escape backslashes, so it's easier to copy expressions from the Find/Change dialog to a script file and back again.

                                                                             

                                                                            function change_time (n, clocktype)
                                                                                {
                                                                                app.findGrepPreferences = null;
                                                                                app.findGrepPreferences.findWhat = /(?<!:)\b\d\d?(?=:\d\d|\s[ap]m)/.source;
                                                                                var hr, hours = app.documents[0].findGrep();
                                                                                for (var i = hours.length-1; i > -1; i--)
                                                                                    {
                                                                                    hr = eval ("Number(hours[i].contents)" + n);
                                                                                    if (hr > clocktype)
                                                                                        hr = hr - clocktype;
                                                                                    else
                                                                                        if (hr < 1)
                                                                                            hr = clocktype-Math.abs(hr);
                                                                                    hours[i].contents = String (hr);
                                                                                    }
                                                                                }
                                                                            
                                                                            change_time ("-1", 12);
                                                                            

                                                                             

                                                                            Peter

                                                                            • 37. Re: Auto Subtract Time Zone Values
                                                                              MadMac55 Level 1

                                                                              Peter, this is the test sentence I used.

                                                                              I have to be at train stops by 10:05 p.m., 9 a.m. and 11 a.m. or 6 p.m. and 6:45 a.m.   By 1:30, I'll have traveled 111 miles.

                                                                               

                                                                              The code in Post 36 will produce this result:   

                                                                              I have to be at train stops by 9:05 p.m., 9 a.m. and 11 a.m. or 6 p.m. and 5:45 a.m.   By 12:30, I’ll have traveled 111 miles. 

                                                                              So, it misses the ref's to 9 a.m. and 11 a.m. or 6 p.m.

                                                                               

                                                                              Post 29 rules the roost, which I know you two felt was less elegant, but it catches all the problems.

                                                                              • 38. Re: Auto Subtract Time Zone Values
                                                                                Peter Kahrel Adobe Community Professional & MVP

                                                                                That's because the one in post 36 doesn't take into account the dots in a.m. and p.m. To fix that, change this:

                                                                                 

                                                                                [ap]m

                                                                                 

                                                                                into

                                                                                 

                                                                                [ap]\.m\.

                                                                                 

                                                                                and to allow both am and a.m. (and to make the expression more complictaed), use this:

                                                                                 

                                                                                [ap]\.?m.\?

                                                                                 

                                                                                Peter

                                                                                • 39. Re: Auto Subtract Time Zone Values
                                                                                  [Jongware] Most Valuable Participant

                                                                                  Finally, a test sentence! I was building up my regexp bit by bit, only guided by what you were telling use

                                                                                   

                                                                                  In this case, the "a.m." and "p.m" times get ignored because you mentioned "am or a.m.". Peter initially searched for "a.m." and I did so for "am". Since there seemed no particular objection against either, Peter adjusted my GREP in turn, producing the last script that now fails for you.

                                                                                   

                                                                                  Here you go: "a.m." and "p.m." now get matched correctly.

                                                                                   

                                                                                  function change_time (n, clocktype)
                                                                                      {
                                                                                      app.findGrepPreferences = null;
                                                                                      app.findGrepPreferences.findWhat = /(?<!:)\b\d\d?(?=:\d\d|\s[ap]\.m\.)/.source;
                                                                                      var hr, hours = app.documents[0].findGrep();
                                                                                      for (var i = hours.length-1; i > -1; i--)
                                                                                          {
                                                                                          hr = eval ("Number(hours[i].contents)" + n);
                                                                                          if (hr > clocktype)
                                                                                              hr = hr - clocktype;
                                                                                          else
                                                                                              if (hr < 1)
                                                                                                  hr = clocktype-Math.abs(hr);
                                                                                          hours[i].contents = String (hr);
                                                                                          }
                                                                                      }
                                                                                  
                                                                                  change_time ("-1", 12);
                                                                                  

                                                                                   

                                                                                  I tried it on your sample sentence, and it seems to me it Does The Job.

                                                                                   

                                                                                  Edited by: [Jongware] While I was testing it to work, Peter noticed the same and supplied the exact same solution (always a good thing). In your case, I'd go for his last expression; just because it would be nicer to have either "am" or "a.m." throughout your entire document, it's best not to trust your writers on this.

                                                                                  1 2 Previous Next