Skip to main content
Known Participant
November 11, 2022
Question

Trying to use app.execMenuItem("Save"); to save a file and having problems

  • November 11, 2022
  • 1 reply
  • 1143 views

I am trying to write a script that will create a tool button to save the current file and then flatten it. This is a workaround to a bug in my PDF-creation routine in my CAD software. So here is the script so far:

 

app.addToolButton({cName: "MyFlattenButton",
cLabel: "Flatten",
cTooltext: "Flatten all pages",
cEnable: "event.rc = (app.doc != null);",
cExec: "saveandflatten();"
});

 

function saveandflatten() {
// app.beginPriv();
app.execMenuItem("Save");
// app.endPriv();
this.flattenPages();
return 0;
}

 

I tried it first without the beginPriv and endPriv calls, then tried with those un-commented. Does not work. I imagine this is something simple. Basically I'm just trying to save the current file.

 

Thanks!

This topic has been closed for replies.

1 reply

Thom Parker
Community Expert
Community Expert
November 12, 2022

As I think you realize, these are priveleged operation. However, your code is missing the "trustedFunction" definition. You can find the details you need about trust and privelege in these articles:

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

https://www.pdfscripting.com/public/Using-Trusted-Functions.cfm

 

 

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
jteselleAuthor
Known Participant
November 12, 2022

Thank you for that guidance. Copying and pasting from the page you linked to, I changed my code to this:

 

app.addToolButton({cName: "MyFlattenButton",
cLabel: "Flatten",
cTooltext: "Flatten all pages",
cEnable: "event.rc = (app.doc != null);",
cExec: "saveandflatten();"
});

 

var Trusted_MyDocSave = app.trustedFunction(function(oDoc)
{
app.beginPriv();
oDoc.saveAs(oDoc.path);
app.endPriv();
});

function saveandflatten() {
Trusted_MyDocSave(this);
this.flattenPages();
return 0;
}

 

This seems to operate correctly now. I also added the Acrobat Javascripts folder as a trusted location in the Enhanced Security preferences window (was that necessary?). 

 

But now a new question -- sort of a long explanation but wondering why this occurs:

 

- My original need to add the save feature to this script is due to some bug in either Acrobat or my CAD software. When I create a PDF directly from my CAD software (Bricscad) it is faster to do it using the Bricscad-supplied create PDF feature rather than printing to Acrobat. But then if I open the file in Acrobat and add a stamp (which I often do) and then flatten the stamp (for security) it messes up all the fonts in the PDF. The workaround is to add the stamp, save the PDF, then flatten. That works fine for some reason.

 

I reported this bug to Adobe and to Bricscad but neither could solve it. So I thought why not just incorporate the save into my flatten routine. But when I do that and run the code above (which appears to operate correctly) the fonts get messed up as if the save did not happen. I tried commenting out the flatten code in the script to be sure the save was happening and it appears that it was being saved. 

 

Is something different happening in the script than when I do this manually? I tried manually using Save As before flattening and that works fine (no messed up fonts). 

jteselleAuthor
Known Participant
November 13, 2022

Fixed it -- for some reason, changing from the oDoc.saveAs(oDoc.path) function to app.execMenuItem("Save") makes it work. So for reference, here is the working routine. This saves the file before flattening and then again after flattening, just for convenience.

 

app.addToolButton({cName: "MyFlattenButton",
cLabel: "Flatten",
cTooltext: "Flatten all pages",
cEnable: "event.rc = (app.doc != null);",
cExec: "saveandflatten();"
});

 

var Trusted_MyDocSave = app.trustedFunction(function()
{
app.beginPriv();
app.execMenuItem("Save");
app.endPriv();
});

 

function saveandflatten() {
Trusted_MyDocSave();
this.flattenPages();
Trusted_MyDocSave();
return 0;
}