Shifting All Page Objects

From Scribus Wiki
Jump to navigation Jump to search
This article is part of the Scripts series.

The genesis of this script came from a question about what to do in a situation where one has a Double-Sided layout, with right and left pages, you have already made several, but then you need to add a page somewhere in the middle. The side effect of this is to completely throw off your entire scheme if the lateral page margins are not equal. It's easy enough of course to fix the assignment of Right and Left Master Pages, but what about the main content?

shift_objects.py

What this script does is relatively simple. It makes a list of the items on your selected page, asks you how much you would like to shift, using a default of 40 points (adjusted according to your page units of points, mm, or inches). This somewhat arbitrary amount came from looking at a Gutenberg layout, where inside margins are 40 points and outside are 80 points (at least for US Letter paper).

The next question is which direction to shift. I thought this would be better than specifying, for example, 40 or -40 for the value. I've made it easy by making Left the default, and if you change to anything else, or even just delete the default, then items will shift Right. Note – this isn't quite true, since putting in left will also shift to the left.

Then it loops through the item list and moves all objects on the page – it doesn't matter whether they are selected or not.

If you consistently use some other sort of margins, then you can change the default values, but note that you need to consider the page units.

The action of the script seems to be Undo-able, but since it's really a series of actions, you will need to click Undo as many times as there are objects on the page. Looking at the Action Manager shows this in a listing.

The script

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

# ****************************************************************************
#  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.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software
#  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
# 
# ****************************************************************************


"""

© 2012 by Gregory Pittman

shift_objects.py


USAGE

Select a page where you need to shift all objects, run script.

The first dialog asks how much to shift, using a distance of 40 points

as the default, but modified to your units.

The second dialog asks whether to shift objects Left (the default), or Right.

Simply enter anything other than Left to shift right.

"""

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)

pageitems = scribus.getAllObjects()
scribus.setRedraw(False)
units = scribus.getUnit()
if units == 0:
    shift = 40
elif units == 1:
    shift = 14.111   # millimeters = points/2.8346
elif units == 2:
    shift = 0.5556   # inches = points/72.0

shiftamount = scribus.valueDialog('Amount of shift?','Enter the amount to shift',str(shift))
shiftamount = float(shiftamount)

shiftdir = scribus.valueDialog('Direction to shift?','Which direction to shift...\nAny change from default shifts to Right','Left')
if ((shiftdir == 'Left') or (shiftdir == 'left')):
    shiftamount = -shiftamount

for item in pageitems:
    scribus.moveObject(shiftamount, 0, item)

scribus.setRedraw(True)