Skip to main content
Inspiring
July 27, 2014
Answered

Can you crrect my script?

  • July 27, 2014
  • 3 replies
  • 891 views

Hi every one

I got this script:

//-----------------------------------------------------------------------------------------------------------

var book = app.activeBook, 
    _content = []; 
    overlap = []; 
for(var i =book.bookContents.length-1;i>=0;i--) 

        _content.push(book.bookContents.name + "\t" + (book.bookContents.documentPageRange.replace(/-[^-]+$/g,"")) + "\r"); 
        if(book.bookContents.documentPageRange == book.bookContents[i-1].documentPageRange) 
        { 
                overlap.push(book.bookContents.name + "\t" +book.bookContents.documentPageRange + "\r" + book.bookContents[i-1].name + "\t" +book.bookContents[i-1].documentStartPage); 
            } 
    } 
alert(overlap.reverse())

//alert(_content);
    WriteToFile(_content, overlap);
function WriteToFile(text) {
    file = new File("~/Desktop/Book contents and page number.txt");
    file.encoding = "UTF-8";
    file.open("w");
    file.write(text);
    file.close();
    file.execute();
}
//-----------------------------------------------------------------------------------------------------------

aim to get book contents report

and tell me the overlop page numbers

I got the text file:

======================================================

13 Notes to account.indd 28

,14 Corporate Information.indd 27

,12 Cash flow statement.indd 26

,11 Equity change.indd 25

,10 Profit and loss.indd 20

,09 Income statement.indd 19

,08 Auditor's report.indd 18

,07 Directors' report.indd 17

,06 CG report.indd 12

,05 MD and A.indd 7

,03 Financial highlight.indd 6

,04 Chairman's statement.indd 3

,02 Summary.indd 2

,01 Contents.indd 1

======================================================

the sequence are upside down

======================================================

1) it always report me, the book has overlop page number, even the book has no overlop page number

2) if the files has overlop over more 2 pages or more two files have overlop page numbers the         alert(overlap.reverse())              will not correct in report

3) the report text file has a comma at beginning of each line

4) the sequence are upside down

I expected the       alert(overlap.reverse())   don't show if book has not overlap page number

can you fix it?

Harvey

This topic has been closed for replies.
Correct answer Chinnadk

Hi Harvey,

Try this,

var book = app.activeBook,  array1 = [], array2 = [],  array3 = [],  _content = "", _overlap = "";

for(var i =0;i<book.bookContents.length;i++)

{

        _content +=book.bookContents.name + "\t" + (book.bookContents.documentPageRange.replace(/-[^-]+$/g,"")) + "\r";

        array1.push(book.bookContents.name + "\t" + book.bookContents.documentPageRange)

        array2.push(book.bookContents.documentPageRange.split("-"))

    }

for(var i =1;i<array2.length;i++)

{

        if(array2[0]<=array2[i-1][1])

        {

                array3.push(array2.join("-"))

            }

    }

for(var i =0;i<array1.length;i++)

{

        for(var j =0;j<array3.length;j++)

        {

                if(array1.toString().indexOf(array3) != -1)

                {

                        _overlap+=array1 + "\r";

                    }

            }

    }

WriteToFile(_content, _overlap);

function WriteToFile(content, overlap)

{

        file = new File("~/Desktop/Book contents and page number.txt");

        file.encoding = "UTF-8";

        file.open("w");

        file.write("Contents\r\r");

        file.write(content);

        file.write("\r\r\r\rOverlap Contents\r\r")

        file.write(overlap);

        file.close();

        file.execute();

    }

Regards,

Chinna

3 replies

HarveyLiuAuthor
Inspiring
July 28, 2014

thank you Uwe

Harvey

Chinnadk
ChinnadkCorrect answer
Legend
July 30, 2014

Hi Harvey,

Try this,

var book = app.activeBook,  array1 = [], array2 = [],  array3 = [],  _content = "", _overlap = "";

for(var i =0;i<book.bookContents.length;i++)

{

        _content +=book.bookContents.name + "\t" + (book.bookContents.documentPageRange.replace(/-[^-]+$/g,"")) + "\r";

        array1.push(book.bookContents.name + "\t" + book.bookContents.documentPageRange)

        array2.push(book.bookContents.documentPageRange.split("-"))

    }

for(var i =1;i<array2.length;i++)

{

        if(array2[0]<=array2[i-1][1])

        {

                array3.push(array2.join("-"))

            }

    }

for(var i =0;i<array1.length;i++)

{

        for(var j =0;j<array3.length;j++)

        {

                if(array1.toString().indexOf(array3) != -1)

                {

                        _overlap+=array1 + "\r";

                    }

            }

    }

WriteToFile(_content, _overlap);

function WriteToFile(content, overlap)

{

        file = new File("~/Desktop/Book contents and page number.txt");

        file.encoding = "UTF-8";

        file.open("w");

        file.write("Contents\r\r");

        file.write(content);

        file.write("\r\r\r\rOverlap Contents\r\r")

        file.write(overlap);

        file.close();

        file.execute();

    }

Regards,

Chinna

HarveyLiuAuthor
Inspiring
July 31, 2014

Hi, Chinna

Thank you Chinna, your script is pefectly fit on my need

thank you so much, appreciate

Best Regard

Harvey

HarveyLiuAuthor
Inspiring
July 28, 2014

Hi, Chinna

It keeps telling me that I have a overlap, even though I don't

can you fix it?

Hi, Obey

I am a really new for sript, Can you Help the fix it?

Harvey

Community Expert
July 28, 2014

I will not provide any code, but layout the idea for it:

1. Open all documents of the book

2. Update all numbers in the book

3. Save the book

4. Loop through every open document of the book

5. Loop through every page of the an individual document of the book

6. Write the name of an individual page to an associative array and assign something meaningful like the name of the document

7. In case something is already assigned to the name of a page, the name is already used by a document.

Alert then. Or write the alert to an array and present its contents at the end of the script.

That's all in essence.

Uwe

Chinnadk
Legend
July 28, 2014

Try this,

var book = app.activeBook, 

    _content = [], 

    overlap = []; 

for(var i =book.bookContents.length-1;i>=0;i--) 

        _content.push(book.bookContents.name + "\t" + (book.bookContents.documentPageRange.replace(/-[^-]+$/g,"")) + "\r"); 

        if(book.bookContents.documentPageRange == book.bookContents[i-1].documentPageRange) 

        { 

                overlap.push(book.bookContents.name + "\t" +book.bookContents.documentPageRange + "\r" + book.bookContents[i-1].name + "\t" +book.bookContents[i-1].documentPageRange); 

            } 

    }

if(overlap.length >0)

{

        alert(overlap.reverse())

    }

_content.reverse();

var con = "";

for(var i =0;i<_content.length;i++)

{

        con+=_content + "\r";

    }

WriteToFile(con);

function WriteToFile(text) {

        file = new File("~/Desktop/Book contents and page number.txt");

        file.encoding = "UTF-8";

        file.open("w");

        file.write(text);

        file.close();

        file.execute();

    }

Regards,

Chinna

HarveyLiuAuthor
Inspiring
July 28, 2014

Hi, Chinna

It works, but without overlap report:

thanks

Regard

Harvey

HarveyLiuAuthor
Inspiring
July 28, 2014

Hi, Chinna

I expected the       alert(overlap.reverse())   don't show if book has not overlap page number

but if there was some overlap pages I need it to report to me the files or page number

for instance:

if

05. MD&A -----------> page number: 5-9

but

06. CG report -------> page number 6-9 or 4-5

the script have to tell me the overlop files

because the sequence

06.XXX always after 05.xxxxx

and the page numbers shall be after then 5-9 (the page numbers of 05. xxxxxxxxxx)

the function aim to tell me that have to solve the book page number problem

Regard

Harvey