A few more simple TextExpander snippets

Here’s another simple but useful TextExpander snippet library. It’s called “Numeric” and it’s what I use to insert fractions and other numeric and sort-of-numeric symbols. You can download it from here.

The symbols and their abbreviations are as follows:

To insert Type
;euro
£ ;pound
¢ ;cent
;ft
;in
½ ;1/2
¼ ;1/4
¾ ;3/4
;1/8
;3/8
;5/8
;7/8
× ;times
÷ ;divide

The abbreviations follow my usual pattern of starting with a semicolon. If you use a different abbreviation prefix, you can use my teprefix script to change the prefix to whatever you like.

(There’s a half-bright group of people who know that curly quotes (’ and ”) are wrong for feet and inches but seem to think that straight quotes (' and ") are OK. They aren’t. Foot and inch marks—which can also be used for minutes and seconds in angular measures—have their own symbols.)

Speaking of teprefix, I’ve added a new script to my tedist GitHub library. It’s called tetable, and it reads a TextExpander library file (a file with a .textexpander extension) and prints out a table of the library’s snippets and abbreviations. Depending on the option you give it, tetable will produce either a Markdown, tab-separated, or HTML table. I used it to make the table in this post.

Here’s the source code for tetable:

python:
 1:  #!/usr/bin/python
 2:  # encoding: utf-8
 3:  
 4:  """
 5:  Read a .textexpander snippet file and generate a Markdown table
 6:  that describes its contents. Useful for short snippets only.
 7:  """
 8:  
 9:  import plistlib
10:  import sys
11:  import os.path
12:  import getopt
13:  
14:  usage = '''Usage: tetable [option] tefile
15:  
16:  Create a table that describes a TextExpander library.
17:  
18:  Options:
19:    -m, --markdown        Markdown table (default)
20:    -t, --tab             Tab-separated table
21:    -w, --web             HTML table
22:    -h, --help            Show this usage message.
23:  '''
24:  
25:  # Get the arguments from the command line.
26:  try:
27:    optlist, args = getopt.getopt(sys.argv[1:], 'hmtw', ['help', 'markdown', 'tab', 'web'])
28:  except getopt.GetoptError, err:
29:    print str(err)
30:    print usage
31:    sys.exit(2)
32:  
33:  # Process the options.
34:  tableType = 'markdown'
35:  for o, a in optlist:
36:    if o in ('-t', '--tab'):
37:      tableType = 'tab'
38:    elif o in ('-w', '--web'):
39:      tableType = 'web'
40:    elif o in ('-m', '--markdown'): 
41:      tableType = 'markdown'
42:    else:
43:      print usage
44:      sys.exit()
45:  
46:  # Extract folder and filename.
47:  try:
48:    infile = args[0]
49:    basename, extension = os.path.splitext(infile)
50:  except IndexError:
51:    print "No input filename."
52:    print usage
53:    sys.exit()
54:  
55:  # Make sure it's a TextExpander file.
56:  if extension != '.textexpander':
57:      print("%s is not a TextExpander file." % infile)
58:      sys.exit(-1)
59:  
60:  # Parse the snippet file
61:  try:
62:      te = plistlib.readPlist(infile)
63:  except IOError:
64:      print("Couldn't open %s to read from." % infile)
65:      sys.exit(-1)
66:  
67:  # The table header.
68:  header = {'tab': 'To insert\tType',
69:            'web': '<table>\n<tr><th>To insert</th><th>Type</th></tr>',
70:            'markdown':'| To insert | Type |\n|:-----:|:----:|'}
71:  # The row templates
72:  row = {'tab': '%s\t%s',
73:         'web': '<tr><td>%s</td><td>%s</td></tr>',
74:         'markdown': '| %s | %s |'}
75:  
76:  # The table footer.
77:  footer = {'tab': '',
78:            'web': '</table>',
79:            'markdown': ''}
80:  
81:  # Print the table.
82:  print header[tableType]
83:  for i in range(len(te['snippetsTE2'])):
84:      print row[tableType] % (te['snippetsTE2'][i]['plainText'], te['snippetsTE2'][i]['abbreviation'])
85:  print footer[tableType]

Because of how I work, I made Markdown the default output format, but you could change that by changing Line 34.

I’m not sure how useful this script would be to everyday TextExpander users, but it’s really handy for anyone who’s writing about a TextExpander library.