6 Replies Latest reply on Feb 4, 2010 11:31 PM by Harbs.

    [JS][ID4]How to parse a string variable

    Em Space

      I know this is the sort of thing any competent scripter should already know, but then again, I never claimed to be competent.

       

      I've got a script that helps me with indexing, and I want to add a feature. In certain cases, InDesign alphabetizes differently than I would. For example, "Applesauce Cake" would show up before "Apple Stuffing." (Can you tell I do cookbooks?) I think that the space should alphabetize before any letter, while InDesign treats the space as if it weren't there.

       

      So, my script involves a generating a dialog in which I can edit the 'Sort As:' attribute of the entries. I try to catch each of these alphabetiziation difficulties and replace the space with the number '1' so that it will sort my way.

       

      The 'Sort As' text box in my dialog is populated with the contents of the selection I make before running the script. What I'd like to do is have the script change all the spaces to 1s by itself, then present the results in the text box. I don't know how to edit a string variable in this way.

       

      Who wants to help the incompetent?

        • 1. Re: [JS][ID4]How to parse a string variable
          Harbs. Level 6

          You can feed the sort() method a function like so: string.sort(function)

           

          This allows you to sort based on whatever criteria you want.

           

          In your case, the standard sort() will work just fine to sort in the 

          order you want. However you might want to change the alphabetical 

          sorting with capitals and lowercase...

           

          Harbs

          • 2. Re: [JS][ID4]How to parse a string variable
            Em Space Level 1

            If I understand you correctly, I don't think that's what I need to do.

             

            Perhaps more informating is needed. The point of the larger script is to allow me to create a number of entries from a single selection. I select a recipe title, and run the script. It creates a dialog that has two text boxes, one for the entry and another for the 'sort as.' Below that are a lot of checkboxes. The checkboxes allow me to select alphabetical, some categories ('Desserts,' 'Entrées,' etc.) and some ingredients ('Broccoli,' 'Salmon,' etc.). I select those that apply to that recipe and click OK.

             

            The script then creates entries for each box checked, putting the recipe name at level two, and the checkbox name as level 1. Alphabetical entries get the first letter of the Sort As text entry as thier level 1, and a pound sign as the sort as attribute. The categories are given a dollar sign in thier Sort As attribute, and the ingredients sort as normal. Then, when I generate my index, I get all the recipe names in alphabetical order, followed by the categories and the recipes in them, and lastly the ingredients with the recipes that contain them. 3 indexes in one.

             

            Except that certain recipes are sorted wrong in the index. So I'd like to force the index feature of InDesign to treat them right by changing the Sort As attribute.

             

            But you don't really need to know any of this. I just include it in case the concrete reason for my need is unclear.

             

            All I need to know is how to take a variable that contains a text string and change some of the characters to something else. In other words, how do I deal with the parts of a string, rather than the whole string as one entity.

            • 3. Re: [JS][ID4]How to parse a string variable
              Michael L Hale Level 5

              Edit: error in code

              • 4. Re: [JS][ID4]How to parse a string variable
                Peter Kahrel Adobe Community Professional & MVP

                There are several string-manipulation functions. Look in any JavaScript book for details. For example, to replace all the spaces in a string with 1s, do this:

                 

                myString = myString.replace (" ", "1")

                 

                Other useful string functions are .slice (), .search(), and .indexOf(). And as Michael mentioned, learning some basic regular expressions is well worth the effort.

                 

                Peter

                • 5. Re: [JS][ID4]How to parse a string variable
                  Em Space Level 1

                  Thanks. That's exactly what I need.

                   

                  Guess I have to get myself a good book if I want to get any better. I think I've just about learned all I'll be able to from the PDF manuals Adobe provides.

                  • 6. Re: [JS][ID4]How to parse a string variable
                    Harbs. Level 6

                    If you're looking for good books,then get yourself Peter's books here:

                    http://www.oreillynet.com/pub/au/2758

                     

                    I've found the pdfs on this site very useful as well:

                    http://www.hunlock.com/

                    1 person found this helpful