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

sel.duplicate problem

Explorer ,
Jun 03, 2018 Jun 03, 2018

Copy link to clipboard

Copied

I'm trying to copy blocks of text from one file to a series of new files - each block in it's own file,

The following code works -- USUALLY. occasionally the text in the new file is truncated in the middle (with no pattern as to when it is truncated.

the alert shows the complete text block so I know the sel is correct

any ideas as to what could be causing the problem?

thanks

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

    app.findGrepPreferences.findWhat=" newblock";

    found=app.activeDocument.findGrep();   

   

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

        ipStart=found.index;

        ipEnd=found[i+1].index-1;

        sel = found[0].parentStory.characters.itemByRange(ipStart,ipEnd);

       //alert(i+"\r"+sel.contents);

      

        nd=makeNewDoc();// creates new doc - sets margins

        ntf=nd.pages[0].textFrames.add();

        gb=ntf.geometricBounds;

        gb[2]=gb[3]=200;

        gb[0]=gb[1]=0;

        ntf.geometricBounds=gb;

       

       sel.duplicate(LocationOptions.AT_BEGINNING,ntf);    

       }

TOPICS
Scripting

Views

579

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

Advocate , Jun 04, 2018 Jun 04, 2018

As per your need I can understand, I think you should try this

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

app.findGrepPreferences.findWhat="newblock"; 

found=app.activeDocument.findGrep(); 

var ipStart = null;

var ipEnd = null;

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

    if(i==0){

        ipStart = found.characters[0].index;

        }

    else{

        if(i < found.length-1){

            ipEnd=found.characters[0].index-1;

            }

        else if(i < found.length){

         

...

Votes

Translate

Translate
Advocate ,
Jun 03, 2018 Jun 03, 2018

Copy link to clipboard

Copied

Hi there,

          If you are more concerned about text only, then instead of grep you should try text search, so your code should be like this,

app.findTextPreferences = app.changeTextPreferences = NothingEnum.nothing;

app.findTextPreferences.findWhat=" newblock";

found=app.activeDocument.findText();

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

    ipStart=found.index;

    ipEnd=found[i+1].index-1;

    sel = found[0].parentStory.characters.itemByRange(ipStart,ipEnd);

    nd=makeNewDoc();

    ntf=nd.pages[0].textFrames.add();

    gb=ntf.geometricBounds;

    gb[2]=gb[3]=200;

    gb[0]=gb[1]=0;

    ntf.geometricBounds=gb;

    sel.duplicate(LocationOptions.AT_BEGINNING,ntf);

    }

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 ,
Jun 04, 2018 Jun 04, 2018

Copy link to clipboard

Copied

Hi


I need to use grep -- "new block" is preceded by numbers that have to be included. I left that out because it doesn't affect the script example

Thanks

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 ,
Jun 04, 2018 Jun 04, 2018

Copy link to clipboard

Copied

Follow up experimenting: if I make the textFrame huge (so the entire text block fits into the frame) then it seems to work properly...

Which doesn't make any sense (as far as I can see)

Any ideas why?

thanks

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
Advocate ,
Jun 04, 2018 Jun 04, 2018

Copy link to clipboard

Copied

As per your need I can understand, I think you should try this

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

app.findGrepPreferences.findWhat="newblock"; 

found=app.activeDocument.findGrep(); 

var ipStart = null;

var ipEnd = null;

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

    if(i==0){

        ipStart = found.characters[0].index;

        }

    else{

        if(i < found.length-1){

            ipEnd=found.characters[0].index-1;

            }

        else if(i < found.length){

            //******you can choose here if you want to add character after last newblock to the end of your story or not******//

            ipEnd=found.parent.characters[found.parent.characters.length-1];

            }

        sel = found[0].parentStory.characters.itemByRange(ipStart,ipEnd); 

        var nd = app.documents.add();

        ntf=nd.pages[0].textFrames.add();

        gb=ntf.geometricBounds;

        gb[2]=gb[3]=200;

        gb[0]=gb[1]=0;

        ntf.geometricBounds=gb;

        sel.duplicate(LocationOptions.AT_BEGINNING,ntf);

        if(i < found.length-1){

            ipStart = found.characters[0].index;

            }

        }

    }

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
Guide ,
Jun 04, 2018 Jun 04, 2018

Copy link to clipboard

Copied

LATEST

Hi,

What if you duplicate the text into ntf.parentStory (instead of ntf)?

@+

Marc

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