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

REPORT TEMPLATE - Spawn, Extract, Rename, & Delete

Contributor ,
Sep 28, 2023 Sep 28, 2023

So, I have stumbled upon a trifecta by managing to get a script working that spawns, extracts, and deletes the spawned page from the original document -- effectively creating a separate uneditable report document using my read-only "DISPLAY..." fieldnames method in a template contained within the parent document.  As the report document is always spawned to the last page, I have set the extract & delete to the last page as well.

 

//Working Script
this.getTemplate("TRANSCOM").spawn();
var sourceDocument = this;
var totalPageCount = this.numPages;
this.pageNum = totalPageCount - 1;
this.extractPages({nStart: this.pageNum =
totalPageCount - 1})
sourceDocument.deletePages(totalPageCount - 1);

//END


Unfortunately, I require a superfecta and have failed miserably in accomplishing a "save as" or script prompted "rename" of the extracted page in addition to the previously successful spawn/extract/delete.  I have attempted numerous methods with zero success to include attempts of scripting a predetermined filename -- all to no avail.

 

//Nonworking Script

this.getTemplate("TRANSCOM").spawn();
var sourceDocument = this;
var totalPageCount = this.numPages;
this.pageNum = totalPageCount -1;
this.extractPages({nStart: this.pageNum = totalPageCount - 1});
extractedPageFile.rename(newFilePath);
var newFilename = app.response("TRANSCOM");
if (newFilename) {
var currentPath = this.path;
var fileExtension = currentPath.substring(currentPath.lastIndexOf("."));
var newPath = currentPath.substring(0, currentPath.lastIndexOf("/") + 1) + newFilename + fileExtension;
this.saveAs(newPath);
this.closeDoc(true);
app.openDoc(newPath);
} else {
app.alert("Please rename file using the following format 'TRANSCOM-ddMMMyyyy'.");
}
sourceDocument.deletePages(totalPageCount - 1);
//END

I've managed to get the "newFilename & app.response" working independently as a scripted button but once combined with the template spawn and extraction, there is an all stop.  Does anybody have any ideas?  Any and all assistance is greatly appreciated. 🙂

TOPICS
How to , JavaScript , PDF , PDF forms
1.3K
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 ,
Sep 28, 2023 Sep 28, 2023

So it's important to assign values to variables before using them, and to use the Acrobat JavaScript reference so that the Acrobat JS objects, functions, and properties are used correctly. And also so that you are using real functions. 

 

Did you look at the JavaScript console to see what error is reported?   

Here's a tutorial on the console:

https://www.pdfscripting.com/public/images/video/AcroJSIntro/AcroJSIntro_ConsoleWindow.cfm 

 

I think I mentioned this before, but extracting a page is a really bad idea for a script inside a document. And it just isn't going to work here. There are also many errors and some non-sense code.  

Read this:

https://www.pdfscripting.com/public/How-to-Save-a-PDF-2.cfm

and this:

https://www.pdfscripting.com/public/Trust-and-Privilege-in-Acrobat-Scripts.cfm

 

 

 

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

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
Contributor ,
Sep 28, 2023 Sep 28, 2023

The JS console merely responds with a terribly unhelpful "undefined".  As for any errors and nonsense code... I will happily hit the "I believe" button as I have zero depth of understanding of what I'm doing.  I think I'v mentioned this before, but I occassionally manage to cobble things together that work and more often than not, come to the Adobe forums as a means to fix what I've either broken or cannot figure out, :S 

 

All of this fun gets dumped on my lap becuase I'm a stubborn knuckldragger who refuses to quit -- there is nearly always a way. 😉  

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 ,
Sep 28, 2023 Sep 28, 2023

"undefined" is not an error. The opposite, actually. It means the code has finished executing, without returning any values or causing any errors.

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 ,
Sep 28, 2023 Sep 28, 2023

You do have multiple errors in your code, though... For example:

 

This is not how you specify a page number:

this.extractPages({nStart: this.pageNum = totalPageCount - 1});

 

There's no "rename" method:
extractedPageFile.rename(newFilePath);

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 ,
Sep 28, 2023 Sep 28, 2023
quote

"undefined" is not an error. ...


By @try67

 

The exception is reporting "extractedPageFile is undefined". So it's an error in this case

 

To fix the reported error, the line before this error should be:

extractedPageFile = this.extractPages ... 

 

However, there is so much bad going on that I think that most of the entire script needs to be deleted and rewritten. So I didn't want to mention this before.  

 

Charles, please read the articles I posted.

 

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

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
Contributor ,
Sep 28, 2023 Sep 28, 2023

Thank you Thom, I've spent the past hour reading them in an attempt to gain something of an understanding of what I'm doing... I'll make no claims as to success but all reference material is a blessing.  My sincerest appreciation for your and Try67/s assistance.  😉

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 ,
Sep 28, 2023 Sep 28, 2023
LATEST

What you are doing is not basic scripting, it's complicated. And if you really need to extract pages, it'll need to be done in an automation script. 

 

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

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