Horizontal Rule over Text Frame: Difference between revisions

From Scribus Wiki
Jump to navigation Jump to search
No edit summary
Line 73: Line 73:


</syntaxhighlight>
</syntaxhighlight>
===Other notes===

Revision as of 16:40, 16 February 2017

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