Lock/Unlock Button - Password Sync Issues
Hi all,
Need some help from someone who knows their stuff...
I've got a Interactive PDF that has a script set to a button where it locks certain fields, unless you know the password to make all fields editable. It's the same script that's on this help forum elsewhere. I need to take it a step further however, but I don't know how. I get the general gist of what I need to do, just don't know how to execute it.
My problem is my button has two states; a 'Locked' (red) state and 'Unlocked' (green) state. I need to know how to sync up the code so that if you click the button to unlock, then cancel the password prompt, that the button doesn't continue to switch to the 'unlock' state with it still being locked.
For example;
- the button is currently red (locked)
- you click the button to unlock, and the password prompt box comes up
- you cancel that prompt
- the button then switches to show the green (unlocked) state, even though it is still locked.
Is there a way to edit the script (below) so that it always knows what state the button is in, and reacts accordingly. So in the locked state, always asks for the password and in the unlocked state always locks the fields?
Any help would be great,
Thanks
----
The script i'm using is as follows:
(function () {
// Prefix for group field names. Change to match what you want to use.
// Rename the fields you want to lock to match this prefix (e.g., "PRIV.NAME")
var f_prefix = "PRIV";
// Your chosen password goes here
var pw = "1234";
// Get a reference to the first field in the group
var f = getField(f_prefix).getArray()[0];
//Determine new readonly state, which is the opposite of the current state
var readonly = !f.readonly;
var readonly_desc = readonly ? "lock" : "unlock";
//Prompt user for the password only if unlocking
if (f.readonly) {
var resp = app.response({
cQuestion: "To " + readonly_desc + " the fields, enter the password:",
cTitle: "Enter password",
bPassword: true,
cLabel: "Password"
});
} else {
var resp = pw;
}
switch (resp) {
case pw:
getField(f_prefix).readonly = readonly;
app.alert("The fields are now " + readonly_desc + "ed.", 3);
break;
case null: // User pressed Cancel button
break;
default: // Incorrect password
app.alert("Incorrect password.", 1);
break;
}
})();
