2 Replies Latest reply on Nov 6, 2014 11:14 PM by LearnIndesignScripting

    How to add a  progress bar into a function which called itself recursively

    LearnIndesignScripting Level 1

      How to add a  progress bar into a function which called itself recursively

        • 1. Re: How to add a  progress bar into a function which called itself recursively
          AnshulJain19 Adobe Employee

          Hi,

          You can use ScriptUI to create progressbar in your script. Following is an example on how progress bar can be created:

           

          var progressRes = "dialog \

          {\

            text: 'Auto Collection',\

            preferredSize: [450,-1],\

            view1: Group\

            {\

            alignment: 'fill', orientation: 'column',\

            _label: StaticText{ alignment: 'left', text: 'Examining images...' },\

            _progress: Progressbar{ alignment: 'fill', properties: { name: 'progress' } },\

            _startAndCancel: Button{ text: 'Start',\

            started: false, cancelled: false,\

            properties: { name: 'cancel' }\

            },\

            }\

          }";

           

          var progressWindow = new Window( progressRes );

           

          progressWindow.progressBar = progressWindow.findElement( 'progress' );

          progressWindow.cancelButton = progressWindow.findElement( 'cancel' );

          progressWindow.cancelButton.onClick = function()

          {

            if (this.started)

            this.cancelled = true;

            else {

            this.started = true;

            this.text = "Cancel";

            this.window.startWork();

            }

          }

           

          // Start the 'work' when the start button is clicked

          progressWindow.startWork = function()

          {

            while (this.progressBar.value < this.progressBar.maxvalue &&

              ! this.cancelButton.cancelled)

            {

            this.progressBar.value = this.progressBar.value + 1;

            this.update();

            this.workFunction();

            }

            progressWindow.close( this.cancelButton.cancelled ? 1 : 2 );

          }

           

          progressWindow.workFunction = function()

          {

            $.sleep(500);

          }

           

          progressWindow.setMax = function( maxval )

          {

            progressWindow.progressBar.maxvalue = maxval;

            progressWindow.progressBar.value = 0;

          }

           

          progressWindow.setText = function( label )

          {

            progressWindow.children[0]['_label'].text = label;

          }

           

          //------------------------testing code

           

          function progressTest()

          {

            progressWindow.setMax(8);

            var result = progressWindow.show();

            // Do something with result...

          }

           

          progressTest();

           

          Thanks

          -Anshul

          • 2. Re: How to add a  progress bar into a function which called itself recursively
            LearnIndesignScripting Level 1

            That is correct can you please tell how we can manage in this function as I am new

              var myDoc = app.activeDocument; 

                                                                         var reportFilePath = myDoc.fullName.absoluteURI.replace(/\.indd$/i, ".txt");

                                                                    myTag(myDoc);                       

              function myTag(elm)

                                                                   {

                                                                try{

                                                                    for (var i = 0; i < elm.xmlElements.length; i++)

                                                                    {

                                                                        try{

                                                                            XMLelementName=elm.xmlElements[i].markupTag.name.toString();

                                                                            if(XMLelementName=="span")

                                                                            {

                                                                                if(elm.xmlElements[i].xmlAttributes.item("class").value == "name")

                                                                                {

                                                                                   

                                                                                    myName = elm.xmlElements[i].parent.contents.replace(elm.xmlElements[i].contents, "");

                                                                                    myName = myName.replace(" ", "");

                                                                                    var endIP = elm.xmlElements[i].insertionPoints.firstItem();

                                                                                    var endTF = endIP.parentTextFrames[0];

                                                                                    var endCitationPage = endTF.parentPage;

                                                                                    myFinalName = elm.xmlElements[i].contents +"," + myName;

                                                                                     WriteToFile(myFinalName+"  "+endCitationPage.name,reportFilePath);                                       

                                                                                }

                                                                                elm.xmlElements[0].select();

                                                                            }

                                                                        }catch(e){}

                                                                        myTag(elm.xmlElements[i]);

                                                                    }

                                                                }catch(e){}

                                                            }

                                                            function WriteToFile(text, reportFilePath) { 

                                                                file = new File(reportFilePath); 

                                                                file.encoding = "UTF-8"; 

                                                                if (file.exists) { 

                                                                    file.open("e"); 

                                                                    file.seek(0, 2); 

                                                                } 

                                                                else { 

                                                                    file.open("w"); 

                                                                } 

                                                                  file.writeln(text);  

                                                                file.close(); 

                                                            }