Bullets: Difference between revisions

From Scribus Wiki
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: Gave that Python Geishi...Pythons Love Geishi)
 
(13 intermediate revisions by 3 users not shown)
Line 1: Line 1:
[[Category:Scripts]]
{{Scripting Index}}
==Usage==
==Usage==


Line 5: Line 7:
==Caveats==  
==Caveats==  


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




<pre>
<syntaxhighlight lang='python'>
#!/usr/bin/env python
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# -*- coding: utf-8 -*-
"""
"""
Format selected paragraphs with bullets.
Format selected paragraphs with bullets.
Line 20: Line 23:
http://meiradarocha.jor.br
http://meiradarocha.jor.br
License GPL 2.0
License GPL 2.0
2011-01-12a
2011-01-14b


======
======
Line 26: Line 29:
Select paragraphs at any points. Run script.
Select paragraphs at any points. Run script.
Caveats:
Caveats:
* Clean all text format of text selection (font, bold, subscript, etc).
* Cleans all text format of text selection (font, bold, subscript, etc).
* Create paragraph style "Bullet", if it doesn't exist.
* Creates paragraph style "Bullet", if it doesn't exist.
* If there is no selection, apply bullets to all text in current frame.
* If there is no selection, apply bullets to all text in current frame.
"""
"""
Line 39: Line 42:
     print "It can only be run from within Scribus."
     print "It can only be run from within Scribus."
     sys.exit(1)
     sys.exit(1)
from scribus import UNIT_MILLIMETERS, BUTTON_OK, ICON_WARNING, mm, pt
from scribus import BUTTON_OK, ICON_WARNING
 
#########################
# YOUR IMPORTS GO HERE  #
#########################


#########
#########
# Locales
# Locales
#########


# Message to ask frame selection
# Message to ask frame selection
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."
wrongFrameTypeErrorMsg = "<h2>Wrong object selected</h2>" \
        +"You must select a <b>text frame</b>."


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


###################
###################
# 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 80: Line 88:
     Text need to be selected.'''
     Text need to be selected.'''
     selectedFrame = scribus.getSelectedObject()
     selectedFrame = scribus.getSelectedObject()
     selectedText = scribus.getText()
     try:
        selectedText = scribus.getText()
    except:
        scribus.messageBox(scriptWindowTitle,wrongFrameTypeErrorMsg,ICON_WARNING,BUTTON_OK)
        sys.exit()
     scribus.deselectAll()
     scribus.deselectAll()
     allText = scribus.getAllText(selectedFrame)
     allText = scribus.getAllText(selectedFrame)
      
      
     ############################################
     if allText.count(unicode(selectedText)) == 1:
    # Weak. Can find wrong text before selection
        cur = allText.find(unicode(selectedText))
    # if selection is short.
        return cur,allText,selectedText
    cur = allText.find(unicode(selectedText))
     else:
    return cur,allText,selectedText
        scribus.messageBox(scriptWindowTitle,askSelectMoreText,ICON_WARNING,BUTTON_OK)
      
        sys.exit(1)
 
###################
def cleanBullets(t):
def cleanBullets(t):
     '''Delete bullets.'''
     '''Delete bullets.'''
Line 97: Line 111:
     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'''
     # Get selected paragraph begin
     # Get selected paragraph begin
     # (last line break position after selection, plus 1)
     # (last line break position after selection, plus 1)
     begin = allText.rfind(u'\r',0,cur)+1 # If '\n'?
     begin = allText.rfind(u'\r',0,cur)+1 # Why not '\n'?
     # Get selection end
     # Get selection end
     end = cur+len(unicode(selectedText))
     end = cur+len(unicode(selectedText))
Line 116: Line 130:
     scribus.deselectAll()
     scribus.deselectAll()
     allText = scribus.getAllText(story)
     allText = scribus.getAllText(story)
    scribus.selectText(begin,len(unicode(cleanedText)),story) 
     # Get selection new end
     # Get selection new end
     end = begin+len(unicode(cleanedText))
     end = begin+len(unicode(cleanedText))
     return begin,end,allText
     return begin,end,allText


############################
# Do all the job
def insertAllBullets(story):
def insertAllBullets(story):
     '''Insert bullets in front selected paragraphs.'''
     '''Insert bullets in front selected paragraphs.'''
Line 138: Line 153:
         begin = nextPoint + bulletsLen + 1
         begin = nextPoint + bulletsLen + 1


#####################
def handleSelected():
def handleSelected():
     """Handle frame selection."""
     """Handle frame selection."""
Line 146: Line 162:
     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 172:
         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:
Line 168: Line 186:
if __name__ == '__main__':
if __name__ == '__main__':
     main_wrapper(sys.argv)
     main_wrapper(sys.argv)
</pre>
</syntaxhighlight>

Latest revision as of 02:13, 20 January 2014

This article is part of the Scripts series.

Usage

Select paragraphs at any points. Run script.

Caveats

  • Weak insertion point algorithm. Select unique text.
  • Script cleans 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, script 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-14b

======
Usage:
Select paragraphs at any points. Run script.
Caveats:
* Cleans all text format of text selection (font, bold, subscript, etc).
* Creates 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

#########
# 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."
wrongFrameTypeErrorMsg = "<h2>Wrong object selected</h2>" \
        +"You must select a <b>text frame</b>."

#######################
# Change this variables
# to match your needs
#######################
bulletStyle = 'Bullet'
# Put your favorite unicode char and tab: '\u2022\t' 
# Get hex code from "Insert > Special character..." palette
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()
    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)

###################
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 # 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)
    # Get selection new end
    end = begin+len(unicode(cleanedText))
    return begin,end,allText

############################
# Do all the 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: 
        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)