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

Create a document with the same number of pages as the selected pdf

Explorer ,
Feb 09, 2017 Feb 09, 2017

Copy link to clipboard

Copied

Hello everyone,

     I am running into a problem where my indesign just completely freezes returning no error code when I run this script, the goal is that indesign will create a document with the same number of pages as the selected pdf. This is what I have so far:

Initialization();

function Initialization() {

    //prompt user to select a file.

    var selectedFile = File.openDialog("Choose a PDF File");

    //Retrieve the page count of the selected file.

    var pageCount = readPageCount(selectedFile);

    createDoc(selectedFile, pageCount);

}

// This function is based on another script and believe the issue lies within this section here:

function readPageCount(selectedFile) {

  // Read in first line of section

  var theLine = selectedFile.readln();

  // Locate the line containing the /Count entry

  while(theLine.indexOf("/Count") == -1)

  {

  theLine = selectedFile.readln();

  }

  // Extract the page count

  return parseInt(theLine.substring(theLine.indexOf("/Count") +6), 10);

}

function createDoc(selectedFile, pageCount) {

    //Create a new document with the same number of pages as the selected document.

    var BC10UP = app.documents.add();

    with (BC10UP.documentPreferences) {

        pageHeight = "11in";

        pageWidth = "8.5in";

        pageOrientation = PageOrientation.landscape;

        pagesPerDocument = pageCount; }

}

any help or advice is much appreciated!!

TOPICS
Scripting

Views

276

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 ,
Feb 09, 2017 Feb 09, 2017

Copy link to clipboard

Copied

Well I have already found a noob mistake, should be item(0) not item [0]. Setill learning :,)

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
Mentor ,
Feb 09, 2017 Feb 09, 2017

Copy link to clipboard

Copied

LATEST

Hi,

1. selectedFile should be opened before calling readln()

2. looks like not every pdf contain the string "/Count" (at least some on my side)

do a "while loop" with selectedFile.eof condition and break a loop if the string "/Count" is found

something like:

//...

selectedFile.open("r");

do {

    theLine = selectedFile.readln();

    theIndex = theLine.indexOf("/Count");

    if (theIndex >=0) {

          selectedFile.close();

          return parseInt(theLine.substring(theIndex + 6), 10)

          }

     }

    while (!selectedFile.eof)

selectedFile.close();

return -1

//....

if -1 returned desired string is not found

Jarek

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