Turn and face the strange changes

After writing yesterday’s post, I kept thinking I could’ve done a better job with the script. Here it is again:

python:
 1:  #!/usr/bin/python
 2:  
 3:  from sys import stdin
 4:  
 5:  oldDirs = "N NE E SE S SW W NW".split()
 6:  newDirs = "E SE S SW W NW N NE".split()
 7:  changeDir = dict(zip(oldDirs, newDirs))
 8:  
 9:  captions = stdin.readlines()
10:  for c in captions:
11:    c = c.rstrip()
12:    fields = c.split('|')
13:    try:
14:      words = set(fields[2].split())
15:      dir = words.intersection(oldDirs).pop()
16:      f2 = fields[2].replace(dir, changeDir[dir])
17:      fields[2] = f2
18:    except IndexError:
19:      pass
20:    print "|".join(fields)

Even though the important part was in Lines 5–7—the part that changed the directions—I spent most of my time writing Lines 10–19—the part that split up the lines and hunted for the directions. Wouldn’t it have been more efficient, I wondered, if I had written a simpler script and used BBEdit’s Text Filters system to make the changes? I was pretty sure the answer was yes.

Here’s a much simpler script. It assumes that standard input consists entirely of the direction and writes the corrected direction to standard output.

python:
 1:  #!/usr/bin/python
 2:  
 3:  from sys import stdin, stdout
 4:  
 5:  oldDirs = "N NE E SE S SW W NW".split()
 6:  newDirs = "E SE S SW W NW N NE".split()
 7:  changeDir = dict(zip(oldDirs, newDirs))
 8:  
 9:  dir = stdin.read()
10:  if dir in oldDirs:
11:    stdout.write(changeDir[dir])
12:  else:
13:    stdout.write(dir)

This may not look like a significantly simpler script because it’s only seven lines shorter, but it took way less thought to write. Lines 9–13 pretty much wrote themselves.

If I save this script as Change Direction.py in BBEdit’s Text Filters folder, it becomes available as the Text‣Text Filters‣Change Direction menu item and can be assigned a keyboard shortcut. Then all I have to do is go through the file of photo descriptions, which has lines that look like this,

Photo001|Laundry Room|looking NW
Photo002|Laundry Room|looking NW
Photo003|Storage Room|looking W
Photo004|Storage Room|looking SW ?
Photo005|Laundry Room|looking N
Photo006|Hallway 1|looking W
Photo007|Hallway 1|looking SW
Photo008|Hallway 1|looking W
Photo009|Boiler Room|looking N
Photo010|Boiler Room|looking NW
Photo011|Electrical Room
Photo012|Electrical Room
Photo013
Photo014

select each direction in turn, and type the keyboard shortcut. Although the amount of manual work would be greater, the overall effort would be less because the script would’ve taken less time to write.

Of course, this does me no immediate good, as the directions in the photo description file have already been corrected, but maybe I’ll remember this technique the next time I have to do something similar.

Actually, I’d already done something sort of like this with Keyboard Maestro. When testing web-based APIs, I often find myself needing to percent encode URLs. I’ve found the easiest way to do this is to put the unencoded URL on the clipboard and run this KM macro:

Keyboard Maestro encoding macro

The macro saves the clipboard into the variable unencoded and runs this Python script:

#!/usr/bin/python
from os import environ
from urllib import quote
print quote(environ['KMVAR_unencoded'], '')

Keyboard Maestro saves user-defined variables in environment variables with a KMVAR_ prefix, which makes them accessible to shell scripts. The Python script accesses unencoded through this mechanism and encodes it via the quote function from the urllib module. Keyboard Maestro then strips the trailing newline and pastes the result.

I use it by selecting and copying a URL, often from Safari’s location field, and then hitting ⌃⌥⌘F11 wherever I need to place the encoded string, usually somewhere in a curl command in the Terminal. It changes things like

http://www.leancrew.com/all-this/2015/01/a-time-to-gain-a-time-to-lose/

into

http%3A%2F%2Fwww.leancrew.com%2Fall-this%2F2015%2F01%2Fa-time-to-gain-a-time-to-lose%2F

If you don’t have Keyboard Maestro, you could use Automator to create a Service that works basically the same as my KM macro.

I could turn this into a BBEdit Text Filter, too, and save the step of copying the URL to the clipboard, but then I’d be restricted to using it in BBEdit only. The extra step is worth it for the extra flexibility.


  1. That’s what Keyboard Maestro says, but because I have the function keys set to act as special keys—brightness control, volume control, etc.—I also have to press the fn key. This is actually pretty easy because it’s right in line with ⌃, ⌥, and ⌘.