Bullets: Difference between revisions
Jump to navigation
Jump to search
m (→Caveats) |
m (→Caveats: Gave that Python Geishi...Pythons Love Geishi) |
||
(9 intermediate revisions by 3 users not shown) | |||
Line 1: | Line 1: | ||
[[Category:Scripts]] | |||
{{Scripting Index}} | |||
==Usage== | ==Usage== | ||
Line 6: | Line 8: | ||
* Weak insertion point algorithm. '''Select unique text'''. | * 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. | * 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. | ||
< | <syntaxhighlight lang='python'> | ||
#!/usr/bin/env python | #!/usr/bin/env python | ||
# -*- coding: utf-8 -*- | # -*- coding: utf-8 -*- | ||
Line 27: | Line 29: | ||
Select paragraphs at any points. Run script. | Select paragraphs at any points. Run script. | ||
Caveats: | 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. | * If there is no selection, apply bullets to all text in current frame. | ||
""" | """ | ||
Line 41: | Line 43: | ||
sys.exit(1) | sys.exit(1) | ||
from scribus import BUTTON_OK, ICON_WARNING | from scribus import BUTTON_OK, ICON_WARNING | ||
######### | ######### | ||
# Locales | # Locales | ||
######### | |||
# Message to ask frame selection | # Message to ask frame selection | ||
Line 62: | Line 61: | ||
+"You must select a <b>text frame</b>." | +"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' | # Put your favorite unicode char and tab: '\u2022\t' | ||
Line 115: | Line 117: | ||
# 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 # | 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 128: | Line 130: | ||
scribus.deselectAll() | scribus.deselectAll() | ||
allText = scribus.getAllText(story) | allText = scribus.getAllText(story) | ||
# Get selection new end | # Get selection new end | ||
end = begin+len(unicode(cleanedText)) | end = begin+len(unicode(cleanedText)) | ||
Line 134: | Line 135: | ||
############################ | ############################ | ||
# Do the | # Do all the job | ||
def insertAllBullets(story): | def insertAllBullets(story): | ||
'''Insert bullets in front selected paragraphs.''' | '''Insert bullets in front selected paragraphs.''' | ||
Line 185: | Line 186: | ||
if __name__ == '__main__': | if __name__ == '__main__': | ||
main_wrapper(sys.argv) | main_wrapper(sys.argv) | ||
</ | </syntaxhighlight> |
Latest revision as of 02:13, 20 January 2014
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)