1

我有一个运行良好的脚本,但可能需要很长时间才能完成。这是一个 javascript 文档并运行多个函数。

该脚本在 InDesign 中运行,并且基于 javascript。ExtendScript Toolkit 提供了几个演示脚本,其中之一是SnpCreateProgressBar.jsx.

现在我想将进度条添加到我当前的脚本中,但我不完全确定如何执行此操作。

在不在这里发布整个代码的情况下,我将执行一个函数。这在整个脚本中重复。

var myDoc = app.activeDocument;
var myTables = myDoc.stories.everyItem().tables.everyItem();
var myTableElements = myTables.getElements();

function basicSearchReplace(findWhat, changeTo){
    app.findGrepPreferences = app.changeGrepPreferences = null;
    app.findChangeGrepOptions = NothingEnum.nothing;
    app.findGrepPreferences.findWhat = findWhat;
    app.changeGrepPreferences.changeTo = changeTo;
    myDoc.changeGrep();
}    


basicSearchReplace ('^~8 ', '~8\\t');
basicSearchReplace ('^·( |\\t)',  '~8\\t');

SnpCreateProgressBar.jsx可以在这里找到

也许还有另一种扩展脚本方法来创建进度条?

--- 小更新 ---
所以经过一些谷歌搜索和测试后,我想出了:

function SnpCreateProgressBar () 
{
    this.windowRef = null;
}

SnpCreateProgressBar.prototype.run = function() {
var win = new Window("palette", "SnpCreateProgressBar", [150, 150, 600, 260]);   
win.pnl = win.add("panel", [10, 10, 440, 100], "Script Progress");  
win.pnl.progBar = win.pnl.add("progressbar", [20, 35, 410, 60], 0, 100);  
win.pnl.progBarLabel = win.pnl.add("statictext", [20, 20, 320, 35], "0%");  

win.show();  

                while(win.pnl.progBar.value < win.pnl.progBar.maxvalue)  
                {  

                          // this is what causes the progress bar increase its progress  
                          win.pnl.progBar.value++;   
                            win.pnl.progBarLabel.text = win.pnl.progBar.value+"%";  
                          $.sleep(10);  
                }  

alert('Done!');   

win.close();  
};
if(typeof(SnpCreateProgressBar_unitTest) == "undefined") {
    new SnpCreateProgressBar().run();
}

现在我想我还没有完全理解。如果我将我的“待运行”代码放在 while 循环中,它只会执行该代码并在最后运行进程栏......底部的 if 循环也是如此

我不明白什么?

4

1 回答 1

1

请参阅此 ProgressBar 实现。我一直在使用它: https ://github.com/indiscripts/extendscript/blob/master/scriptui/ProgressBar.jsx

/*******************************************************************************

        Name:           ProgressBar
        Desc:           Simple progress bar.
        Path:           /inc/progress.lib.jsxinc
        Require:        ---
        Encoding:       ÛȚF8
        Kind:           Constructor
        API:            show()=reset() msg() hit() hitValue() hide() close()
        DOM-access:     NO (ScriptUI)
        Todo:           Does not work in Mac El Capitan
        Created:        141112 (YYMMDD)
        Modified:       160228 (YYMMDD)

*******************************************************************************/

$.hasOwnProperty('ProgressBar')||(function(H/*OST*/,S/*ELF*/,I/*NNER*/)
{
    H[S] = function ProgressBar(/*str*/title,/*uint*/width,/*uint*/height)
    {
        (60<=(width||0))||(width=340);  
        (40<=(height||0))||(height=60);  
      
        var H = 22,  
            Y = (3*height-2*H)>>2,  
            W = new Window('palette', ' '+title, [0,0,width,height]),  
            P = W.add('progressbar', { x:20, y:height>>2, width:width-40, height:12 }, 0,100),  
            T = W.add('statictext' , { x:0, y:Y, width:width, height:H }),  
            __ = function(a,b){ return localize.apply(null,a.concat(b)) };  
      
        this.pattern = ['%1'];  
      
        W.center();  
      
        // ---  
        // API  
        // ---  
         
        this.msg = function(/*str*/s,  v)  
        // ---------------------------------  
        {  
            s && (T.location = [(width-T.graphics.measureString(s)[0])>>1, Y]);  
            
            T.text = s;
            W.update();
        };  
      
        this.show = this.reset = function(/*str*/s, /*uint*/v)  
        // ---------------------------------  
        {  
            if( s && s != localize(s,1,2,3,4,5,6,7,8,9) )  
                {  
                this.pattern[0] = s;  
                s = __(this.pattern, [].slice.call(arguments,2));  
                }  
            else  
                {  
                this.pattern[0] = '%1';  
                }  
             
            P.value = 0;  
            P.maxvalue = v||0;  
            P.visible = !!v;  
      
            this.msg(s);  
            
            W.show();
            W.update();
        };  
      
        this.hit = function(x)  
        // ---------------------------------  
        {  
            ++P.value;  
            ('undefined' != typeof x) && this.msg(__(this.pattern, [].slice.call(arguments,0)));
            W.update();
        };

        this.hitValue = function(v,x)
        // ---------------------------------  
        {  
            P.value = v;
            ('undefined' != typeof x) && this.msg(__(this.pattern, [].slice.call(arguments,1)));
            W.update();
        };

        this.hide = function()  
        // ---------------------------------  
        {  
            W.hide();  
        };  
         
        this.close = function()  
        // ---------------------------------  
        {  
            W.close();  
        };  
    };

})($,{toString:function(){return 'ProgressBar'}},{});

示例代码:

//------------------------------------------------  
//      SAMPLE CODE  
//------------------------------------------------  
  
(function()  
{  
    var PB = new $.ProgressBar("Script Title",350,100),  
        i, vMax;  
  
    // Quick process  
    // ---  
    PB.show("Processing quickly... %1 / 500", vMax=500, i=0);  
    for( ; i < vMax ; $.sleep(8), PB.hit(++i) );  
     
    PB.reset("Wait for 800 ms...");  
    $.sleep(800);  
  
    // Slow process  
    // ---  
    PB.reset("And now slowly (%1%)  |  Stage %2", vMax=13, 0, i=0);  
    for( ; i < vMax ; ++i, PB.hit((100*(i/vMax)).toFixed(2), i), $.sleep(400+200*(.5-Math.random())) );  
  
    $.sleep(500);  
    PB.msg("The job is done.");  
    $.sleep(1500);  
  
    // Quit  
    // ---  
    PB.close();  
})();
于 2018-04-19T20:07:47.833 回答