Skip to main content
Participating Frequently
October 18, 2023
Answered

Blocking/unlocking the pages range

  • October 18, 2023
  • 2 replies
  • 610 views

Hi,

I have a request for help

 

I found here scripts for blocking and unlocking objects. How to make it to work on the scope (range) of the pages (e.g. pages from 15 to 60)?

Or selected pages (e.g. 15, 18 and 25)?

 

The code looks like this:

1. unlock all items in the document:

app.activeDocument.pageItems.everyItem().locked = false;

 

2. unlock items just on master pages use this:

app.activeDocument.masterSpreads.everyItem().pageItems.everyItem().locked = false;

 

3. unlock all items just on pages, not master pages:

app.activeDocument.pages.everyItem().pageItems.everyItem().locked = false;

 

Thank you for your help and best regards

Piotr_W

 
This topic has been closed for replies.
Correct answer Eugene Tyson
// Create a dialog box
var inputDialog = app.dialogs.add({
    name: "Unlock Pages",
    canCancel: true
});

// Add a dialog column
var dialogColumn = inputDialog.dialogColumns.add();

// Add a static text to the dialog box
dialogColumn.staticTexts.add({
    staticLabel: "Enter page numbers or range (e.g., 15-60)"
});

// Add an editbox to the dialog box for user input
var pageInput = dialogColumn.textEditboxes.add({
    minWidth: 200
});

// Show the dialog box
if (inputDialog.show() === true) {
    // Get user input from the editbox
    var userInput = pageInput.editContents;
    
    // Parse user input and unlock items on specified pages
    var pagesToUnlock = parsePageInput(userInput);
    unlockItemsOnPages(pagesToUnlock);
}

// Function to parse user input and return an array of page numbers
function parsePageInput(input) {
    var pageNumbers = [];
    var inputArray = input.split(",");

    for (var i = 0; i < inputArray.length; i++) {
        var range = inputArray[i].split("-");
        if (range.length === 1) {
            // Single page number
            pageNumbers.push(parseInt(range[0]) - 1); // Page numbers are 1-indexed, convert to 0-indexed
        } else if (range.length === 2) {
            // Page range
            var start = parseInt(range[0]);
            var end = parseInt(range[1]);
            for (var j = start; j <= end; j++) {
                pageNumbers.push(j - 1); // Page numbers are 1-indexed, convert to 0-indexed
            }
        }
    }
    return pageNumbers;
}

// Function to unlock items on specified pages
function unlockItemsOnPages(pages) {
    var document = app.activeDocument;
    for (var i = 0; i < pages.length; i++) {
        document.pages[pages[i]].pageItems.everyItem().locked = false;
    }
}

2 replies

Robert at ID-Tasker
Legend
November 11, 2023

For just a range - you can use "itemByRange" function directly:

 

var myPageRange = prompt("Enter Page Range","1,-1");
app.doScript("app.activeDocument.pages.itemByRange(" + myPageRange + ").pageItems.everyItem().locked = true;",ScriptLanguage.JAVASCRIPT);

 

  1. line - "1,-1" - default value, just in case if you won't enter anything = "first to last page" - negative values count from the end - so you could use "2,-2" - from second to second to last page, etc. And YOU HAVE to enter two numbers separated by "," - you can't use "normal" way - "2-5" - with "-" as a separator and you can't enter single digit either - if you want to process only one page - you need to enter same digit twice - "4,4".
  2. line - I don't know how to do this "directly" in JavaScript - I'm VB guy - so I'm using workaround - create string with code inside - of course you need to change "locked = true;" to "locked = false;" if you need to unlock.

Or it could be done as a second prompt.

 

Otherwise, @Eugene Tyson solution is much more sophisticated.

 

Piotr_WAuthor
Participating Frequently
November 11, 2023

Thank you, I'll practice.

I'm currently using the code from @Eugene Tyson and it works perfectly.

Eugene TysonCommunity ExpertCorrect answer
Community Expert
October 18, 2023
// Create a dialog box
var inputDialog = app.dialogs.add({
    name: "Unlock Pages",
    canCancel: true
});

// Add a dialog column
var dialogColumn = inputDialog.dialogColumns.add();

// Add a static text to the dialog box
dialogColumn.staticTexts.add({
    staticLabel: "Enter page numbers or range (e.g., 15-60)"
});

// Add an editbox to the dialog box for user input
var pageInput = dialogColumn.textEditboxes.add({
    minWidth: 200
});

// Show the dialog box
if (inputDialog.show() === true) {
    // Get user input from the editbox
    var userInput = pageInput.editContents;
    
    // Parse user input and unlock items on specified pages
    var pagesToUnlock = parsePageInput(userInput);
    unlockItemsOnPages(pagesToUnlock);
}

// Function to parse user input and return an array of page numbers
function parsePageInput(input) {
    var pageNumbers = [];
    var inputArray = input.split(",");

    for (var i = 0; i < inputArray.length; i++) {
        var range = inputArray[i].split("-");
        if (range.length === 1) {
            // Single page number
            pageNumbers.push(parseInt(range[0]) - 1); // Page numbers are 1-indexed, convert to 0-indexed
        } else if (range.length === 2) {
            // Page range
            var start = parseInt(range[0]);
            var end = parseInt(range[1]);
            for (var j = start; j <= end; j++) {
                pageNumbers.push(j - 1); // Page numbers are 1-indexed, convert to 0-indexed
            }
        }
    }
    return pageNumbers;
}

// Function to unlock items on specified pages
function unlockItemsOnPages(pages) {
    var document = app.activeDocument;
    for (var i = 0; i < pages.length; i++) {
        document.pages[pages[i]].pageItems.everyItem().locked = false;
    }
}
Piotr_WAuthor
Participating Frequently
October 18, 2023

Thank you very much!

That's what it was, works beautiful.

Best Regards

Piotr W.