From Scribus Wiki
Jump to navigation Jump to search
This article is part of the Scripts series.

Creating en and em dashes from typewriter hyphens

This script is a stripped down and modified version of Autoquote.py. It might be more accurate to say that it was inspired by Autoquote.py, since there is hardly anything left of the original in the script you see below.

Instead of converting typewriter quotes to typographic quotes, here we want to set up a system so that we can type and enter '-' (a single hyphen) when we just want a hyphen, '--' (2 hyphens) when we want an en-dash, and '---' (3 hyphens) when we want an em-dash. If you have something like '----' (4 hyphens) you'll probably end up with an em-dash followed by a hyphen.

There are no dialogs except for the message box at the end.


#!/usr/bin/env python
# -*- coding: utf-8 -*-
# File: en+emdash.py - convert hyphens to en and em dashes
# © 2014.04.27 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.
You must have a document open, and a text frame selected.
There are no dialogs. The presumption is that you have encoded a single
hyphen to mean a single hyphen, two hyphens to mean an en-dash, and three
hyphens to mean an em-dash.
import scribus
if scribus.haveDoc():
    c = 0
    scribus.messageBox('Usage Error', 'You need a Document open', icon=scribus.ICON_NONE, button1=scribus.BUTTON_OK)
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)
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)
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)
contents = scribus.getTextLength(textbox)
ndash = u"\u2013"
mdash = u"\u2014"
prevchar = ''
while 1:
    if ((c == contents) or (c > contents)): break
    if ((c + 1) > contents - 1):
        nextchar = ' '
        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 == '-'):
	if (nextchar == '-'):
	  scribus.selectText(c-1, 3, textbox)
	  scribus.insertText(mdash, c-1, textbox)
	  char = mdash
	  scribus.selectText(c-1, 2, textbox)
	  scribus.insertText(ndash, c-1, textbox)
	  char = ndash
	c += 1

	c += 1
    prevchar = char
    contents = scribus.getTextLength(textbox)
scribus.messageBox("Finished", "That should do it!",icon=scribus.ICON_NONE,button1=scribus.BUTTON_OK)

Usage of en dashes and em dashes

There are many resources on usage of dashes. This Wikipedia article is a reasonable resource, since it gives you a number of references so that you can get a broad sense of usage.

What you will find is that different publications or authorities have somewhat varying recommendations. In particular, there are differences between what is recommended in American versus British sources. To some extent, all agree that you should avoid overusage, and in addition use them in a consistent way.