Using the new applyMasterPage() command

From Scribus Wiki
Jump to navigation Jump to search
This article is part of the Scripts series.

Here is an initial script which makes use of the new applyMasterPage() command now in 1.4.5svn and 1.5.0svn versions of Scribus. This particular script takes some of its structure from paste2pagelist.py, which was written for the recently added copyObject() and pasteObject() commands.

assignMP.py

There is some intentionally introduced weirdness in the script. You will note when you use it that at the end of the script you find yourself in Edit > Master Pages, with the MP you selected. This is because I found that without this maneuver, the appearance of the document would not update, so you would not see your changes. I found that if I selected Edit > Master Pages, then closed it, it would refresh and show the changes. I have seen this problem once before, and at the moment can't recall if I found some nicer workaround.

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# assignMP.py
# © Gregory Pittman 2014-10-09
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
"""
USAGE
You must have a document open. You will presumably have more than one 
Master Page, since otherwise the script makes little sense.

The first dialog shows a numbered list of your Master Pages, and asks
you to choose which one to apply by number.

Next you are asked for a list of pages to assign this MP to.
At the end you are left in the Edit Master Page mode. This is a kludge 
to make sure that the document refreshes to show your new MP assignments.

"""
import scribus

if scribus.haveDoc():
    scribus.setRedraw(0)
    mpnames = scribus.masterPageNames()
    mps = ""
    item = 0
    for mp in mpnames:
      mps = mps + str(item) + " " + mp + "\n"
      item += 1
    masterPage = scribus.valueDialog('Master Pages', "Here are your Master Pages: \n" + mps +"\n Choose the number of the MP to assign", "0")
    pagelist = scribus.valueDialog('Assign Master Page to...',"Paste to which pages?\n(page numbers, separated by white space)","1")
    pageslist = pagelist.split()
    pages = scribus.pageCount()
    for p in pageslist:
        p_no = int(p)
        if ((p_no > pages) or (p_no < 1)):
            scribus.messageBox('OOPS!', "You have a page number outside the range of pages in your document", scribus.ICON_WARNING, scribus.BUTTON_OK)
            sys.exit(2)

    for p in pageslist:
	p_no = int(p)
	scribus.gotoPage(p_no)
	scribus.applyMasterPage(mpnames[int(masterPage)], p_no)
    scribus.editMasterPage(mpnames[int(masterPage)])
    scribus.setRedraw(1)
    scribus.redrawAll()
    scribus.docChanged(1)
    scribus.messageBox("Finished", "Done",icon=0,button1=1)

else:
    scribus.messageBox('Usage Error', 'You need a Document open', icon=0, button1=1)
    sys.exit(2)