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);
}
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){
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);
}
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
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
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;
}
}
}
Copy link to clipboard
Copied
Hi,
What if you duplicate the text into ntf.parentStory (instead of ntf)?
@+
Marc