Align to left page margin

From Scribus Wiki
Revision as of 13:18, 5 March 2010 by Ale (talk | contribs)
Jump to navigation Jump to search
#!/usr/bin/python

"""
adjust placement  of text frames when the insertion of a page
has scrambled the previous left/right "order"
@author: alessandro rimoldi
@version: 0.1 / 20100304
@copyright (c) 2010 alessandro rimoldi under the mit license
           http://www.opensource.org/licenses/mit-license.html
@bugs fix scripter API documentation: for double sided pages, getPageMargins() returns inner and outer margins instead of left and right
"""
import sys
try:
   import scribus
except ImportError:
   print "This script only works from within Scribus"
   sys.exit(1)

# check that the selection is one text frame and get that frame
frame_n = scribus.selectionCount()
if frame_n == 0 :
    scribus.messageBox('Error:', 'No frame selected')
    sys.exit(1)
elif frame_n > 1 :
    scribus.messageBox('Error:', 'You may select only one frame')
    sys.exit(1)

# store the current unit and use pt
unit_current = scribus.getUnit()
scribus.setUnit(scribus.UNIT_POINTS)

(mT, mI, mO , mB) = scribus.getPageMargins()

frame = scribus.getSelectedObject(0)
(x, y) = scribus.getPosition(frame)
(w, h) = scribus.getSize(frame)
p = scribus.currentPage()
n = scribus.pageCount()
# @todo: invert if first page is left
if (p % 2) == 0 :
    dX = mO - x
else :
    dX = mI - x
if dX == 0:
    scribus.messageBox('Error:', 'The current frame is already correctly placed')
    sys.exit(1)

for i in range(p, n + 1) :
    scribus.gotoPage(i)
    # scribus.messageBox('Message:', str(i))
    (iT, iI, iO , iB) = scribus.getPageMargins()
    iItem = scribus.getPageItems()
    for jItem in iItem:
        jFrame = jItem[0]
        (jX, jY) = scribus.getPosition(jFrame)
        (jW, jH) = scribus.getSize(jFrame)
        if (i % 2) == 0 :
            djX = iO - j
        else :
            djX = iI - jX
        # scribus.messageBox('Message:', 'i:'+str(i)+'\ndjX:'+str(djX))

        # the item must be at the same height as the original have the same width
        # and have a distance from the left margin equal to the difference
        # between left and right margin
        if (round(jY, 2) == round(y, 2)) and (round(jW, 2) == round(w, 2)) and (abs(round(djX, 2)) == abs(round(dX, 2))) :
            scribus.moveObject(djX, 0, jFrame)

# restore the user's unit
scribus.gotoPage(p)
scribus.setUnit(unit_current)