FLOSS Manuals

 English |  Español |  Français |  Italiano |  Português |  Русский |  Shqip

Python Scripting with Scribus


This script makes use of the only 2 Scripter commands I had a hand in creating. I noticed that there was a duplicateObject() command, and on looking at the C program code for it, I could see that what the command did internally was to copy the object, then paste it. I thought there was enough structure that I could split this into two new commands, copyObject() and pasteObject(), and with enough help from other Scribus developers, I managed to get it done.

The script for this chapter is one that I wrote to illustrate how one might use these commands. Although the name is paste2all.py, we'll that it's more flexible than that.

# -*- coding: utf-8 -*-
# File: paste2all.py

You must have a document open. Select a single object. Run the script, which asks whether you want

all, odd, or even pages to get a copy of the selected object (no copy is made to the original

page of the object). You may also specify the nth page of a recurring number of pages. For example,

if you specify 2/3, the object will be copied to the second of every 3 pages.

Any other input is ignored. When pasted, the copies go to the same page

coordinates of the original.

The script does not work with groups, and in fact Scribus will hang and then crash if you have

selected a group. If you select more than one item without grouping, only one of those

will be copied and pasted.

import scribus

if scribus.haveDoc():

    if scribus.selectionCount() == 0:
        scribus.messageBox('Scribus - Usage Error',
                           "There is no object selected.\nPlease try again.",
                           scribus.ICON_WARNING, scribus.BUTTON_OK)
    if scribus.selectionCount() > 1:
        scribus.messageBox('Scribus - Usage Error', "You have more than one object selected.\nPlease select one frame and try again.", scribus.ICON_WARNING, scribus.BUTTON_OK)
    paste2 = scribus.valueDialog('Paste to...',"Paste where?\n(all, odd, even)","all")
    selframe = scribus.getSelectedObject()
    pages = scribus.pageCount()
    currpage = scribus.currentPage()
    if (paste2 == 'all'):
        i = 1
        jump = 1
    elif (paste2 == 'odd'):
        i = 1
        jump = 2
    elif (paste2 == 'even'):
        i = 2
        jump = 2
    elif ('/' in paste2):
        pattern = paste2.split('/')
        i = int(pattern[0])
        jump = int(pattern[1])

    while (i <= pages):
        if (i != currpage):
        i = i + jump

    scribus.messageBox("Finished", "Done",icon=0,button1=1)

    scribus.messageBox('Usage Error', 'You need a Document open', icon=0, button1=1)

You have seen my standard error-detection schemes, as we see at the beginning – we need to have a document, and an object selected. The first user input comes when we ask what sort of pattern of pasting is desired. The obvious choices in the messageBox() are all, odd, or even.  Notice that I don't do any error-checking for unorthodox entries. If you enter something strange, the script fails all the options and is quickly over. There is also a weird possible entry, something like 1/3, or 4/5 as described in the USAGE comment. This chooses a single page at some integer interval. 1/3 pastes to the first of every 3 pages, 4/5 pastes to the 4th of every 5 pages. This may never be used, but the logic seemed interesting.

Notice that getSelectedObject() only gets you the name of the object, this is why the copyObject() command was needed.

So now if we look at how we manage these schemes, you can see that I set up a starting point, i, and jump factor. For 'all' pages we start at 1 and jump by 1. For 'odd' and 'even' we jump two but start at page 1 or 2 respectively. Finally, with our weird scheme, we split the string at the '/', with the first integer being our starting point, then the second integer the jump factor.

After this, it's just a matter of going to the appropriate pages and pasting.

There has been error in communication with Booktype server. Not sure right now where is the problem.

You should refresh this page.