Bullets

Usage
Select paragraphs at any points. Run script.

Caveats

 * Weak insertion point algorithm. Select unique text.
 * Clean all text format of text selection (font, bold, subscript, etc).
 * Script creates paragraph style "Bullet", if it doesn't exist.
 * If there is no selection, apply bullets to all text in current frame.


 * 1) !/usr/bin/env python
 * 2) -*- coding: utf-8 -*-

""" Format selected paragraphs with bullets. Author: prof. MS. José Antonio Meira da Rocha mailto:joseantoniomeira@gmail.com http://meiradarocha.jor.br License GPL 2.0 2011-01-14b

=
Usage: Select paragraphs at any points. Run script. Caveats: """
 * Clean all text format of text selection (font, bold, subscript, etc).
 * Create paragraph style "Bullet", if it doesn't exist.
 * If there is no selection, apply bullets to all text in current frame.

import sys

try: import scribus except ImportError,err: print "This Python script is written for the Scribus scripting interface." print "It can only be run from within Scribus." sys.exit(1) from scribus import BUTTON_OK, ICON_WARNING


 * 1) YOUR IMPORTS GO HERE  #
 * 1) YOUR IMPORTS GO HERE  #


 * 1) Locales
 * 1) Locales

scriptWindowTitle = "Format bullets" askSelectText = " Select a text \n" \ +"Select a story text where you want insert bullets." askOpenDoc = " Open a document " \ +"Open a document befora run this script." askSelectMoreText = " Select more text " \ +"Not unique text selected. Select more text before run this script." wrongFrameTypeErrorMsg = " Wrong object selected " \ +"You must select a text frame."
 * 1) Message to ask frame selection
 * 1) Message to open a document
 * 1) Message asking select more text

bulletStyle = 'Bullet' bulletString = u'•\t' # u'-\t' #
 * 1) Change this variables
 * 2) to match your needs
 * 1) to match your needs
 * 1) Put your favorite unicode char and tab: '\u2022\t'
 * 2) Get hex code from "Insert > Special character..." palette

def applyStyle(style,frame): '''Apply a style in selected text. If style doesn't exist, create it.''' try: scribus.setStyle(style,frame) except: scribus.createParagraphStyle(style) scribus.setStyle(style,frame) return frame
 * 1) Apply style
 * 1) Apply style

def getTextCursor(story): '''Find position of text cursor in text frame. Text need to be selected.''' selectedFrame = scribus.getSelectedObject try: selectedText = scribus.getText except: scribus.messageBox(scriptWindowTitle,wrongFrameTypeErrorMsg,ICON_WARNING,BUTTON_OK) sys.exit scribus.deselectAll allText = scribus.getAllText(selectedFrame) if allText.count(unicode(selectedText)) == 1: cur = allText.find(unicode(selectedText)) return cur,allText,selectedText else: scribus.messageBox(scriptWindowTitle,askSelectMoreText,ICON_WARNING,BUTTON_OK) sys.exit(1)
 * 1) Get text cursor position
 * 1) Get text cursor position

def cleanBullets(t): Delete bullets. global bulletString while t.count(bulletString): t = t.replace(bulletString,'') return t

def getParagraphLimits(cur,allText,selectedText,story): Get paragraph begin/end and delete old bullets # Get selected paragraph begin # (last line break position after selection, plus 1) begin = allText.rfind(u'\r',0,cur)+1 # Why not '\n'? # Get selection end end = cur+len(unicode(selectedText)) scribus.selectText(begin,(end - begin),story) selectedText = scribus.getText(story) # Clean old bullets cleanedText = cleanBullets(unicode(selectedText)) scribus.deleteText(story) scribus.insertText(cleanedText,begin,story) # Text was modified. Get all text again. scribus.deselectAll allText = scribus.getAllText(story) scribus.selectText(begin,len(unicode(cleanedText)),story) # Get selection new end end = begin+len(unicode(cleanedText)) return begin,end,allText
 * 1) Get paragraph begin
 * 1) Get paragraph begin

def insertAllBullets(story): Insert bullets in front selected paragraphs. global bulletString,bulletStyle cur,allText,selectedText = getTextCursor(story) begin,end,allText = getParagraphLimits(cur,allText,selectedText,story) # Insert bullets bulletsLen = 0 bl = len(unicode(bulletString)) # len needs unicode nextPoint = begin while True: scribus.insertText(bulletString,begin,story) applyStyle(bulletStyle,story) bulletsLen = bulletsLen + bl # total strings inserted lenght nextPoint = allText.find(u'\r',nextPoint+1,end) if nextPoint == -1: break begin = nextPoint + bulletsLen + 1
 * 1) Do all the job
 * 1) Do all the job

def handleSelected: """Handle frame selection.""" story = scribus.getSelectedObject(0) if story: insertAllBullets(story) scribus.docChanged(True) else: scribus.messageBox(scriptWindowTitle,askSelectText,ICON_WARNING,BUTTON_OK)

def main(argv): """Main entry point.""" if scribus.haveDoc: scribus.setRedraw(False) handleSelected else: scribus.messageBox(scriptWindowTitle,askOpenDoc,ICON_WARNING,BUTTON_OK)

def main_wrapper(argv): try: scribus.statusMessage("Running script...") scribus.progressReset main(argv) finally: if scribus.haveDoc: scribus.setRedraw(True) scribus.statusMessage("") scribus.progressReset

if __name__ == '__main__': main_wrapper(sys.argv)