Double2emspace.py: Difference between revisions
Jump to navigation
Jump to search
(Created page with "Category:Scripts {{Scripting Index}} ==double2emspace.py==") |
|||
Line 2: | Line 2: | ||
{{Scripting Index}} | {{Scripting Index}} | ||
==double2emspace.py== | ==double2emspace.py== | ||
<syntaxhighlight lang='python'> | |||
# -*- coding: utf-8 -*- | |||
# File: double2emspace.py - convert double spaces to emspace or single space | |||
# © 2014.11.08 Gregory Pittman | |||
# This program is free software; you can redistribute it and/or modify | |||
# it under the terms of the GNU General Public License as published by | |||
# the Free Software Foundation; either version 2 of the License, or | |||
# (at your option) any later version. | |||
""" | |||
USAGE | |||
You must have a document open, and a text frame selected. | |||
There is one dialog, which asks you if you wish to replace double spaces | |||
with an em space. The default choice is Yes, any other input causes a double | |||
space to convert to a single space. | |||
""" | |||
import scribus | |||
if scribus.haveDoc(): | |||
c = 0 | |||
else: | |||
scribus.messageBox('Usage Error', 'You need a Document open', icon=0, button1=1) | |||
sys.exit(2) | |||
if scribus.selectionCount() == 0: | |||
scribus.messageBox('Scribus - Usage Error', | |||
"There is no object selected.\nPlease select a text frame and try again.", | |||
scribus.ICON_WARNING, scribus.BUTTON_OK) | |||
sys.exit(2) | |||
if scribus.selectionCount() > 1: | |||
scribus.messageBox('Scribus - Usage Error', | |||
"You have more than one object selected.\nPlease select one text frame and try again.", scribus.ICON_WARNING, scribus.BUTTON_OK) | |||
sys.exit(2) | |||
textbox = scribus.getSelectedObject() | |||
pageitems = scribus.getPageItems() | |||
boxcount = 1 | |||
for item in pageitems: | |||
if (item[0] == textbox): | |||
if (item[1] != 4): | |||
scribus.messageBox('Scribus - Usage Error', "This is not a textframe. Try again.", scribus.ICON_WARNING, scribus.BUTTON_OK) | |||
sys.exit(2) | |||
contents = scribus.getTextLength(textbox) | |||
emspace = u"\u2003" | |||
replace_char = " " | |||
selection = scribus.valueDialog("Replace Character", 'Replace with em space?\n Anything but Yes will use normal space', 'Yes') | |||
if (selection == 'Yes'): | |||
replace_char = emspace | |||
prevchar = '' | |||
while 1: | |||
if ((c == contents) or (c > contents)): break | |||
if ((c + 1) > contents - 1): | |||
nextchar = 'emspace' | |||
else: | |||
scribus.selectText(c+1, 1, textbox) | |||
nextchar = scribus.getText(textbox) | |||
scribus.selectText(c, 1, textbox) | |||
char = scribus.getText(textbox) | |||
if (len(char) != 1): | |||
c += 1 | |||
if (char == ' '): | |||
if (prevchar == ' '): | |||
scribus.selectText(c-1, 2, textbox) | |||
scribus.deleteText(textbox) | |||
scribus.insertText(replace_char, c-1, textbox) | |||
char = replace_char | |||
else: | |||
c += 1 | |||
else: | |||
c += 1 | |||
prevchar = char | |||
contents = scribus.getTextLength(textbox) | |||
scribus.setRedraw(1) | |||
scribus.docChanged(1) | |||
scribus.messageBox("Finished", "That should do it!",icon=0,button1=1) | |||
</syntaxhighlight> |
Revision as of 02:21, 9 November 2014
double2emspace.py
# -*- coding: utf-8 -*-
# File: double2emspace.py - convert double spaces to emspace or single space
# © 2014.11.08 Gregory Pittman
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
"""
USAGE
You must have a document open, and a text frame selected.
There is one dialog, which asks you if you wish to replace double spaces
with an em space. The default choice is Yes, any other input causes a double
space to convert to a single space.
"""
import scribus
if scribus.haveDoc():
c = 0
else:
scribus.messageBox('Usage Error', 'You need a Document open', icon=0, button1=1)
sys.exit(2)
if scribus.selectionCount() == 0:
scribus.messageBox('Scribus - Usage Error',
"There is no object selected.\nPlease select a text frame and try again.",
scribus.ICON_WARNING, scribus.BUTTON_OK)
sys.exit(2)
if scribus.selectionCount() > 1:
scribus.messageBox('Scribus - Usage Error',
"You have more than one object selected.\nPlease select one text frame and try again.", scribus.ICON_WARNING, scribus.BUTTON_OK)
sys.exit(2)
textbox = scribus.getSelectedObject()
pageitems = scribus.getPageItems()
boxcount = 1
for item in pageitems:
if (item[0] == textbox):
if (item[1] != 4):
scribus.messageBox('Scribus - Usage Error', "This is not a textframe. Try again.", scribus.ICON_WARNING, scribus.BUTTON_OK)
sys.exit(2)
contents = scribus.getTextLength(textbox)
emspace = u"\u2003"
replace_char = " "
selection = scribus.valueDialog("Replace Character", 'Replace with em space?\n Anything but Yes will use normal space', 'Yes')
if (selection == 'Yes'):
replace_char = emspace
prevchar = ''
while 1:
if ((c == contents) or (c > contents)): break
if ((c + 1) > contents - 1):
nextchar = 'emspace'
else:
scribus.selectText(c+1, 1, textbox)
nextchar = scribus.getText(textbox)
scribus.selectText(c, 1, textbox)
char = scribus.getText(textbox)
if (len(char) != 1):
c += 1
if (char == ' '):
if (prevchar == ' '):
scribus.selectText(c-1, 2, textbox)
scribus.deleteText(textbox)
scribus.insertText(replace_char, c-1, textbox)
char = replace_char
else:
c += 1
else:
c += 1
prevchar = char
contents = scribus.getTextLength(textbox)
scribus.setRedraw(1)
scribus.docChanged(1)
scribus.messageBox("Finished", "That should do it!",icon=0,button1=1)