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

Export 000-000-000 instances to text or .csv

Return to Member Forum

  • Author
    Posts
    • #62923
      nellbern
      Member

      Looking for advise how to approach this situation.

      I need to export as text or a .csv document all instances of this pattern 000-000-000 (Example 980-800-603) in hundreds of ID documents. I was playing around with the following grep expresion:

      d{3}[-]d{3}[-]d{3} which it works & finds the 9 digit plus 2 hyphens but I dont want to copy each find & paste it into Excel. The good thing that pattern is in a layer with another text elements. I can select that layer but it will export everything. How can I do that. Is it psosible to this & get also the file name to know from which document came from?

    • #62940
      nellbern
      Member

      I found an obstacale, the expresion d{3}[-]d{3}[-]d{3} works but it is also finding phone numbers 895-780-4080 which technically is part of the expresion & correct but I don't need them. I know grep is greedy but then What is the correct grep expresion in this case?

    • #62947

      GREP is indeed greedy, but that's not the issue here. It's only greedy when you use potentially “unlimited match” operators + and *. For your phone numbers it still works as intended, finding three sets of three digits — but your phone number also contains a fourth. Note that this fourth one is not 'matched', i.e., it's not included in the selection. Then again, matching the partial phone number is also no good.

      So in this case you want to find 3 sets of 3 digits not followed by a fourth digit. That can be expressed as such:

      d{3}-d{3}-d{3}(?!d)

      As for the rest of your script … it's really fairly easy, honestly — I just don't seem to find the time to put something together, test it, and post it here :( But in short: if you are able to use “find” to match the exact stuff you need, you can loop over the resulting 'found items' array and write this text into a file.

      One note: this method will give you a list of just those numbers. Is that really all you need — the numbers, nothing more?

    • #62967
      nellbern
      Member

      Thank You Jongware! Wonderful & clear explanation :-)

      “One note: this method will give you a list of just those numbers. Is that really all you need — the numbers, nothing more?”

      That is exactly what I need. Like this:

      Page 456.indd

      789-456-123

      741-852-963

      159-951-753

      Page 562

      748-985-698

      125-985-963

    • #62977

      Okay, let's try this — please make sure to check the script very carefully after copying, because the forum software may attempt to prettify the code!

      This script lets you select a single folder, then it goes over all .indd files in it, gathers your numbers, and finally lets you select an output file.

      /* 1. What you want? */
      var foundList = [];
      var docFolder = Folder(Folder.myDocuments).selectDlg('Select a folder');

      if (docFolder != null)
      {
      /* 2. How much of'em? */
      fileList = docFolder.getFiles(“*.indd”);

      for (f=0; f<fileList.length; f++)
      {
      /* 3. What you want with each? */
      app.scriptPreferences.userInteractionLevel = UserInteractionLevels.NEVER_INTERACT;
      try {
      doc = app.open (fileList[f]);
      } catch (e)
      {
      foundList = foundList.concat ([fileList[f], “(Unable to open)”], '');
      app.scriptPreferences.userInteractionLevel = UserInteractionLevels.INTERACT_WITH_ALL;
      continue;
      }
      foundList = foundList.concat (getFoundList (doc));
      doc.close(SaveOptions.NO);
      app.scriptPreferences.userInteractionLevel = UserInteractionLevels.INTERACT_WITH_ALL;
      }
      }

      /* 4. Where you want it? */
      defaultFile = new File (Folder.myDocuments+”/numbers.csv”);
      writeFile = defaultFile.saveDlg( 'Save report', File.fs == “Windows” ? “CSV file:*.csv;All files:*.*” : function(file) { return file instanceof Folder || (!(file.hidden) && file.name.match(/.csv$/i)); } );

      if (writeFile != null)
      {
      if (writeFile.open(“w”))
      {
      writeFile.encoding = “utf8”;
      writeFile.write (foundList.join('r')+'r');
      writeFile.close();
      } else
      {
      alert (“Something went wrong attempting to save!”);
      }
      }

      function getFoundList (someDoc)
      {
      var i, thisDocList;

      /* Where you at? */
      var documentName = someDoc.name;

      /* What you want? */
      app.findGrepPreferences = null;
      app.findGrepPreferences.findWhat = “\d{3}-\d{3}-\d{3}(?!\d)”;
      thisDocList = someDoc.findGrep();

      if (thisDocList.length == 0)
      return [];

      for (i=0; i<thisDocList.length; i++)
      thisDocList[i] = thisDocList[i].contents;

      /* Where you want it? */
      return [ documentName ].concat(thisDocList);
      }

    • #63144
      nellbern
      Member

      Jongware,

      Sorry for MY ignorance but copied & pasted your script into ExtendScript Toolkit & save as .jsx on the scipts folder in ID. When I run it I'm getting a Javascript error number 4 Error string “Underterminated string constant”

    • #63146

      I guess you'll have to note down the line number in that error dialog, and then check the line in the ESTK.

      The ESTK should be displaying text strings with a different color or font, so it should be easy to spot where it goes astray. The same thing goes for the numerous comments — they should end at the next */ sequence, and if the color coding shows it seems to continue that, check for stray spaces etc.

      Possibly one of the quotes got changed on copying to a curly quote, and That Won't Do for Javascript — both single and double quotes ought to be straight.

Viewing 6 reply threads
  • You must be logged in to reply to this topic.
Forum Ads