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

Script to auto-place PDFs failing on password-protected PDFs

Community Expert ,
Nov 07, 2019 Nov 07, 2019

I am iterating through a file array and placing PDFs on new pages, and some of the files are password protected. If the user doesn't know the password, I'd like them to be able to cancel and skip to the next file. Does anyone know how to access this part of the file through ExtendScript or a try/catch to solve for it? I have a try/catch on the frame.place() function, but that didn't work. Thanks. Below is the snippet if helpful. I am not doing anything with the file on the page beforehand except getting the list through file.openDialog and sorting the file array by name.  

 

 

for (var i = 0; i < sortedFiles.length; i++) {
  var page = doc.pages.add();
  page.appliedMaster = doc.masterSpreads.item(‘A-Master’);
  var targetFrame = page.rectangles.add({
  geometricBounds: [.9, .5, 10.5, 8],
  strokeWeight: 1,
  });
  try {
    targetFrame.place(sortedFiles[i]);
    targetFrame.fit(FitOptions.PROPORTIONALLY);
    targetFrame.fit(FitOptions.CENTER_CONTENT);
  } catch (e) {
  $.writeln(e);
  continue;
  }
}

 

TOPICS
Scripting
834
Translate
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 ,
Nov 08, 2019 Nov 08, 2019

I'm not sure this is what you need, but you can create an Acrobat action to find protected PDFs in a folder, create a list, and copy them into a new folder. In Acrobat, go to tools> Action Wizard> New Action> Document Processing> Preflight> (+) Use the Preflight check Document> Document is encripted. (In order for the preflight to be available to the Action Wizard, you will first need to export the preflight to your desktop, then import it back into Acrobat, so it will appear in your Imported Profiles folder). 

 

Translate
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 ,
Nov 08, 2019 Nov 08, 2019

Hi Brian,

just an idea, perhaps it would help if you are temporarily change the user interaction level in the scriptPreferences?

app.scriptPreferences.userInteractionLevel = UserInteractionLevels.NEVER_INTERACT;

If then no error message pops up you could test if the file was indeed placed.

frame.graphics.length should be 0 then.

 

Don't forget to restore the userInteractionLevel to: UserInteractionLevels.INTERACT_WITH_ALL.

That's the default.

 

DOM documentation compiled by Gregor Fellenz:

https://www.indesignjs.de/extendscriptAPI/indesign14/#ScriptPreference.html

 

Regards,
Uwe Laubender

( ACP )

 

 

Translate
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 ,
Nov 08, 2019 Nov 08, 2019

Good idea, Uwe, but unfortunately even with interaction disabled, the password prompt still pops up and then crashes the script on cancel. Here is the Debugger error message when that happens, if that gets your wheels spinning any more. The placing function is happening within main(): 

(#15)Cannot execute script in target engine 'main'!

Translate
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 ,
Nov 08, 2019 Nov 08, 2019

Relatedly, I just discovered that each file above is a multipage PDF. So, essentially, I am attempting to automate the placement of multiple multipage pdfs. I figure I can handle each page using PDFPlacePreference.pageNumber, but I need to know the total number of pages in each multipage PDF before doing that. I can’t seem to find where this info would be exposed though. Does anyone have any thoughts? I'm trying a hack, but I don't know how to access the sublink of this file name (with ":2" at the end). 

 

Screen Shot 2019-11-08 at 7.51.35 PM.png

Translate
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 ,
Nov 11, 2019 Nov 11, 2019

Hi Brian,

yes indeed, the total number of pages in a PDF you want to place is a problem.

I can only think of a way to indirectly detect this number by trial and error.

Where error does not mean that ExtendScript is throwing an error message!

 

Let's say you have a PDF with 10 pages and you set:

app.pdfPlacePreferences.pageNumber = 100;

placing the PDF will be successful. No error message.

The only error that you can detect is, that page 1 is placed and the value for pageNumber is set to default number 1.

 

So you could use that behavior of InDesign to get the number of pages in a PDF.

You have to change the value of pageNumber coming from an unlikely value until you meet a value that is not 1 next to a value that is 1. In our case above you place the PDF with pageNumber value 10 and then with pageNumber value 11.

 

Regards,
Uwe Laubender

( ACP )

 

Translate
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 ,
Nov 11, 2019 Nov 11, 2019
LATEST

Thanks, Uwe. That's similar to how I ended up resolving this problem. Posting here in case others need the same solution. If you use app.pdfPlacePreferences.pageNumber set to a number that exceeds the number of pdfs in the doc, it starts posting the first page again. I basically set a counter, then compared that counter to the PDF's PDFAttribute.pageNumber as I iterated through the place. 

 

 

var multiPDFPlacer = function(doc, file) { 
    app.pdfPlacePreferences.pageNumber = 1;
    var pageNum = 1;
    
    while(true) {
        
        var page = doc.pages.add();
        page.appliedMaster = doc.masterSpreads.item('A-Master');
        var targetFrame = page.rectangles.add({
            geometricBounds: [.9, .5, 10.5, 8],
            strokeWeight: 1,
        });
        
        try { 
            targetFrame.place(file);

            if (pageNum == 1) {
                continue;
            }
            else {
                
                if (targetFrame.pdfs[0].pdfAttributes.pageNumber == 1) {
                    page.remove();
                    break;
                }
            }
        } catch (e) {
            $.writeln(e);
            $.writeln("Problem placing page number " + pageNum + " from " + file.name + "Skipping rest of cutsheet."); 
            break;
        }
        pageNum++;
        app.pdfPlacePreferences.pageNumber = pageNum;
    }
};

 

 

Translate
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