7 Replies Latest reply on May 29, 2009 1:00 PM by Peter Kahrel

    Export thousands of entries to separate files

    I.Am.SilverDollarNYC

      I don't know scripting. But I would sure like to find a script to do this little thing for me!

       

      Some background is necessary:

       

      Years ago, I was a dedicated HyperCard user. (If you know what I mean then you know my pain). I had a rolodex phone book with thousands of entries.  As OSX came along, I ran HyperCard in classic. Now that I'm on an Intel Mac, classic is no longer supported. My clever solution was to export my stack entries to text and bring them into InDesign.

       

      I'm limping along pretty well. Search in InD is very fast. And I've formatted the text using InD search and replace functions, I've made clear separations between entries. Each entry is one Paragraph, and between each entry is a spacer paragraph which carries a particular paragraph style called "Card Spacer". See the screenshot below.

       

      My goal now is to move the data to a more useful platform  - Notebook, Yojimbo come to mind. I'm thinking that what I need to do is EXPORT each entry as a separate text file to my hard drive. Then I can import the entries into the new database and each entry will be separate.

       

      I'm hoping that a kind member with the scripting skills to do this will help me out.

       

      With appreciation,

      Lance (NYC)

      Member NYInD User Group

      CS3 Intel Mac 10.5.7

      Picture 1.jpg

        • 1. Re: Export thousands of entries to separate files
          Peter Kahrel Adobe Community Professional & MVP

          Lance,

           

          Try the script below. Load your database file in InDesign, place the cursor somewhere in the text, and run the script. The individual files are written to the folder where the database is stored, using the filenames card-0.txt, card-1.txt, etc.

           

          Peter

           

           

          #target indesign;

          if (app.documents.length == 0) exit();
          if (app.selection.length == 0 || app.selection[0].constructor.name != "InsertionPoint")
             errorm ("Select an insertion point");
          folder = app.documents[0].fullName.path + "/";
          app.findGrepPreferences = app.changeGrepPreferences = null;
          story = app.selection[0].parentStory;

          replace ("\\n\\r", "\\r");
          replace ("(?<=\\r)\\r+", "");
          replace ("[\\r\\n]+\\Z", "");
          replace ("\\n", "@@##");

          records = story.contents.split ("\r");
          for (i = 0; i < records.length; i++)
             {
             temp = records[i].replace (/@@##/g, "\r");
             f = new File (folder + "card-" + String (i) + ".txt");
             f.open ("w");
             f.write (temp);
             f.close ()
             }

          function replace (f, r)
             {
             app.findGrepPreferences.findWhat = f;
             app.changeGrepPreferences.changeTo =r;
             app.documents[0].changeGrep ()
             }

          function errorm (m) {alert (m); exit ()}
          1 person found this helpful
          • 2. Re: Export thousands of entries to separate files
            [Jongware] Most Valuable Participant

            Although that script might work the way you asked, wouldn't it be (vastly) easier to repalce the soft returns with a tab and export the entire story to a plain text file? That would result in a CSV-(comma separated values) file, and most database programs should be able to read that.

            1 person found this helpful
            • 3. Re: Export thousands of entries to separate files
              I.Am.SilverDollarNYC Level 1

              Hey Peter!

               

              Thanks so much! The script worked, it created over 4,000 text files in the folder where the file sat. It took about 5 seconds! I think this will solve my problem.

               

              Before I close this thread (with a "Yes, answered" click), there's one minor hiccup I'd like to show you. The script is creating files both for my entry paragraphs and also for the spacer paragraphs. So I get one spacer text file for each "record" file.

               

              I thought it would be easy to simply delete all the odd or all the even numbered files, but for some reason, the odd/eveness of the record files is not the same throughout the 4000 files. Probably there is an errant extra Hard Return character occasionally, causing the alternation of odd/even to switch.

               

              Can the script be modified to skip the card spacers? Can you test for the paragraph style and skip paragraphs that are styled with "Card Spacer"? Of course, I can insert any other character into the spacer paragraphs if you'd rather filter for something else.

               

              I don't know if, or how, I would get rid of the extra records if I import them into the new program.

               

              Peter, this is not the first time I've used one of your scripts. Looking in my scripts folder, I saw another with your name: KahrelBatchExport.js. Thanks again for your help.

               

              Check out this screen shot.

              • 4. Re: Export thousands of entries to separate files
                I.Am.SilverDollarNYC Level 1

                That is an excellent idea.

                I just don't know exactly how Yojimbo, Notebook, Evernote (etc) work exactly since I don't have any of them and can't test it. Peter's script is working well (see my response) and I'm going to pursue that avenue for now.

                Still, I understand your idea and will keep it in mind.

                Thanks

                Lance

                • 5. Re: Export thousands of entries to separate files
                  Peter Kahrel Adobe Community Professional & MVP

                  The version here should take care of that hiccup. If it doesn't then you should let me have a look at part of that file.

                   

                  Peter

                   

                  #target indesign;
                  
                  if (app.documents.length == 0) exit();
                  if (app.selection.length == 0 || app.selection[0].constructor.name != "InsertionPoint")
                     errorm ("Select an insertion point");
                  folder = app.documents[0].fullName.path + "/";
                  app.findGrepPreferences = app.changeGrepPreferences = null;
                  story = app.selection[0].parentStory;
                  
                  replace ("\\n*\\r\\n*", "\\r");
                  replace ("(?<=\\r)\\r+", "");
                  replace ("\\A[\\r\\n]+", "");
                  replace ("[\\r\\n]+\\Z", "");
                  replace ("\\n", "@@##");
                  
                  records = story.contents.split ("\r");
                  for (i = 0; i < records.length; i++)
                     {
                     temp = records[i].replace (/@@##/g, "\r");
                     f = new File (folder + "card-" + pad (i, 4) + ".txt");
                     f.open ("w");
                     f.write (temp);
                     f.close ()
                     }
                  
                  function replace (f, r)
                     {
                     app.findGrepPreferences.findWhat = f;
                     app.changeGrepPreferences.changeTo =r;
                     app.documents[0].changeGrep ()
                     }
                  
                  function pad (n, len) {return ("000"+String(n)).slice (-len)}
                  function errorm (m) {alert (m); exit ()}
                  • 6. Re: Export thousands of entries to separate files
                    I.Am.SilverDollarNYC Level 1

                    Peter,

                     

                    I am SO HAPPY to have that problem resolved. It's been a roadblock for me for years as I tried to find a replacement for Hypercard.

                     

                    the script you e-mailed to me (off forum) worked perfectly.  I'm sorry that I posted those Blue on Blue formatting characters. No wonder you couldn't see the returns. I'm including a screen shot for posterity that will make the formatting easy to see.

                     

                    Do you want to post the final version to complete the thread?

                     

                    Can't thank you enough!

                     

                    Best,

                    Lance

                    • 7. Re: Export thousands of entries to separate files
                      Peter Kahrel Adobe Community Professional & MVP

                      > Do you want to post the final version to complete the thread?

                       

                      Here goes --

                       

                      Peter

                      www.kahrel.plus.com

                       

                      #target indesign;
                      
                      if (app.documents.length == 0) exit();
                      if (app.selection.length == 0 || app.selection[0].constructor.name != "InsertionPoint")
                         errorm ("Select an insertion point");
                      folder = app.documents[0].fullName.path + "/";
                      app.findGrepPreferences = app.changeGrepPreferences = null;
                      story = app.selection[0].parentStory;
                      
                      replace ("(?i)\\s*New Card START\\s*", "\\r");
                      replace ("\\A[\\r\\n]+", "");
                      replace ("[\\r\\n]+\\Z", "");
                      replace ("\\n", "@@##");
                      
                      records = story.contents.split ("\r");
                      for (i = 0; i < records.length; i++)
                         {
                         temp = records[i].replace (/@@##/g, "\r");
                         f = new File (folder + "card-" + pad (i, 4) + ".txt");
                         f.open ("w");
                         f.write (temp);
                         f.close ()
                         }
                      
                      function replace (f, r)
                         {
                         app.findGrepPreferences.findWhat = f;
                         app.changeGrepPreferences.changeTo =r;
                         app.documents[0].changeGrep ()
                         }
                      
                      function pad (n, len) {return ("000"+String(n)).slice (-len)}
                      function errorm (m) {alert (m); exit ()}