Using the new applyMasterPage() command

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

Here are two scripts which make use of the new applyMasterPage() command now in 1.4.5svn and 1.5.0svn versions of Scribus.

Both of them take some of their structure from paste2pagelist.py and paste2all.py, which were written for the recently added copyObject() and pasteObject() commands. They will allow only one Master Page to be assigned at a time, so run them sequentially to make other assignments.

Note: I have removed the editMasterPage() command from the scripts, since the former problem has been resolved.

assignMP.py

The script now has the ability to allow an entry of a range, using a hyphen. For example, '1 3-6 9 11', will be expanded to '1 3 4 5 6 9 11'. To make it more complete, you can now enter pages to skip in a third valueDialog(), which could make some sense only if you used a range in your list of pages to apply the MP to. Since there are two situations where we might have ranges for input, I created the expand_pages function at the beginning.

# -*- 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.

Finally, you are asked if there are pages to skip, which you might want
to do if you have specified one or more ranges. The skipping list can also
include ranges.

"""
import scribus

def expand_pages(inlist):
    outlist = []
    for item in inlist:
        if '-' in item:
	    items = item.split('-')
	    for i in range(int(items[0]), int(items[1]) + 1):
	        outlist.append(i)
	else:
	    outlist.append(item)
    return outlist

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...',"Apply to which pages?\n(page numbers, separated by white space\nand could include a range separated by a hyphen)","1")
    
    templist = pagelist.split()
    pageslist = expand_pages(templist)
    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)

    skiplist = scribus.valueDialog('Skip these pages...',"Any pages or range to skip?\n(numbers separated by white space, can also be range\nseparated by a hyphen)","")
    skip_pages = []
    if (skiplist != ""):
        skippinglist = skiplist.split()
	skip_pages = expand_pages(skippinglist)

    for p in pageslist:
        skip_it = "no"
	p_no = int(p)
	for skip in skip_pages:
	    if (p_no == int(skip)):
	        skip_it = "yes"
	if (skip_it == "no"):
	    scribus.gotoPage(p_no)  # this line is probably unnecessary since AFAIK the applyMasterPage command is not dependent on this.
	    scribus.applyMasterPage(mpnames[int(masterPage)], p_no)
	    
    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)

assignMP2all.py

Here is another script, this time approaching the issue as if you might want to apply a Master Page to all pages, even pages, odd pages, or maybe even something more esoteric like the 4th out of every 9 pages. If you make a mistake in entry, the script runs and nothing happens, the exception perhaps if you might do something like 9/3.

In addition, there is an additional dialog which asks for a list of pages to skip, in case for example, you want to apply the MP to the even pages, except for page 10, or maybe the range of pages 10-16.

Something you may notice with the input for the list of pages to skip is that I don't test to see if the list contains pages outside the range of the document. This is unnecessary, since it won't affect the running of the script if you check the skip list for such pages.

# -*- coding: utf-8 -*-
# assignMP2all.py
# © Gregory Pittman 2014-10-11
# 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 whether you want to assign to all, even, or odd pages, but
you may also enter something like 3/5, for the third out of every 5 pages.

Finally, you are asked if you want to skip any pages, entered as individual numbers
separated by white space, and you can also enter a range, such as 3-5.

"""
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")
    apply_to = scribus.valueDialog('Assign Master Page to...',"Apply to which pages?\nall, even, odd, or\nor i/n for the ith member of every n pages","all")
    skiplist = scribus.valueDialog('Skip these pages...',"Any pages or range to skip?\n(numbers separated by white space, can also be range\nseparated by a hyphen)","")
    pages = scribus.pageCount()
    skip_pages = []
    if (skiplist != ""):
        skippinglist = skiplist.split()
        skip_pages = []
        for item in skippinglist:
	    if '-' in item:
	        items = item.split('-')
	        for i in range(int(items[0]), int(items[1]) + 1):
		    skip_pages.append(i)
	    else:
		skip_pages.append(item)
    
    if (apply_to == 'all'):
	i = 1
	jump = 1
    elif (apply_to == 'odd'):
        i = 1
        jump = 2
    elif (apply_to == 'even'):
        i = 2
        jump = 2
    elif ('/' in apply_to):
        pattern = apply_to.split('/')
        i = int(pattern[0])
        jump = int(pattern[1])
    while (i <= pages):
	skip_it = "no"
        for skip in skip_pages:
	   if (i == int(skip)):
	       skip_it = "yes"
	if (skip_it == "no"):
	   scribus.gotoPage(i)
	   scribus.applyMasterPage(mpnames[int(masterPage)], i)
	i = i + jump
	
    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)