[ID CC-CC2021] Найти далее и найти назад

rezunoff

Участник
Топикстартер
Сообщения
69
Реакции
13
Подскажите, почему не работает поиск назад. Результатом поиска является последний -BLA-, и почему при повторном запуске ничего не происходит? Может, есть альтернативные варианты?
Источник: find next and find previous with indesign javascript
Код:
var main = function() {
    var doc = app.properties.activeDocument,
    selText, st, ftp = app.findTextPreferences.properties, found, n, currIndex, foundIndex, nextText;
    if ( !doc ) return;

    if (
        app.selection.length!=1
        || typeof ( app.selection[0].properties.baselineShift)=="undefined"
        || (app.selection[0] instanceof TextFrame )
    ) {
        alert("Please select text then run the script again" );
        return;
    }

    app.findTextPreferences = null;


    selText = app.selection[0];
    st = selText.parentStory;

    app.findTextPreferences.properties = {
        findWhat : "BLA"
    };

    found = st.findText();
    n = found.length;

    if ( !n ) {
        alert("No results found, sorry." );
        return;
    }

    currIndex = app.selection[0].index;
   
    while ( n-- ) {
        foundIndex = found[n].index;
       
        if ( !nextText && foundIndex<currIndex ) {
            nextText = found[n];
        }
        else if (foundIndex<currIndex && foundIndex>nextText.index)  {
             nextText = found[n];
        }
        else {
            break;
        }
    }

    app.findTextPreferences.properties = ftp;

    if ( !nextText ) {
        alert("No further results, sorry." );
        return;
    }

    if ( nextText.parentTextFrames.length && nextText.parentTextFrames[0].parentPage ) {

        var c1 = nextText.characters[0].words[0].characters[0],
        cn = nextText.characters[-1].words[0].characters[-1];

        var data =
        {
            geometricBounds:[c1.baseline, c1.horizontalOffset/1.25, c1.baseline-c1.pointSize, cn.endHorizontalOffset*1.25],
        };
        var rect = nextText.parentTextFrames[0].parentPage.rectangles.add(data);
   //     zoomObject ( rect );
        rect.remove();
    }
    app.select( nextText );
}

//Snippet designed by Dave Saunders at http://jsid.blogspot.fr/2006/01/zoom-in-on-object.html
function zoomObject(theObj) {
try {
  var objBounds = theObj.geometricBounds;
} catch (e) {
  throw "Object doesn't have bounds."
}
var ObjHeight = objBounds[2] - objBounds[0];
var ObjWidth = objBounds[3] - objBounds[1];
var myWindow = app.activeWindow;
var pageBounds = myWindow.activePage.bounds;
var PgeHeight = pageBounds[2] - pageBounds[0];
var PgeWidth = pageBounds[3] - pageBounds[1];
var hRatio = PgeHeight/ObjHeight;
var wRatio = PgeWidth/ObjWidth;
var zoomRatio = Math.min(hRatio, wRatio);
app.select(theObj); // to make active the page that holds theObj
myWindow.zoom(ZoomOptions.fitPage);
myWindow.zoomPercentage = myWindow.zoomPercentage * zoomRatio;
//exit() // Because there's no point in doing this if you don't exit to let the user see
}

var u;

app.doScript ( "main()",u,u,UndoModes.ENTIRE_SCRIPT, "The Script" );
 
Последнее редактирование:

rezunoff

Участник
Топикстартер
Сообщения
69
Реакции
13
Цель кода, при повторном нажатии искать следующий или предыдущий (указанный) фрагмент текста. Желательно через - findGrep.
Вот сокращенный вариант выше представленного кода, который ищет каждую следующую цифру 5. Как сделать обратный ход (искать каждую предыдущую цифру)?
Код:
var main = function() {
    var doc = app.properties.activeDocument,
    selText, st, ftp = app.findGrepPreferences.properties, found, n, currIndex, foundIndex, nextText;
    if ( !doc ) return;
 
app.findGrepPreferences = null;

    selText = app.selection[0];
    st = selText.parentStory;
    
app.findGrepPreferences.properties = ({findWhat:"5"});
    found = st.findGrep();
    n = found.length;

currIndex = app.selection[0].index;
  
    while ( n-- ) {
        foundIndex = found[n].index;
 if ( !nextText && foundIndex>currIndex ) {
     nextText = found[n];
        }
    
else if (foundIndex>currIndex && foundIndex<nextText.index)  {
       nextText = found[n];
        }
        else {
            break;
        }
    }
    app.findGrepPreferences.properties = ftp;
    if ( !nextText ) {
        alert("No further results, sorry." );
        return;
    }
    app.select( nextText );
}
var u;
app.findTextPreferences = app.changeTextPreferences = NothingEnum.NOTHING;
app.doScript ( "main()",u,u,UndoModes.ENTIRE_SCRIPT, "The Script" );
app.findGrepPreferences = null;
 

Skvoznyak

15 лет на форуме
Сообщения
5 500
Реакции
2 168
ну этот скрипт нерабочий, 4 ошибки поправил, он все равно ничего не делает
 

Skvoznyak

15 лет на форуме
Сообщения
5 500
Реакции
2 168
скопировал не целиком видимо
 

Skvoznyak

15 лет на форуме
Сообщения
5 500
Реакции
2 168
Код:
var main = function() {
    var doc = app.properties.activeDocument,
    selText, st, ftp = app.findGrepPreferences.properties, found, n, currIndex, foundIndex, nextText;
    if ( !doc ) return;
 
app.findGrepPreferences = null;

    selText = app.selection[0];
    st = selText.parentStory;
    
app.findGrepPreferences.properties = ({findWhat:"5"});
    found = st.findGrep();
    n = found.length;

currIndex = app.selection[0].index;
 
    while ( n-- ) {
        foundIndex = found[n].index;
 if (foundIndex<currIndex ) {
     nextText = found[n];
     break;
        }
    
    }
    app.findGrepPreferences.properties = ftp;
    if ( !nextText ) {
        alert("No further results, sorry." );
        return;
    }
    app.select( nextText );
}
var u;
app.findTextPreferences = app.changeTextPreferences = NothingEnum.NOTHING;
app.doScript ( "main()",u,u,UndoModes.ENTIRE_SCRIPT, "The Script" );
app.findGrepPreferences = null;
 
  • Спасибо
Реакции: rezunoff

rezunoff

Участник
Топикстартер
Сообщения
69
Реакции
13
Код:
var main = function() {
    var doc = app.properties.activeDocument,
    selText, st, ftp = app.findGrepPreferences.properties, found, n, currIndex, foundIndex, nextText;
    if ( !doc ) return;

app.findGrepPreferences = null;

    selText = app.selection[0];
    st = selText.parentStory;
   
app.findGrepPreferences.properties = ({findWhat:"5"});
    found = st.findGrep();
    n = found.length;

currIndex = app.selection[0].index;

    while ( n-- ) {
        foundIndex = found[n].index;
if (foundIndex<currIndex ) {
     nextText = found[n];
     break;
        }
   
    }
    app.findGrepPreferences.properties = ftp;
    if ( !nextText ) {
        alert("No further results, sorry." );
        return;
    }
    app.select( nextText );
}
var u;
app.findTextPreferences = app.changeTextPreferences = NothingEnum.NOTHING;
app.doScript ( "main()",u,u,UndoModes.ENTIRE_SCRIPT, "The Script" );
app.findGrepPreferences = null;
Ок! Спасибо!
 

Oleg Butrin

20 лет на форуме
Сообщения
1 989
Реакции
1 213
Объясните, зачем нужно запускать именно этот скрипт через doScript? Документ не изменяется, выигрыша в скорости не будет.
 

rezunoff

Участник
Топикстартер
Сообщения
69
Реакции
13
Может у кого есть альтернативные варианты?
 

Skvoznyak

15 лет на форуме
Сообщения
5 500
Реакции
2 168
Объясните, зачем нужно запускать именно этот скрипт через doScript? Документ не изменяется, выигрыша в скорости не будет.

и grep не нужен, и поиск по всей story излишен. 'hz' да и скрипт не нужен, opt-cmd-F достаточно