FLOSS Manuals

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

Python Scripting with Scribus

resizeobject.py

Here's another simple script. We might even just call this a utility. All it does is enlarge or shrink an object by a certain amount. Perhaps there may only be a reason to use it if you need to resize a number of objects, so you could just go down the document, selecting objects and running the script. It would not take too much work to operate on more than one selected object.

#!/usr/bin/env python
# -*- coding: utf-8  -*-
#resizeobject.py

"""

Select an object, start script.

Enter a value to shrink/enlarge by, click Ok.

"""

try:
    import scribus
except ImportError:
    print "Unable to import the 'scribus' module. This script will only run within"
    print "the Python interpreter embedded in Scribus. Try Script->Execute Script."
    sys.exit(1)

if not scribus.haveDoc():
    scribus.messageBox('Scribus - Script Error', "No document open", scribus.ICON_WARNING, scribus.BUTTON_OK)
    sys.exit(1)

if scribus.selectionCount() == 0:
    scribus.messageBox('Scribus - Script Error',
            "There is no object selected.\nPlease select a frame and try again.",
            scribus.ICON_WARNING, scribus.BUTTON_OK)
    sys.exit(2)
if scribus.selectionCount() > 1:
    scribus.messageBox('Scribus - Script Error',
            "You have more than one object selected.\nPlease select one frame and try again.",
            scribus.ICON_WARNING, scribus.BUTTON_OK)
    sys.exit(2)
selected_frame = scribus.getSelectedObject()
scribus.setRedraw(False)

dimensions = scribus.getSize(selected_frame) # (width, height)
factor = scribus.valueDialog("Resize Object", "Resize by multiple or decimal fraction", "0.5")
factor = float(factor)
newwidth = dimensions[0]*factor
newheight = dimensions[1]*factor
scribus.sizeObject(newwidth, newheight)

scribus.setRedraw(True)
scribus.redrawAll()

At this point you should be quite familiar with these methods for checking for scribus, checking for a document, if an object is selected, and that only one object is selected.

The only user input is to give a floating point number for the change, the default being half-size or 0.5. Something you may notice is that the position of the object stays the same, that is, the X-Pos, Y-Pos of the upper left corner. It might be interesting to figure out the math so that the center of the object remains the same, but the corners go in or out in a symmetrical fashion. In case you might wonder, even if you set the object's basepoint to the center in Properties, the effect of the script is the same.

And so, here are the additions:

dx = (dimensions[0] - newwidth)/2
dy = (dimensions[1] - newheight)/2
scribus.moveObject(dx, dy)

Put these 3 lines right after scribus.sizeObject(). Note that we don't need to know the X-Pos and Y-Pos of the object, since moveObject() simply deals with relative movement. If you do change the basepoint of the object to its center, you can see that this does not move with this resizing.

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

You should refresh this page.