Skip to main content
Known Participant
July 21, 2023
Question

NotAllowedError

  • July 21, 2023
  • 2 replies
  • 3165 views

I have a custom button in JavaScript and everytime I run it I get the

"NotAllowedError: Security settings prevent access to this property or method.

App.addMenuItem:40:Field encrypt:Mouse Up"

 

Does anyone have any ideas on how to solve this?

This topic has been closed for replies.

2 replies

try67
Community Expert
Community Expert
July 21, 2023

The addMenuItem method needs to be executed from a privileged context.

Known Participant
July 21, 2023
//apply the security policy
function applyMySecurityPolicy() {
    var oMyPolicy = null;
    
    var aPols = security.getSecurityPolicies();
    
    for (var index = 0; index < aPols.length; index++) {
        if (aPols[index].name === "Test") {
            oMyPolicy = aPols[index];
            break;
        }
    }
    
    if (oMyPolicy == null) {
        app.alert("Policy Not Found");
        return;
    }
    
    app.beginPriv();
    
    var rtn = this.encryptUsingPolicy({ oPolicy: oMyPolicy });
    
    if (rtn.errorCode != 0) {
        app.alert("Security Error: " + rtn.errorText);
    }
    
    app.endPriv();
}

//handle button click
function applySecurityButtonClicked() {
    app.trustedFunction(function() {
        applyMySecurityPolicy();
    })();
}

//button click event
app.addMenuItem({
    cName: "Apply Security",
    cParent: "Tools",
    cExec: "applySecurityButtonClicked();",
    cEnable: "event.rc = (event.target != null);",
    cMarked: "event.rc = false;",
});

 

Like this?

Thom Parker
Community Expert
Community Expert
July 24, 2023

So theres no way to apply the security policy from a button click?


No, put the code in a folder level script, then call the "applySecurityPolicy()" function from the button. That will work. 

But there other ways, read 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
Bernd Alheit
Community Expert
Community Expert
July 21, 2023

What does you use there?