• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
1

Delete unused master spreads

Contributor ,
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

2.6K

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

Community Expert , 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

...

Votes

Translate

Translate
Community Expert ,
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

Votes

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 ,
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

Votes

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;

  }

  }

}

Votes

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
Community Expert ,
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

If the answer wasn't in my post, perhaps it might be on my blog at colecandoo!

Votes

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
Community Expert ,
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

Votes

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
Community Expert ,
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)

 

Votes

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
Community Expert ,
May 10, 2021 May 10, 2021

Copy link to clipboard

Copied

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 )

Votes

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
Community Expert ,
Jan 09, 2023 Jan 09, 2023

Copy link to clipboard

Copied

Hi gang,

Which of the 4 scripts on this thread is the most reliable version of script to remove master pages/spreads? I see 4 versions here?

Mike Witherell

Votes

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
Community Expert ,
Jan 09, 2023 Jan 09, 2023

Copy link to clipboard

Copied

What if you just copy normal pages to a new document? 😉 it should copy ONLY used Masters... but it will also strip all unused *Styles and colors - so may not be exactly what you are looking for... 

 

▒► ID-Tasker / ID-Tasker Server - work smart not hard ◄▒

Votes

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
Community Expert ,
Jan 10, 2023 Jan 10, 2023

Copy link to clipboard

Copied

Hi @Mike Witherell ,

test the code Gregor Fellenz ( @grefel  ) did out of Peter's post.

https://community.adobe.com/t5/indesign-discussions/delete-unused-master-spreads/m-p/12026724#M42555...

 

Regards,
Uwe Laubender
( Adobe Community Expert )

Votes

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
Community Expert ,
Jan 10, 2023 Jan 10, 2023

Copy link to clipboard

Copied

LATEST

Thanks! 🙂

Mike Witherell

Votes

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