Using the new applyMasterPage() command: Difference between revisions

From Scribus Wiki
Jump to navigation Jump to search
No edit summary
 
(15 intermediate revisions by the same user not shown)
Line 1: Line 1:
{{Scripting Index}}
{{Scripting Index}}


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.
Here are two scripts which make 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.  
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.  
You could also add another valueDialog() to make a list of pages to skip when using such patterns.
 
''Note: I have removed the editMasterPage() command from the scripts, since the former problem has been resolved.''


==assignMP.py==
==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.
 
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'.


<syntaxhighlight lang="python">
<syntaxhighlight lang="python">
Line 28: Line 26:
you to choose which one to apply by number.
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
Next you are asked for a list of pages to assign this MP to.
whitespace; you may also specify a range of numbers separated by a hyphen (no
 
space between the numbers and the hyphen).
Finally, you are asked if there are pages to skip, which you might want
At the end you are left in the Edit Master Page mode. This is a kludge
to do if you have specified one or more ranges. The skipping list can also
to make sure that the document refreshes to show your new MP assignments.
include ranges.


"""
"""
import scribus
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():
if scribus.haveDoc():
Line 46: Line 55:
       item += 1
       item += 1
     masterPage = scribus.valueDialog('Master Pages', "Here are your Master Pages: \n" + mps +"\n Choose the number of the MP to assign", "0")
     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")
     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()
     templist = pagelist.split()
     pageslist = []
     pageslist = expand_pages(templist)
    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()
     pages = scribus.pageCount()
     for p in pageslist:
     for p in pageslist:
Line 63: Line 65:
             scribus.messageBox('OOPS!', "You have a page number outside the range of pages in your document", scribus.ICON_WARNING, scribus.BUTTON_OK)
             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)
             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:
     for p in pageslist:
        skip_it = "no"
p_no = int(p)
p_no = int(p)
scribus.gotoPage(p_no)
for skip in skip_pages:
scribus.applyMasterPage(mpnames[int(masterPage)], p_no)
    if (p_no == int(skip)):
     scribus.editMasterPage(mpnames[int(masterPage)])
        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)
 
</syntaxhighlight>
 
==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.
 
<syntaxhighlight lang="python">
# -*- 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.setRedraw(1)
     scribus.redrawAll()
     scribus.redrawAll()

Latest revision as of 19:29, 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.

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)