Welcome Dialog

Welcome to the Community!

We have a brand new look! Take a tour with us and explore the latest updates on Adobe Support Community.


Delete unused master spreads

Participant ,
Aug 21, 2015 Aug 21, 2015

Copy link to clipboard

Copied

Hi,

   I need to delete unused master spreads from document. If there is any unused master spreads then its removed.  But when there is no unused master spreads, its pop up the below alert. Its removing pages. I'm using the below code.

  Whats my in the below code.

Screen shot 2015-08-22 at 8.03.59 AM.png

function removeUnusedMaster()

{

    app.selection=1851876449;

    if(app.menuActions.itemByID(6145).checked == true)

    {   

        try{           

            app.menuActions.itemByID(6151).invoke();

            app.menuActions.itemByID(6150).invoke();           

        }catch(e){        // Remove all spread except 1st one, when all spreads are unused except none.               

            app.activeDocument.masterSpreads[1].select()

            app.activeDocument.masterSpreads[app.activeDocument.masterSpreads.length-1].select(SelectionOptions.addTo);

            app.menuActions.itemByID(6176).invoke();           

        }

    }else

    {

        //try{           

            app.menuActions.itemByID(6145).invoke();

            app.menuActions.itemByID(6151).invoke();

            app.menuActions.itemByID(6150).invoke();           

        //}catch(e){}

    }

}

Thanks in advance,

Sudha K

TOPICS
Scripting

Views

1.6K

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Adobe Community Professional , Aug 22, 2015 Aug 22, 2015
The alert that you see pops up not when there are no unused master spreads, but when you try to delete a master that's applied to another master or a page/spread. You can't use menuActions for this I don't think because you need to be able to tell whether a master is applied. With menuActions the test is to check whether that alert pops up, but you can't script that.But it's not too hard to check whether masters are used: create a list of masters applied to the document's pages, another list of ...

Likes

Translate

Translate
Adobe Community Professional ,
Aug 22, 2015 Aug 22, 2015

Copy link to clipboard

Copied

The alert that you see pops up not when there are no unused master spreads, but when you try to delete a master that's applied to another master or a page/spread. You can't use menuActions for this I don't think because you need to be able to tell whether a master is applied. With menuActions the test is to check whether that alert pops up, but you can't script that.

But it's not too hard to check whether masters are used: create a list of masters applied to the document's pages, another list of masters applied to the document's masters (i.i. a list of masters based on other masters), then remove the masters that don't occur in either list.

(function(){

 

  var pages;

  var masters;

  var i;

  var appliedToPages = {};

  var appliedToMasters = {};

 

  // Get the names of the masters applied to document pages

  pages = app.documents[0].pages.everyItem().getElements();

  for (i = pages.length-1; i >= 0; i--) {

    if (pages.appliedMaster !== null) {

      appliedToPages[pages.appliedMaster.name] = true;

    }

  }

  // Get the names of masters applied to masters (i.e. masters based on other masters)

  masters = app.documents[0].masterSpreads.everyItem().getElements();

  for (i = masters.length-1; i > 0; i--) {

    if (masters.appliedMaster !== null) {

      appliedToMasters[masters.appliedMaster.name] = true;

    }

  }

  // Delete masters that are not applied to any document pages or other masters

  for (i = masters.length-1; i > 0; i--) {

    if (!appliedToPages[masters.name] && !appliedToMasters[masters.name]) {

     // $.writeln (masters.name);

      masters.remove();

    }

  }

}());

Peter

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Aug 27, 2015 Aug 27, 2015

Copy link to clipboard

Copied

Hi,

     Thank you... Its working fine... Sorry for the late reply...

 

Thanks,

Sudha K

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Aug 29, 2015 Aug 29, 2015

Copy link to clipboard

Copied

Almost what I was looking for.

I added a do/while loop because masters not used in the document with unused masters based on them weren't being deleted. I'm sure there is a faster way of doing it, but this is working for me.

var myDocument = app.activeDocument;

var masterPagesHaveBeenDeleted = false;

var pages;

var masters;

var i;

var appliedToPages = {};

mastersAppliedToPages ();

do {

removeUnusedMasters ();

}

while (masterPagesHaveBeenDeleted === true);

function mastersAppliedToPages (){

  // Get the names of the masters applied to document pages

  pages = myDocument.pages.everyItem().getElements();

  for (i = pages.length-1; i >= 0; i--) {

  if (pages.appliedMaster !== null) {

  appliedToPages[pages.appliedMaster.name] = true;

  }

  }

}

function removeUnusedMasters (){

  masterPagesHaveBeenDeleted = false;

  var appliedToMasters = {};

  // Get the names of masters applied to masters (i.e. masters based on other masters)

  masters = myDocument.masterSpreads.everyItem().getElements();

  for (i = masters.length-1; i > 0; i--) {

  if (masters.appliedMaster !== null) {

  appliedToMasters[masters.appliedMaster.name] = true;

  }

  }

  // Delete masters that are not applied to any document pages or other masters

  for (i = masters.length-1; i > 0; i--) {

  //$.writeln ("checking  " + masters.name);

  if (!appliedToPages[masters.name] && !appliedToMasters[masters.name]) {

  //$.writeln ("     deleting  " + masters.name);

  masters.remove();

  masterPagesHaveBeenDeleted = true;

  }

  }

}

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Aug 29, 2015 Aug 29, 2015

Copy link to clipboard

Copied

I have just tested this script with a scenario as set out by Uwe Laubender from this post (12th post in) https://forums.adobe.com/message/1112390#1112390

The script has an unwanted result of removing a master page that is in use.

Thought it best to warn you.

Colin

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Sep 03, 2015 Sep 03, 2015

Copy link to clipboard

Copied

Thanks, Colin. I had completely forgotten about that thread. The script in comment 1, above, can be changed to handle Uwe's case by looking at master pages rather than master spreads in the second for-loop:

(function(){

  var pages;

  var masterPages;

  var masterSpreads;

  var i;

  var appliedToPages = {};

  var appliedToMasters = {};

  // Get the names of the masters applied to document pages

  pages = app.documents[0].pages.everyItem().getElements();

  for (i = pages.length-1; i >= 0; i--) {

    if (pages.appliedMaster !== null) {

      appliedToPages[pages.appliedMaster.name] = true;

    }

  }

  // Get the names of masters applied to masters (i.e. masters based on other masters)

  masterPages = app.documents[0].masterSpreads.everyItem().pages.everyItem().getElements();

  for (i = masterPages.length-1; i > 0; i--) {

    if (masterPages.appliedMaster !== null) {

      appliedToMasters[masterPages.appliedMaster.name] = true;

    }

  }

  // Delete masters that are not applied to any document pages or other masters

  masterSpreads = app.documents[0].masterSpreads.everyItem().getElements();

  for (i = masterSpreads.length-1; i > 0; i--) {

    if (!appliedToPages[masterSpreads.name] && !appliedToMasters[masterSpreads.name]) {

      masterSpreads.remove();

    }

  }

}());

Nice catch, Naomi. I didn't put your fix in yet.

Peter

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
May 10, 2021 May 10, 2021

Copy link to clipboard

Copied

Peter scripts works fine. Fixed a little glitch in the second for loop (>= instead of >)

 

function remmoveMasterSpreads(dok) {
    // Peter Kahrel https://community.adobe.com/t5/indesign/delete-unused-master-spreads/td-p/7433938
    var appliedToPages = {};
    var appliedToMasters = {};
    // Get the names of the masters applied to document pages
    var pages = dok.pages.everyItem().getElements();
    for (var i = pages.length - 1; i >= 0; i--) {
        var page = pages[i];
        if (page.appliedMaster !== null) {
            appliedToPages[page.appliedMaster.name] = true;
        }
    }
    // Get the names of masters applied to masters (i.e. masters based on other masters)
    var masterPages = dok.masterSpreads.everyItem().pages.everyItem().getElements();
    for (i = masterPages.length - 1; i >= 0; i--) {
        var masterPage = masterPages[i]
        if (masterPage.appliedMaster !== null) {
            appliedToMasters[masterPage.appliedMaster.name] = true;
        }
    }
    // Delete masters that are not applied to any document pages or other masters
    var masterSpreads = dok.masterSpreads.everyItem().getElements();
    for (i = masterSpreads.length - 1; i > 0; i--) {
        var masterSpread = masterSpreads[i];
        if (!appliedToPages[masterSpread.name] && !appliedToMasters[masterSpread.name]) {
            masterSpread.remove();
        }
    }
} 

remmoveMasterSpreads(app.activeDocument)

 

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
May 10, 2021 May 10, 2021

Copy link to clipboard

Copied

LATEST

Hi Colin,

the link you posted is not working anymore, because that thread was moved from the old InDesign Scripting forum to the new InDesign forum by the end of 2019. Searched a bit in the forum. I think, you refer to this post by me:

 

Can I build a script to delete unused masters in CS2?

Laubender, Feb 24, 2015

https://community.adobe.com/t5/indesign/can-i-build-a-script-to-delete-unused-masters-in-cs2/m-p/115...

 

Regards,
Uwe Laubender

( ACP )

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines