Using the new applyMasterPage() command: Difference between revisions

From Scribus Wiki
Jump to navigation Jump to search
Line 8: Line 8:
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. At first one worries that the script didn't work, but if you save the file and reload it, you see the changes. Looking for something simpler than that, I saw 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.
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. At first one worries that the script didn't work, but if you save the file and reload it, you see the changes. Looking for something simpler than that, I saw 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.


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


<syntaxhighlight lang="python">
<syntaxhighlight lang="python">

Revision as of 01:30, 14 October 2014

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.

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. At first one worries that the script didn't work, but if you save the file and reload it, you see the changes. Looking for something simpler than that, I saw 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.

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.

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

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

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.

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.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)