Horizontal Rule over Text Frame

From Scribus Wiki
Revision as of 16:39, 16 February 2017 by Gpittman (talk | contribs)
Jump to: navigation, search

{Scripting Index} 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.


#!/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.

    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."

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)
    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)
    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)

    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)