Using the new applyMasterPage() command: Difference between revisions

From Scribus Wiki
Jump to navigation Jump to search
No edit summary
Line 64: Line 64:
     for p in pageslist:
     for p in pageslist:
p_no = int(p)
p_no = int(p)
scribus.gotoPage(p_no)
scribus.gotoPage(p_no)     # this line is probably unnecessary
scribus.applyMasterPage(mpnames[int(masterPage)], p_no)
scribus.applyMasterPage(mpnames[int(masterPage)], p_no)
     scribus.editMasterPage(mpnames[int(masterPage)])
     scribus.editMasterPage(mpnames[int(masterPage)])
Line 77: Line 77:


</syntaxhighlight>
</syntaxhighlight>
==assignMP2all.py==
==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.
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.

Revision as of 12:34, 12 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'.

# -*- 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 was 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)     # this line is probably unnecessary
	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.

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