6 Replies Latest reply on May 21, 2016 11:29 PM by davidhmorgan

    Finally! A progress bar for AppleScripters!

    Lev Ger Level 1

      Progress bars are something that my scripts have lacked as I mainly program in AppleScript. I had resorted to using an outside app as a progress bar in my scripts...

      But no more!! I have borrowed Marc Autret's Javascript progress bar code from this post and made it into some handy handlers. I hope this helps other people too.

      Here they are in an example AppleScript.

      on myProgressBarCreate(pbBarName, firstAction, pbMax)
      
        set pbJS to "#targetengine 'pbar'
      var ProgressBar = function(/*str*/title)
      {
           var w = new Window('palette', ' '+title, {x:0, y:0, width:340, height:60}),
                pb = w.add('progressbar', {x:20, y:12, width:300, height:12}, 0, 100),
                st = w.add('statictext', {x:10, y:36, width:320, height:20}, '');
           st.justify = 'center';
           w.center();
           this.reset = function(msg,maxValue)
                {
                st.text = msg;
                pb.value = 0;
                pb.maxvalue = maxValue||0;
                pb.visible = !!maxValue;
                w.show();
                };
           this.hit = function() {++pb.value;};
           this.hide = function() {w.hide();};
           this.close = function() {w.close();};
      };
      
      
      pBar = new ProgressBar('" & pbBarName & "');
      pBar.reset('" & firstAction & "', " &a pbMax & ");"
        tell application "Adobe InDesign CS5"
        activate
        do script pbJS language javascript
        end tell
      end myProgressBarCreate
      
      
      on myProgressBarIncrement()
        set pbJS to "#targetengine 'pbar'
        pBar.hit()"
        tell application "Adobe InDesign CS5" to do script pbJS language javascript
      end myProgressBarIncrement
      
      
      on myProgressBarReset(newAction, newMax)
        set pbJS to "#targetengine 'pbar'
        pBar.reset('" & newAction & "', " & newMax & ");"
        tell application "Adobe InDesign CS5" to do script pbJS language javascript
      end myProgressBarReset
      
      
      on myProgressBarClose()
        set pbJS to "#targetengine 'pbar'
        pBar.close();"
        tell application "Adobe InDesign CS5" to do script pbJS language javascript
      end myProgressBarClose
      
      
      
      
      
      
      tell application "Adobe InDesign CS5"
        activate
        my myProgressBarCreate("Test", "PDFing...", 10)
      
        repeat 10 times
      
        my myProgressBarIncrement()
        delay 0.25
        end epeat
      
        my myProgressBarReset("Creating pages...", 5)
      
        repeat 5 times
      
        my myProgressBarIncrement()
        delay 0.5
        end repeat
      
      
        my myProgressBarClose()
      
      end tell
      
        • 1. Re: Finally! A progress bar for AppleScripters!
          DBLjan Level 2

          I found two errors in there while compiling:

           

          26: &a pbMax &

          Change to: pBar.reset('" & firstAction & "', " & pbMax & ");" 

           

          67: end epeat

          Change to: end repeat

           

          :-)

          • 2. Re: Finally! A progress bar for AppleScripters!
            DBLjan Level 2

            I cant code this JS in AS, but I can mimic how to :-)

            So I added this statusText function, to let people know whats going on right now…

            Theres an addition in your main function line 19 – 23.

             

            on myProgressBarCreate(pbBarName, firstAction, pbMax)
                
                set pbJS to "#targetengine 'pbar'  
                var ProgressBar = function(/*str*/title)  
                {  
                     var w = new Window('palette', ' '+title, {x:0, y:0, width:340, height:60}),  
                          pb = w.add('progressbar', {x:20, y:12, width:300, height:12}, 0, 100),  
                          st = w.add('statictext', {x:10, y:36, width:320, height:20}, '');  
                     st.justify = 'center';  
                     w.center();  
                     this.reset = function(msg,maxValue)  
                          {  
                          st.text = msg;  
                          pb.value = 0;  
                          pb.maxvalue = maxValue||0;  
                          pb.visible = !!maxValue;  
                          w.show();  
                          };  
                 this.statustext = function(msg)  
                          {  
                          st.text = msg;  
                          w.show();  
                          };          
                     this.hit = function() {++pb.value;};  
                     this.hide = function() {w.hide();};  
                     this.close = function() {w.close();};  
                };  
                  
                  
                pBar = new ProgressBar('" & pbBarName & "');  
                pBar.reset('" & firstAction & "', " & pbMax & ");"
                tell application "Adobe InDesign CS5.5"
                    activate
                    do script pbJS language javascript
                end tell
            end myProgressBarCreate
            
            
            on myProgressBarStatustext(newText)
                set pbJS to "#targetengine 'pbar'  
                pBar.statustext('" & newText & "');"
                tell application "Adobe InDesign CS5.5" to do script pbJS language javascript
            end myProgressBarStatustext
            
            
            
            set Stuff to {"doing this…", "analyzing that…", "compiling this…", "waiting for that…", "processing, pls wait…"}
            
            tell application "Adobe InDesign CS5.5"
                activate
                my myProgressBarCreate("MyProgress", "…", 10)
                
                repeat 10 times
                    my myProgressBarIncrement()
                    delay 0.5
                    my myProgressBarStatustext(item (random number from 1 to length of Stuff) of Stuff)
                end repeat
                
                my myProgressBarClose()
            end tell
            
            • 3. Re: Finally! A progress bar for AppleScripters!
              Kevin Parrott Level 2

              I like the look of this, so I thought id try it out using Adobe InDesign CC 2014, compiles ok

              but when I run it, it throws an error saying <<script>> doesn't understand the "myProgressBarIncrement" message.

              any ideas?

              • 4. Re: Finally! A progress bar for AppleScripters!
                DBLjan Level 2

                Thats because I only posted the function I changed.

                Ill hook you up with the whole AS:

                 

                
                set MyArray to {"Afghanistan", "Ägypten", "Albanien", "Algerien", "Antigua", "Argentinien", "Armenien", "Aserbaidschan", "Äthiopien", "Australien", "Bahamas", "Bangladesch", "Barbados", "Belgien", "Bolivien", "Bosnien-Herzegowina", "Botswana", "Brasilien", "Bulgarien", "Burkina Faso", "Chile", "China", "Costa Rica", "Dänemark", "Deutschland", "Dominikanische Republik", "Ecuador", "Elfenbeinküste", "Estland", "Finnland", "Frankreich", "Georgien", "Ghana", "Griechenland", "Guatemala", "Guinea", "Haiti", "Honduras", "Indien", "Indonesien", "Irak", "Iran", "Irland", "Island", "Israel", "Italien", "Jamaika", "Japan", "Jemen", "Jordanien", "Kambodscha", "Kamerun", "Kanada", "Kasachstan", "Katar", "Kenia", "Kolumbien", "Kongo", "Korea", "Kosovo", "Kroatien", "Kuba", "Kuwait", "Laos", "Lettland", "Libanon", "Libyen", "Liechtenstein", "Litauen", "Luxemburg", "Madagaskar", "Malawi", "Malaysia", "Mali", "Malta", "Marokko", "Mauritius", "Mazedonien", "Mexiko", "Monaco", "Mongolei", "Montenegro", "Mosambik", "Namibia", "Nepal", "Neuseeland", "Nicaragua", "Niederlande", "Nigeria", "Norwegen", "Österreich", "Pakistan", "Papua-Neuguinea", "Paraguay", "Peru", "Philippinen", "Polen", "Portugal", "Rumänien", "Russland", "Samoa", "Saudi-Arabien", "Schweden", "Schweiz", "Senegal", "Serbien", "Simbabwe", "Singapur", "Slowakei", "Slowenien", "Somalia", "Spanien", "Sri Lanka", "Südafrika", "Sudan", "Syrien", "Tansania", "Thailand", "Togo", "Trinidad", "Tschad", "Tschechien", "Tunesien", "Türkei", "Turkmenistan", "Ukraine", "Ungarn", "Uruguay", "Usbekistan", "Venezuela", "Vereinigte Arabische Emirate", "Vereinigte Staaten von Amerika", "Vietnam", "Zypern"}
                
                set start_percent to "000%"
                
                tell application "Adobe InDesign CS5.5"
                    activate
                    
                    my myProgressBarCreate("Countrys I Like <3", "…", length of MyArray)
                    
                    repeat with k from 1 to length of MyArray
                        
                        set MyPercent to round ((((100 / (length of MyArray))) * k)) as real
                        
                        my myProgressBarIncrement(MyPercent & "%")
                        delay 0.08
                        my myProgressBarStatustext("(" & (((length of MyArray) + 1) - k) & ") – " & item k of MyArray)
                    end repeat
                    
                    my myProgressBarClose()
                end tell
                
                
                
                on myProgressBarCreate(pbBarName, firstAction, pbMax)
                    
                    set pbJS to "#targetengine 'pbar'  
                    var ProgressBar = function(/*str*/title)  
                    {  
                         var w = new Window('palette', ' '+title, {x:0, y:0, width:360, height:60}),  
                              pb = w.add('progressbar', {x:10, y:12, width:300, height:12}, 0, 100),
                        pt = w.add('statictext', {x:320, y:10, width:40, height:20}, ''),
                              st = w.add('statictext', {x:10, y:36, width:360, height:20}, '');  
                         st.justify = 'left';         w.center();  
                         this.reset = function(msg,maxValue)  
                              {  
                              st.text = msg;  
                              pb.value = 0;  
                              pb.maxvalue = maxValue||0;  
                              pb.visible = !!maxValue;  
                              w.show();  
                              };  
                         this.statustext = function(msg)  
                              {  
                              st.text = msg;  
                              w.show();  
                              };
                        this.prozent = function(msg)  
                              {  
                              pt.text = msg;  
                              w.show();  
                              };                    
                          
                         this.hit = function() {++pb.value;};  
                         this.hide = function() {w.hide();};  
                         this.close = function() {w.close();};  
                    };  
                      
                      
                    pBar = new ProgressBar('" & pbBarName & "');  
                    pBar.reset('" & firstAction & "', " & pbMax & ");"
                    tell application "Adobe InDesign CS5.5"
                        activate
                        do script pbJS language javascript
                    end tell
                end myProgressBarCreate
                
                
                on myProgressBarStatustext(newText)
                    set pbJS to "#targetengine 'pbar'  
                    pBar.statustext('" & newText & "');"
                    tell application "Adobe InDesign CS5.5" to do script pbJS language javascript
                end myProgressBarStatustext
                
                
                on myProgressBarIncrement(prozent)
                    set pbJS to "#targetengine 'pbar'  
                      pBar.hit()"
                    tell application "Adobe InDesign CS5.5" to do script pbJS language javascript
                    set pbJS to "#targetengine 'pbar'  
                    pBar.prozent('" & prozent & "');"
                    tell application "Adobe InDesign CS5.5" to do script pbJS language javascript
                end myProgressBarIncrement
                
                
                on myProgressBarReset(newAction, newMax)
                    set pbJS to "#targetengine 'pbar'  
                      pBar.reset('" & newAction & "', " & newMax & ");"
                    tell application "Adobe InDesign CS5.5" to do script pbJS language javascript
                end myProgressBarReset
                
                
                on myProgressBarClose()
                    set pbJS to "#targetengine 'pbar'  
                      pBar.close();"
                    tell application "Adobe InDesign CS5.5" to do script pbJS language javascript
                end myProgressBarClose
                
                
                
                
                
                
                • 5. Re: Finally! A progress bar for AppleScripters!
                  Kevin Parrott Level 2

                  this is pretty slick, cheers for the code

                  • 6. Re: Finally! A progress bar for AppleScripters!
                    davidhmorgan Level 1

                    Thank you to Lev Ger and DBLjan for posting this, it's very useful! Two questions DBLjan:

                    1. The function myProgressBarReset doesn't get called in your script - what does it do?

                    2. When the script is running and the progress bar is showing, there is a red cancel button but clicking on it doesn't do anything. How could I get this to activate?