Bullets: Difference between revisions
Jump to navigation
Jump to search
(Created page with "==Usage== Select paragraphs at any points. Run script. ==Caveats== * Weak insertion point algorithm. Can find wrong text before selection if selection is too short. '''Selec...") |
m (→Caveats) |
||
Line 5: | Line 5: | ||
==Caveats== | ==Caveats== | ||
* Weak insertion point algorithm | * Weak insertion point algorithm. '''Select unique text'''. | ||
* Clean all text format of text selection (font, bold, subscript, etc). | * Clean all text format of text selection (font, bold, subscript, etc). | ||
* Create paragraph style "Bullet", if it doesn't exist. | * Create paragraph style "Bullet", if it doesn't exist. | ||
Line 20: | Line 20: | ||
http://meiradarocha.jor.br | http://meiradarocha.jor.br | ||
License GPL 2.0 | License GPL 2.0 | ||
2011-01- | 2011-01-14a | ||
====== | ====== | ||
Line 55: | Line 55: | ||
askOpenDoc = "<h2>Open a document</h2>" \ | askOpenDoc = "<h2>Open a document</h2>" \ | ||
+"Open a document befora run this script." | +"Open a document befora run this script." | ||
# Message asking select more text | |||
askSelectMoreText = "<h2>Select more text</h2>" \ | |||
+"Not unique text selected. Select more text before run this script." | |||
########################## | ########################## | ||
bulletStyle = 'Bullet' | bulletStyle = 'Bullet' | ||
bulletString = u'•\t' # u'-\t' # | bulletString = u'•\t' # u'-\t' # | ||
################### | ################### | ||
# Apply style | # Apply style | ||
def applyStyle(style,frame): | def applyStyle(style,frame): | ||
'''Apply a style in selected text. | '''Apply a style in selected text. | ||
Line 84: | Line 85: | ||
allText = scribus.getAllText(selectedFrame) | 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) | |||
################### | |||
def cleanBullets(t): | def cleanBullets(t): | ||
'''Delete bullets.''' | '''Delete bullets.''' | ||
Line 97: | Line 101: | ||
return t | return t | ||
################## | ###################### | ||
# | # Get paragraph begin | ||
def getParagraphLimits(cur,allText,selectedText,story): | def getParagraphLimits(cur,allText,selectedText,story): | ||
'''Get paragraph begin/end and delete old bullets''' | '''Get paragraph begin/end and delete old bullets''' | ||
Line 121: | Line 125: | ||
return begin,end,allText | return begin,end,allText | ||
############################ | |||
# Do the all job | |||
def insertAllBullets(story): | def insertAllBullets(story): | ||
'''Insert bullets in front selected paragraphs.''' | '''Insert bullets in front selected paragraphs.''' | ||
Line 138: | Line 144: | ||
begin = nextPoint + bulletsLen + 1 | begin = nextPoint + bulletsLen + 1 | ||
##################### | |||
def handleSelected(): | def handleSelected(): | ||
"""Handle frame selection.""" | """Handle frame selection.""" | ||
story = scribus.getSelectedObject(0) | story = scribus.getSelectedObject(0) | ||
if story: | if story: | ||
insertAllBullets(story) | try: | ||
insertAllBullets(story) | |||
scribus.docChanged(True) | |||
except: | |||
scribus.messageBox(scriptWindowTitle,askSelectText,ICON_WARNING,BUTTON_OK) | |||
else: | else: | ||
scribus.messageBox(scriptWindowTitle,askSelectText,ICON_WARNING,BUTTON_OK) | scribus.messageBox(scriptWindowTitle,askSelectText,ICON_WARNING,BUTTON_OK) | ||
############### | |||
def main(argv): | def main(argv): | ||
"""Main entry point.""" | """Main entry point.""" | ||
Line 155: | Line 166: | ||
scribus.messageBox(scriptWindowTitle,askOpenDoc,ICON_WARNING,BUTTON_OK) | scribus.messageBox(scriptWindowTitle,askOpenDoc,ICON_WARNING,BUTTON_OK) | ||
####################### | |||
def main_wrapper(argv): | def main_wrapper(argv): | ||
try: | try: |
Revision as of 08:52, 13 January 2011
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).
- Create paragraph style "Bullet", if it doesn't exist.
- If there is no selection, apply bullets to all text in current frame.
#!/usr/bin/env python # -*- 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-14a ====== 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 UNIT_MILLIMETERS, BUTTON_OK, ICON_WARNING, mm, pt ######################### # YOUR IMPORTS GO HERE # ######################### ######### # Locales # Message to ask frame selection scriptWindowTitle = "Format bullets" askSelectText = "<h2>Select a text</h2>\n" \ +"Select a <b>story text</b> where you want insert bullets." # Message to open a document askOpenDoc = "<h2>Open a document</h2>" \ +"Open a document befora run this script." # Message asking select more text askSelectMoreText = "<h2>Select more text</h2>" \ +"Not unique text selected. Select more text before run this script." ########################## bulletStyle = 'Bullet' bulletString = u'•\t' # u'-\t' # ################### # Apply style 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 ########################## # Get text cursor position def getTextCursor(story): '''Find position of text cursor in text frame. Text need to be selected.''' selectedFrame = scribus.getSelectedObject() selectedText = scribus.getText() 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) ################### def cleanBullets(t): '''Delete bullets.''' global bulletString while t.count(bulletString): t = t.replace(bulletString,'') return t ###################### # Get paragraph begin 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 # If '\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 ############################ # Do the all job 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 ##################### def handleSelected(): """Handle frame selection.""" story = scribus.getSelectedObject(0) if story: try: insertAllBullets(story) scribus.docChanged(True) except: scribus.messageBox(scriptWindowTitle,askSelectText,ICON_WARNING,BUTTON_OK) 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)