Back

If your email is not recognized and you believe it should be, please contact us.

  • You must be logged in to reply to this topic.Login

Grep – Can you have it do math?

Return to Member Forum

  • Author
    Posts
    • #117604
      Kelli Fleck
      Member

      I am looking to insert a variable for the years of experience on a resume. I am hoping Grep can do a math equation so I could say [current year] minus 1976 = output. The output would be what shows up in the document. Am I just dreaming or is Grep capable of this sort of command?

    • #117606
      David Blatner
      Keymaster

      Sorry, GREP cannot do that.

    • #117609

      Indeed GREP is thinking in the wrong direction. GREP is nothing more than a (very) fancy search-and-replace function.

      That said: the functionality you are looking for – math calculations – can be done inside InDesign through a script! A script has access to the Text Variables in your current document (https://helpx.adobe.com/indesign/using/text-variables.html) and so it can change their contents. And JavaScript (my preferred InDesign scripting language) has built-in operators to retrieve the current date.

      If you write the above down in a basic script, you’ll practically have what you ask for; but then the problem is when to run this script. After all, if you cannot remember to change a year value once a year, you’d also be prone to forgetting to run the script at least once a year (“at least once”, because once run, it’d set the variable to the correct value for the entire year to come).

      It’s far easier if your friendly InDesign kept the variable up-to-date. And it can! Here, copy this rather short-but-sweet script into the Extendscript Toolkit Editor and save it as a new file “set_experience_variable.jsx” in a folder called “Startup Scripts” – not your regular “Scripts Panel” folder. In case this is new for you, see “Download the script” in https://creativepro.com/add-missing-options-to-the-book-menu.php for a practical step-by-step instruction. (But no need to download that script.)

      //DESCRIPTION:Write date difference in variable
      // A Jongware Script 12-Jul-2019
      #targetengine "set_experience_variable"

      app.addEventListener("afterOpen", check_and_set_variable, false);

      function check_and_set_variable (event)
      {
      var theDoc = event.parent;
      if (theDoc instanceof Document)
      {
      var tvar = theDoc.textVariables.item('experience');
      if (tvar.isValid && tvar.variableType == VariableTypes.CUSTOM_TEXT_TYPE)
      {
      tvar.variableOptions.contents = String(new Date().getFullYear() - 1976)
      }
      }
      }

      For the script to work at start-up, you must quit and restart InDesign once you copied this into the Startup Scripts folder, but afterwards it should fire automatically. What it does is, every time you OPEN an existing document, it checks if there is a Text Variable called “experience” in that document. If it is, it double-checks if its type is “Custom Text” (so it can change the contents), and if that is the case, it immediately sets the contents string to your difference in years.

      You can see it works correctly: 1. Create a new document (or open an existing one). 2. Create a new Text Variable of type “Custom Text” and name it “experience”. Set its contents to some random text such as “oooh so clever”. 3. Insert this text variable somewhere in an appropriate place in the document text. For now, it’ll read “I have ooh so clever years of experience”. 4. Save and close the document. 5. The magic happens when opening this document again – please do so now!

      If you followed all the steps above correctly, the text will now magically read “I have 43 years of experience”. And it will automatically update each new year!

    • #117611
      Kelli Fleck
      Member

      Wow thank you Theunis! I will try that. :)

    • #117612
      Kelli Fleck
      Member

      Do you think the script can grab information from the variable text, which instead of “oh so clever” would be the date they started in the industry. Then it would be

      [current year] – [variable text input of year started in the industry]

      I have many resumes. If not, then I will just create multiples of this script for each year. :) Thank you so much!

    • #117616

      Kelli, I’m afraid that won’t work. In the current setup, the original value gets overwritten once, and then you would have lost its original value for the next time.

      Since you mention multiple years (I was basically thinking you’d need only one), I’m thinking a more complicated set-up would still be possible. Something like:

      1. you create a variable “year” and set its contents to your starting year;
      2. on opening the document, the script creates a *new* variable named “year-now” and sets its value to “now minus variable value”. You would use *this* variable in your document, not the original “year” one;
      3. .. and on saving and then re-opening this document, the variable “year-now” would get updated but “year” would stay the same.

      If you need to be able to manually add more variables, I can think of two further scenario’s. Either you could add each one to the script (requiring a restart each time you modify it), or devise a consistent naming scheme where a set prefix determines whether the new variable should be created. Such a prefix could be something like year-” and so, when you add a new variable “year-DavidB”, the script checks all variables starting with “year-” and creates “now-DavidB”. (It cannot create “year-DavidB-now”, because a next time it would create one called “year-DavidB-now-now” … and so on … see the problem? :)

      If on of these workflows would work for you, I could look into it. Other script writers are hereby invited to think along :)

    • #117619
      Kelli Fleck
      Member

      That sounds like what I need! I have resumes in separate documents, so each document would have the year set up to when they started in the industry. I believe the script would then set the “year-now” just as I need it. I won’t have an instance where I need multiple years of experience within the same document, but I have several separate documents with different years of experience. Hope that helps clear it up.

    • #117636

      Ah, that took a bit of tinkering but I think I got it narrowed down. Here is the startup script – if there is a variable called year in a document that you open (it blindly assumes this has a reasonable value!), an existing variable year-now will be updated, and if it does not exist, it will be created first.

      //DESCRIPTION:Write date difference in variable
      // A Jongware Script 15-Jul-2019
      #targetengine "set_experience_variable"
      app.addEventListener("afterOpen", check_and_set_variable, false);
      function check_and_set_variable (event)
      {
      var theDoc = event.parent;
      if (theDoc instanceof Document)
      {
      var tvar = theDoc.textVariables.item('year');
      if (tvar.isValid && tvar.variableType == VariableTypes.CUSTOM_TEXT_TYPE)
      {
      var uvar = theDoc.textVariables.item('year-now');
      if (!uvar.isValid)
      uvar = theDoc.textVariables.add({name:'year-now', variableType:VariableTypes.CUSTOM_TEXT_TYPE});
      uvar.variableOptions.contents = String(new Date().getFullYear() - Number(tvar.variableOptions.contents))
      }
      }
      }

      … and a second script that you can place in your regular scripts folder. This does the same thing, but directly into your current document; that way you don’t have to close-and-reopen a document if you want to add the variables!

      //DESCRIPTION:Write date difference in variable
      // A Jongware Script 15-Jul-2019
      var theDoc = app.activeDocument
      var tvar = theDoc.textVariables.item('year');
      if (tvar.isValid && tvar.variableType == VariableTypes.CUSTOM_TEXT_TYPE)
      {
      var uvar = theDoc.textVariables.item('year-now');
      if (!uvar.isValid)
      uvar = theDoc.textVariables.add({name:'year-now', variableType:VariableTypes.CUSTOM_TEXT_TYPE});
      uvar.variableOptions.contents = String(new Date().getFullYear() - Number(tvar.variableOptions.contents))
      }

Viewing 7 reply threads
  • The forum ‘General InDesign Topics (CLOSED)’ is closed to new topics and replies.
Forum Ads