Skip to main content
Known Participant
July 21, 2023
Question

NotAllowedError

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

Known Participant
July 24, 2023

No, any JS code placed in a ".js" file and saved to one of the two Acrobat JavaScript folders will be loaded and run at Acrobat startup. Loaded script are run as privileged.  So this is where any global variables, trusted functions, menu items, or toolbar buttons are defined.  

 


So I have this code. But unfortunately, I am still getting the error. (I added this code to the JavaScript Folder just in case not sure if thats causing an issue. 

// Define the "applyMySecurityPolicy" function with the trusted function
var applyMySecurityPolicy = app.trustedFunction(function (oDoc) {
var oMyPolicy = null;

var aPols = security.getSecurityPolicies();

for (var index = 0; index < aPols.length; index++) {
if (aPols[index].name === "MySecurityPol") {
oMyPolicy = aPols[index];
break;
}
}

if (oMyPolicy == null) {
app.alert("Policy Not Found");
return;
}

oDoc.beginPriv();

var rtn = oDoc.encryptUsingPolicy({ oPolicy: oMyPolicy });

if (rtn.errorCode != 0) {
app.alert("Security Error: " + rtn.errorText);
}

oDoc.endPriv();
});

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

 

Bernd Alheit
Community Expert
Community Expert
July 21, 2023

What does you use there?