Skip to main content
Known Participant
July 21, 2023
Question

NotAllowedError

  • July 21, 2023
  • 2 replies
  • 3167 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 21, 2023

Maybe I did it wrong but I went to the Adobe folder from the C: Drive then navigated all the way to the JavaScript folder and then I created a file in there named secpol.js and put the same code that I have for the button in there. Then, I went to Security (Enhanced) and added the path to that file. Is that not correct? I still got the same error


The trusted function is not defined correctly. The code needs to be changed. 

First, delete the "applySecurityButtonClicked" function.

 Next Define the "applySecurityPolicy" function like this.

 

var applyMySecurityPolicy = app.trustedFunction(function (oDoc) {
   app.beginPriv();
 ...  your code ...
   app.endPriv();
});

 

 Now use this function in the "cExec" argument of the "addMenuItem" function. 

 

There is another problem with the code. It is a bad policy to use the keyword "this" in the "applyMySecurityPolicy" function, because there is a context switch when the menu item is called. The current object is not necessaryily the current document. Notice I've added the "oDoc" argument to the "applyMySecurityPolicy" function.  

 

This is how the function should be setup in the "addMenuItem"

 cExec: "applySecurityPolicy(event.target);",

 

 

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?