• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

How to write data at the end of doc in reverse order?[CS6-jsx]

Explorer ,
May 15, 2013 May 15, 2013

Copy link to clipboard

Copied

I need to move all my spec texts entries at et end of doc. To keep correct reference in all found entries i have to process in reverse order

for (i = mFound.length-1; i>=0; i--){

...etc

but can't to think up, how to place entries in write order at the end.

String:

mStory.insertionPoints.item(-1).select();

app.paste();

does not work properly (of course). It makes this:

10.

9.

8.

7.

6.

...etc

How to get

1.

2.

3.

4.

...etc

?

TOPICS
Scripting

Views

2.0K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , May 17, 2013 May 17, 2013

Dmitry,

Well, I was basically dealing with moving the notes to the end of the story in the right order. But anyway, as to the superscripted numbers, there's no need to select things and remove those selections. It considered safer, as Vamitul too mentioned, to work on objects. To handle the superscripted numbers, I would do this:

app.findGrepPreferences = null;

// Search just the note contents: lookaround leaves the braces in the text

app.findGrepPreferences.findWhat = /(?<=\{\{\{).+?(?=\}\}\})/.sou

...

Votes

Translate

Translate
Contributor ,
May 15, 2013 May 15, 2013

Copy link to clipboard

Copied

Hi,

I think what you are doing is the code below.

you should fix using

findGrep(true) => true is 'reverse order' option

and move()

var dummy = "Um veles 1.eatiorum et alic tem 2.quibusda nit volest, to corion conserum rem ipsanitate verores sequiduciat la demquis 3.inctorem ellestem reptis a et offic tem quidel earcide bissere mpellorerit ipsaecearios 4.volorrovid earchit omnitat atquatus aut quam nimet idelluptatum quis aut lam quo 5.ipsapiciis se simus autem fugias esto et et volupti to eum rem reius nobitatur aut evelis rae arumque lanihilicia sapitiam volorep ellupta volupta aut qui rendia doluptusam am necatur? Em aut abo. "

var doc = app.documents.add(); var tf = doc.textFrames.add({geometricBounds: ["10mm","10mm","100mm", "80mm"], contents:dummy}); var st = tf.parentStory; var find_grep_obj = {   findWhat : "\\d+\\.[^\\s]+" }; with(app.findChangeGrepOptions){   includeFootnotes            = false;   includeHiddenLayers         = false;   includeLockedLayersForFind  = false;   includeLockedStoriesForFind = false;   includeMasterPages          = false;   kanaSensitive               = true;   widthSensitive              = true; } var match = grep_find(doc, find_grep_obj); function grep_find(target_obj, find_grep_obj){   app.findGrepPreferences = NothingEnum.nothing;   app.findGrepPreferences.properties = find_grep_obj;   var result = target_obj.findGrep(true); //=> reverse order [..., "3.inctorem","2.quibusda", "1.eatiorum"]   app.findGrepPreferences = NothingEnum.nothing;   return result; } var i = match.length; while (i--) {   st.insertionPoints[-1].contents = "\r";   match.move(LocationOptions.AT_END, st); }

thank you.

mg

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
May 16, 2013 May 16, 2013

Copy link to clipboard

Copied

Yes, param True is good. But unforunately, i need to move entries with numbering its. So numbers appear in reverse order. The mision: to create endnotes list of specially marked texts fragments. I can't use "move" method, because i need to set a number of entrie in place of entrie. So i use "cut-paste" method. And if use a TRUE-key in FindGrep then i got a reverse numbering in text. So far i think to make reverse list, and then in Word make ascending sorting and paste back in Indi-doc.

Thanks for hint, anyway!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advisor ,
May 16, 2013 May 16, 2013

Copy link to clipboard

Copied

i think this will interest you:

http://jongware.mit.edu/idcs5/pc_Array.html#reverse

long story short after the find grep or whatever

myFoundArray.reverse();

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
May 17, 2013 May 17, 2013

Copy link to clipboard

Copied

Yes, .reverse() method helped:

var mStory = app.selection[0].insertionPoints[0].parentStory;

var aEndList = []; // prepare array

mFind ="\\{\\{\\{.+?\\}\\}\\}"; // my coding of text being searched (kinda {{{some needed text}}})

   app.findGrepPreferences = app.changeGrepPreferences = NothingEnum.nothing;

    app.findChangeGrepOptions.includeFootnotes = false;

    app.findGrepPreferences.findWhat = mFind;

var mFound = mStory.findGrep();

$.writeln(mFound.length);

for (i = mFound.length-1; i>=0; i--){

mSel = mFound;

mStr_tmp  = String(i+1) + ". " + mFound.contents; //temporary string with added number

mSel.select();

app.selection[0].remove();

//place number of refer instead of refer itself in text

mSel.insertionPoints[0].texts[0].position = Position.SUPERSCRIPT;

mSel.insertionPoints[0].texts[0].contents = String(i+1);

aEndList.push(mStr_tmp);

}

aEndList.reverse();

myString = aEndList.join ("\r");

//place resorted and numbered list

mStory.insertionPoints.item(-1).select();

mStory.insertionPoints.item(-1).contents = myString;

But the problem arose. Operating with contents looses text formatting. So i used cut-paste method. can i keep formattin by some way in this code?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advisor ,
May 17, 2013 May 17, 2013

Copy link to clipboard

Copied

not sure i understand what you are trying to achieve, but i am quite sure you are overcomplicating things. From your code in guess you are trying to do some kind of endnote system? if so, here is a bit of pseudocode it might help you:

//set grep find options as desired

var myFound =myStory.findGrep();

var l=myFound.length;

while (l--) //simple iterate backwards trick

{

// add the numbering, without changing anything else:

myFound.insertionPoints[0].position=Position.SUPERSCRIPT;

myFound.insertionPoints[0].contents=l+1; // javascript does the automatic conversion, so String(l+1) is redundant, but nice

}

// get the last insertion point of the story:

var myLastIP=myStory.insertionPoints[-1];

//search  again

var myFound =myStory.findGrep();

var l=myFound.length;

while (l--) {

//move the found stuff AFTER the last insertion point

myFound.move(LocationOptions.AFTER,myLastIP);

}

a side note: my "pseudocode" looks suspicious like javascript, however i did not test it, provide no guarantees, take no responsability whatsoever about it.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 17, 2013 May 17, 2013

Copy link to clipboard

Copied

@Vamitul – basically using the texts object retaining formatting is a good concept!

However:

1. I got an error from the ESTK, because the automatic conversion from number to string cannot take place.

l+1 remains a Number object (ESTK 3.8.0.12 testet with InDesign CS5.5)

So "contents=l+1" should be:  "contents=(l+1).toString()" or: "contents=String(l+1)".


2. In the while loop at the end you are concatenating every found texts in one paragraph (unless the found text has a paragraph sign).


3. And the while loop concatenates the found results in the wrong order (exactly reverse from what is needed).

Instead I would do the following:

1. Iterate the found texts not in reverse!
2. Duplicating the found texts one after another to the end of the story  adding consecutive numbers and things (paragraph signs at the end)
3. Changing the found texts in reverse order to superscript numbers


Uwe

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 17, 2013 May 17, 2013

Copy link to clipboard

Copied

The result of findGrep()/Text() is an array, not a collection, so you have more leeway in moving the objects around. All you need is this:

app.findGrepPreferences = null;

app.findGrepPreferences.findWhat = /\{\{\{.+?\}\}\}/.source;

found = app.documents[0].findGrep();

for (i = 0; i < found.length; i++){

    found.parentStory.insertionPoints[-1].contents = '\r' + String (i+1) + '.\t';

    found.move (LocationOptions.after, found.parentStory.insertionPoints[-1]);

}

Peter

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
May 17, 2013 May 17, 2013

Copy link to clipboard

Copied

Peter, your code, unfortunately, do not place superscript numbers before moving entries.

I added another findGrep-cycle (to write associated numbers in text) and change move method for duplicate in first cycle (not showed):

app.findGrepPreferences = null;

app.findGrepPreferences.findWhat = /\{\{\{.+?\}\}\}/.source;

found = app.documents[0].findGrep();

if (found.length > 0){

    for (i = 0; i < found.length; i++){

        found.parentStory.insertionPoints[-1].contents = '\r' + String (i+1) + ". "; // пробел-em

        found.duplicate (LocationOptions.after, found.parentStory.insertionPoints[-1]);

    }

// next cycle

for (i = found.length-1; i>=0; i--){

mSel =found;

mSel.select();

app.selection[0].remove();

//ставим номер ссылки вместо самой ссылки

mSel.insertionPoints[0].texts[0].position = Position.SUPERSCRIPT;

mSel.insertionPoints[0].texts[0].contents = String(i+1);

    }

}

Thanks Peter and Laubender and Vamitul!

p.s. i guess, my code again not laconic))

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
May 17, 2013 May 17, 2013

Copy link to clipboard

Copied

And last question.

Can i remove {{{ and }}} without converting texts in string and loss of formatting?

not like this:

m1 = /\{\{\{/;

m2 = /\}\}\}/;

if(mStr_tmp.match(m1)) {mStr_new = mStr_tmp.replace(m1,"")}

if(mStr_new.match(m2)){mStr_new = mStr_new.replace(m2,"")}

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 17, 2013 May 17, 2013

Copy link to clipboard

Copied

Dmitry,

Well, I was basically dealing with moving the notes to the end of the story in the right order. But anyway, as to the superscripted numbers, there's no need to select things and remove those selections. It considered safer, as Vamitul too mentioned, to work on objects. To handle the superscripted numbers, I would do this:

app.findGrepPreferences = null;

// Search just the note contents: lookaround leaves the braces in the text

app.findGrepPreferences.findWhat = /(?<=\{\{\{).+?(?=\}\}\})/.source;

found = app.documents[0].findGrep();

for (i = 0; i < found.length; i++){

    found.parentStory.insertionPoints[-1].contents = '\r' + String (i+1) + '.\u2003';

    found.move (LocationOptions.after, found.parentStory.insertionPoints[-1]);

}

// Now search {{{}}}

app.findGrepPreferences.findWhat = /\{\{\{\}\}\}/.source;

found = app.documents[0].findGrep();

for (i = found.length-1; i >= 0; i--){

    found.contents = String (i+1);

    found.position = Position.SUPERSCRIPT;

}

So first move the notes to the end of the story, leaving behind all the triplets of braces. Then, after moving the notes, you do a new search on the sextets of braces. Don't select and remove, simply redefine the contents of what you found and apply any formatting.

Peter

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advisor ,
May 17, 2013 May 17, 2013

Copy link to clipboard

Copied

Peter.. the Regex to source.. BRILIANT.

Wish i would of thoght of that before i wrote this abomination:

String.prototype.escapeR = function() {

  return this.replace(/[\-\[\]{}()*+?.,\\\^$|#\s]/g, "\\$&");

}

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 17, 2013 May 17, 2013

Copy link to clipboard

Copied

That /.../.source trick I got from J. Hawkinson a while ago in this forum. Maybe before your time

P.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
May 17, 2013 May 17, 2013

Copy link to clipboard

Copied

Eurika! This trick with lookaround is magic...

I worked/debugged with my original script for three days (until headache). And didn't advanced too far, anyway.

But solution appeared mach simpler (though not so obviously)

Thanks!

p.s. by the way, in second cycling no need to use reverse counting (i--). I tried the straight count and all do work correctly.

//~ for (i = found.length-1; i >= 0; i--){

    for (i=0; i< found.length; i++){

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 17, 2013 May 17, 2013

Copy link to clipboard

Copied

LATEST

If all your notes are between 1 and 9, OR between 10 and 99 -- then all works well. But if your text contains single- and double-digit note references (e.g. 1 to 40), then you must use 'reverse counting'. Check your document to convince yourself.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines