Skip to main content
added 42 characters in body
Source Link
Brendan
  • 16.4k
  • 12
  • 56
  • 107

Now, take the following script, copy/paste into a text editor, and save it to your Scripts directory (as a .js or .jsx file, doesn't matter):

Now, take the following script and save it to your Scripts directory (.js or .jsx, doesn't matter):

Now, take the following script, copy/paste into a text editor, and save it to your Scripts directory (as a .js or .jsx file, doesn't matter):

Renamed a variable. Hope I did it right :P
Source Link
Brendan
  • 16.4k
  • 12
  • 56
  • 107
try // to get the path of the file that's active when you run the script.
{
    var OpenFilePath = app.documents.item(0).fullName; // Declare a variable representing the open document.
    var OpenFile = app.open(File(OpenFilePath), true, 1332757360); // Create a duplicate to work with. In Adobe's world, "1332757360" means "open a copy".
}
catch (err)
{
    var OpenFile = "error";
    alert("Please save this file before using the script.");
}

var OpenFileLength = OpenFile.pages.length; // Get number of pages of open document and master file.

// These help make the array that stores master markers.
var ArrayCounter = 0;
var MasterMarkersOpenFileFindTheMarkers = new Array();

for (var i=0; i<OpenFileLength; i++) // Loop through every page.
{
    ItemsOnPage = OpenFile.pages.item(i).pageItems.length; // Get the number of items on the page.
    
    for (var j=0; j<ItemsOnPage; j++) // Loop through every item.
    {
        var ScriptLabel = OpenFile.pages.item(i).pageItems.item(j).label;   

        if (ScriptLabel != "" && ScriptLabel.indexOf("DRAFT_LABEL") == 0) // If the item has a label and it equals what we want it to,
        {
            MasterMarkersOpenFile[ArrayCounter]FindTheMarkers[ArrayCounter] = i; // Put the page number in the array.
            ArrayCounter++; // Advance the counter for next time!
        }
    }
}

var numberToSubtract = 0; // This compensates for screwing up the page counter when you remove a page.

for (i=0; i<MasterMarkersOpenFilei<FindTheMarkers.length; i++) // Loop through the array and remove pages!
{
    OpenFile.pages.item(MasterMarkersOpenFile[i]FindTheMarkers[i] - numberToSubtract).remove();
    numberToSubtract++;
}
try // to get the path of the file that's active when you run the script.
{
    var OpenFilePath = app.documents.item(0).fullName; // Declare a variable representing the open document.
    var OpenFile = app.open(File(OpenFilePath), true, 1332757360); // Create a duplicate to work with. In Adobe's world, "1332757360" means "open a copy".
}
catch (err)
{
    var OpenFile = "error";
    alert("Please save this file before using the script.");
}

var OpenFileLength = OpenFile.pages.length; // Get number of pages of open document and master file.

// These help make the array that stores master markers.
var ArrayCounter = 0;
var MasterMarkersOpenFile = new Array();

for (var i=0; i<OpenFileLength; i++) // Loop through every page.
{
    ItemsOnPage = OpenFile.pages.item(i).pageItems.length; // Get the number of items on the page.
    
    for (var j=0; j<ItemsOnPage; j++) // Loop through every item.
    {
        var ScriptLabel = OpenFile.pages.item(i).pageItems.item(j).label;   

        if (ScriptLabel != "" && ScriptLabel.indexOf("DRAFT_LABEL") == 0) // If the item has a label and it equals what we want it to,
        {
            MasterMarkersOpenFile[ArrayCounter] = i; // Put the page number in the array.
            ArrayCounter++; // Advance the counter for next time!
        }
    }
}

var numberToSubtract = 0; // This compensates for screwing up the page counter when you remove a page.

for (i=0; i<MasterMarkersOpenFile.length; i++) // Loop through the array and remove pages!
{
    OpenFile.pages.item(MasterMarkersOpenFile[i] - numberToSubtract).remove();
    numberToSubtract++;
}
try // to get the path of the file that's active when you run the script.
{
    var OpenFilePath = app.documents.item(0).fullName; // Declare a variable representing the open document.
    var OpenFile = app.open(File(OpenFilePath), true, 1332757360); // Create a duplicate to work with. In Adobe's world, "1332757360" means "open a copy".
}
catch (err)
{
    var OpenFile = "error";
    alert("Please save this file before using the script.");
}

var OpenFileLength = OpenFile.pages.length; // Get number of pages of open document and master file.

// These help make the array that stores master markers.
var ArrayCounter = 0;
var FindTheMarkers = new Array();

for (var i=0; i<OpenFileLength; i++) // Loop through every page.
{
    ItemsOnPage = OpenFile.pages.item(i).pageItems.length; // Get the number of items on the page.
    
    for (var j=0; j<ItemsOnPage; j++) // Loop through every item.
    {
        var ScriptLabel = OpenFile.pages.item(i).pageItems.item(j).label;   

        if (ScriptLabel != "" && ScriptLabel.indexOf("DRAFT_LABEL") == 0) // If the item has a label and it equals what we want it to,
        {
            FindTheMarkers[ArrayCounter] = i; // Put the page number in the array.
            ArrayCounter++; // Advance the counter for next time!
        }
    }
}

var numberToSubtract = 0; // This compensates for screwing up the page counter when you remove a page.

for (i=0; i<FindTheMarkers.length; i++) // Loop through the array and remove pages!
{
    OpenFile.pages.item(FindTheMarkers[i] - numberToSubtract).remove();
    numberToSubtract++;
}
Source Link
Brendan
  • 16.4k
  • 12
  • 56
  • 107

Because of the page number thing, this might not be a perfect solution. But here's what solution I came up using scripting:

Download this sample file and save to your system. It's IDML from CS5, so it should work from CS4+.

You'll notice on pages 3 and 4 that there is a big pink block of text that says DRAFT:

DRAFT screenshot

If you pull up the Script Label panel (Window > Utilities > Script Label), you'll see that it's labeled as "DRAFT_LABEL".

Now, take the following script and save it to your Scripts directory (.js or .jsx, doesn't matter):

try // to get the path of the file that's active when you run the script.
{
    var OpenFilePath = app.documents.item(0).fullName; // Declare a variable representing the open document.
    var OpenFile = app.open(File(OpenFilePath), true, 1332757360); // Create a duplicate to work with. In Adobe's world, "1332757360" means "open a copy".
}
catch (err)
{
    var OpenFile = "error";
    alert("Please save this file before using the script.");
}

var OpenFileLength = OpenFile.pages.length; // Get number of pages of open document and master file.

// These help make the array that stores master markers.
var ArrayCounter = 0;
var MasterMarkersOpenFile = new Array();

for (var i=0; i<OpenFileLength; i++) // Loop through every page.
{
    ItemsOnPage = OpenFile.pages.item(i).pageItems.length; // Get the number of items on the page.
    
    for (var j=0; j<ItemsOnPage; j++) // Loop through every item.
    {
        var ScriptLabel = OpenFile.pages.item(i).pageItems.item(j).label;   

        if (ScriptLabel != "" && ScriptLabel.indexOf("DRAFT_LABEL") == 0) // If the item has a label and it equals what we want it to,
        {
            MasterMarkersOpenFile[ArrayCounter] = i; // Put the page number in the array.
            ArrayCounter++; // Advance the counter for next time!
        }
    }
}

var numberToSubtract = 0; // This compensates for screwing up the page counter when you remove a page.

for (i=0; i<MasterMarkersOpenFile.length; i++) // Loop through the array and remove pages!
{
    OpenFile.pages.item(MasterMarkersOpenFile[i] - numberToSubtract).remove();
    numberToSubtract++;
}

Before you run the script, save the document. Then run it!

I'm a designer who scripts and not the other way around, so this might not be the most elegant code. But what it's doing is scanning through your document for page items that have the "DRAFT_LABEL" tag, then it stores that page number in an array. Once it's done scanning, it removes the appropriate pages.

You're then left with a new file that has the draft pages removed!