Using the new applyMasterPage() command: Difference between revisions

From Scribus Wiki
Jump to navigation Jump to search
Line 47: Line 47:
     templist = pagelist.split()
     templist = pagelist.split()
     pageslist = []
     pageslist = []
     for item in templist:
     for item in templist:   # here we parse templist to expand any range of pages which were entered
         if '-' in item:
         if '-' in item:
  item = item.split('-')
  item = item.split('-')

Revision as of 01:40, 11 October 2014

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. It will allow only one Master Page to be assigned, so run it sequentially to make other assignments. If you look at CopyObject()_and_pasteObject(), you will see examples of syntax to allow for assigning to all, even, or odd pages. You could also add another valueDialog() to make a list of pages to skip when using such patterns.

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

# -*- 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. Enter page numbers separated by 
whitespace; you may also specify a range of numbers separated by a hyphen (no
space between the numbers and the hyphen).
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...',"Apply to which pages?\n(page numbers, separated by white space)","1")
    templist = pagelist.split()
    pageslist = []
    for item in templist:   # here we parse templist to expand any range of pages which were entered
        if '-' in item:
	  item = item.split('-')
	  for i in range(int(item[0]),int(item[1])+1):
	    pageslist.append(i)
	else:
	  pageslist.append(item)

    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)