Align to left page margin

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

Warning: before using this script, please consider going the easiest way: adding two pages in the middle of the document and remove the exceeding one at its end.

The simple script: re-align the main frame to the left margin

  • insert the new page(s).
  • select the first frame which is misplaced
  • the script will correct the placement of the selected frame and of all the frames
    • with the same width
    • at the same height
    • with the same distance from the margin on all the pages to the end of the file
  • possible features (but i won't implement them before the new scripter is there):
    • allow also documents starting with a left page
    • let the user define a page range

warning:

  • the script it will only work correctly on documents with the first page on the right side
#!/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)