3 Replies Latest reply on May 13, 2014 4:26 PM by snipppyBishop

    Scripting tips

    frameexpert Level 4

      Hi Scripters, I want to offer some advice to those seeking scripting help. First of all, this forum isn't too busy, but there are some extremely helpful people on here. If you follow this advice, it will make it easier for willing people to help you.

       

      One of the most important things you can do when solving a scripting problem is to isolate the problem to the smallest possible unit. For example, if you are solving a problem with tables, don't post code that processes all of the tables in the document. Isolate the code so it just works on the selected table. Or, if you are trying to apply a condition format to paragraphs, don't post all of the code for processing all of the components in a book. Instead, isolate your code to work on a single, selected paragraph. When code is isolated to small units like this, it makes it easier to test and troubleshoot. And when it doesn't work, it makes it easier for other people to stage things on their computers so they can see what is wrong.

       

      In general, this is the best way to develop scripting solutions anyway. When your individual "units" are tested and debugged, you can expand the code out to process an entire document or book. As an added benefit, your code becomes more modular and easier to reuse in other scripts. I will try to follow up with some specific code examples so you can see how it works. Please let me know if you have any questions or comments. -Rick Quatro

        • 1. Re: Scripting tips
          Arnis Gubins Adobe Community Professional & MVP

          Hi Rick,

           

          Great advice. I'll also add that posters should mention which version of FM that they are using and what platform they are running on. [Older versions of Extendscript had numerous issues]

           

          Also, for those reading this thread for the first time, please use the "Correct Answer" link when your problem has been solved so that others can be see that a solution exists and to give credit to who helped you out.

          • 2. Re: Scripting tips
            frameexpert Level 4

            Here is a good place to start. Someone recently asked for help applying a condition format to certain paragraphs. When working on paragraphs, first get your code working on the paragraph containing the insertion point. Here is a useful snippet for doing that:

             

            // Set a variable for the active document.
            var doc = app.ActiveDoc;
            
            // Get the paragraph at the beginning of the selection or insertion point.
            var pgf = doc.TextSelection.beg.obj;
            
            // Do something with the paragraph here:
            // ...
            
            

             

            Here is code you can use for working on the selected table, or the table containing your text cursor:

             

            // Set a variable for the active document.
            var doc = app.ActiveDoc;
            
            // Get the table currently selected or that contains the insertion point.
            var tbl = doc.SelectedTbl;
            
            // Do something with the table here:
            // ...
            
            

             

            Do you need to work on table cells? Here is how to isolate the cell object that contains your text cursor.

             

            // Set a variable for the active document.
            var doc = app.ActiveDoc;
            
            // Get the paragraph that contains the insertion point.
            var pgf = doc.TextSelection.beg.obj;
            
            // Get the table cell that contains the paragraph.
            var cell = pgf.InTextObj;
            
            // Do something with the cell here:
            // ...
            

             

            Or, find the row that contains the cell where your text cursor is:

             

            // Set a variable for the active document.
            var doc = app.ActiveDoc;
            
            // Get the paragraph that contains the insertion point.
            var pgf = doc.TextSelection.beg.obj;
            
            // Get the table cell that contains the paragraph.
            var cell = pgf.InTextObj;
            
            // Get the row that contains the cell.
            var row = cell.CellRow;
            
            // Do something with the row here:
            // ...
            
            • 3. Re: Scripting tips
              snipppyBishop

              Thank you for the helpful suggestions, Rick. I'm new to scripting, as you know, so helpful tips are very appreciated!