Horizontal Rule over Text Frame

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

Here is a simple script whose idea is to create a horizontal line over a text frame, much the way you might create a horizontal rule. The only value you enter is the width of the line as a percentage of the width of the text frame.

As written, it requires a text frame, but could be modified to use with some other object. The line is placed in a centered position over the frame, centered both horizontally and vertically. The presumption would be that you would then adjust the Y-Pos of the line as needed. The default line width is set at 1.0 points.

hrule.py

#!/usr/bin/env python
# -*- coding: utf-8  -*-
# hrule.py - draw horizontal line over text frame
# © 2017.02.16  Gregory Pittman

"""
This is a simple script that creates a horizontal rule of 1.0 pts

centered at the midpoint of a text frame, the length of the line 

specified as a percentage of the text frame width.
"""

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)

def main(argv):
    if scribus.selectionCount() == 0:
        scribus.messageBox('Scribus - Script Error',
            "There is no object selected.\nPlease select a text 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 text frame and try again.",
            scribus.ICON_WARNING, scribus.BUTTON_OK)
        sys.exit(2)
    textbox = scribus.getSelectedObject()
    pageitems = scribus.getPageItems()
    for item in pageitems:
        if (item[0] == textbox):
            if (item[1] != 4):
                scribus.messageBox('Scribus - Script Error', 
                          "This is not a textframe. Try again.", scribus.ICON_WARNING, scribus.BUTTON_OK)
                sys.exit(2)

    left, top = scribus.getPosition(textbox)
    o_width, o_height = scribus.getSize(textbox)
    linewidth = scribus.valueDialog('Size of line','Line should be what percentage of frame width?','80')
    linewidth = float(linewidth)/100
    lwidth = o_width * linewidth
    lineleft = left + (o_width - lwidth)/2
    linetop = top + o_height/2
    new_line = scribus.createLine(lineleft, linetop, lineleft + lwidth, linetop)
    scribus.setLineWidth(1.0, new_line) # default line width set

if __name__ == '__main__':

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

    scribus.setRedraw(False)

    try:
        main(sys.argv)
    finally:
        try:
            scribus.setRedraw(True)
        except:
            pass

Other notes

One might consider modifications for use in frames with columns, though what you might do in that situation is use the InfoBox script, then create the line over the InfoBox frame by selecting it. Something else to think about is that, once you get your line situated, you may want to group the frame and line(s) so that you don't accidentally move them.

Here is a hint on centering the line vertically between two lines of text. Roughly place the line, then increase its width to about the size of the space between the text above and below, then more precisely center it to your satisfaction. Reduce the line width to where you want it to be, and you should find it very well centered.

I also tried creating a line, then sending to Inline items, then inserting into the text of a frame. This is doable, and easy enough to manage centering horizontally by selecting center justification, but what is trickier is the vertical adjustment in the line, and you also lose the ability to adjust properties of the line.