Recursive jot

Today I had to break up a giant PDF full of scanned drawings for a building, a job similar to that described in this post. So I opened the post and started following the steps. It worked well, but this time I added a twist: using jot to create a set of jot commands.

After using pdftk to burst the PDF into individual files and using sips to convert them into JPEGs, I had a set of over 300 JPEG files with names like pg_0001.jpg. I wanted to rename them to roughly match the drawing numbers given in the title blocks.

These were all architectural drawings (as opposed to structural, mechanical, electrical, etc. drawings), so they all had drawing numbers like A1.01, A5.14, and A11.3, where the number before the period indicates a section. There are sections of elevation drawings, sections of plan drawings, sections of details, and so on. Sixteen sections in all.

Following the steps of my earlier post, I wanted to create a file called names.txt that would contain 300+ lines, one for each of the files, giving the name I wanted to change it to. The shell script to create that file was this:

bash:
 1:  jot -w 'A0-%02d.jpg' 1 > names.txt
 2:  jot -w 'A1-%02d.jpg' 4 >> names.txt
 3:  jot -w 'A2-%02d.jpg' 12 >> names.txt
 4:  jot -w 'A3-%02d.jpg' 18 >> names.txt
 5:  jot -w 'A4-%02d.jpg' 41 >> names.txt
 6:  jot -w 'A5-%02d.jpg' 87 >> names.txt
 7:  jot -w 'A6-%02d.jpg' 15 >> names.txt
 8:  jot -w 'A7-%02d.jpg' 18 >> names.txt
 9:  jot -w 'A8-%02d.jpg' 4 >> names.txt
10:  jot -w 'A9-%02d.jpg' 18 >> names.txt
11:  jot -w 'A10-%02d.jpg' 17 >> names.txt
12:  jot -w 'A11-%02d.jpg' 3 >> names.txt
13:  jot -w 'A12-%02d.jpg' 22 >> names.txt
14:  jot -w 'A13-%02d.jpg' 3 >> names.txt
15:  jot -w 'A14-%02d.jpg' 3 >> names.txt
16:  jot -w 'A15-%02d.jpg' 56 >> names.txt
17:  jot -w 'A16-%02d.jpg' 4 >> names.txt

(The A0 section consisted of just the index of all the other drawings. It didn’t have a drawing number, so I decided to call it A0.01. The numbers just before the redirection operators represent the number of drawings in each section.

I didn’t want to write this script. Even though it’s only 17 lines long, and each line could be entered by repeatedly pasting and editing, the repetition would’ve driven me crazy. And I would have made mistakes. Editing the number of drawings for each section would be easy, because I could double-click on the default number and then type the correct value. But changing the section number would require either precision clicking and dragging or a lot of cursor movements that I was likely to screw up.

I thought it would be fun to use jot itself to generate the lines. It wasn’t hard. This command,

jot -w "jot -w 'A%d-%%02d.jpg' 000 >> names.txt" 17 0 > jotter.sh

created the script, called jotter.sh. I used 000 as the default number of drawings for each section because it made for a big double-click target when I edited jotter.sh to put in the correct numbers.1

Yes, I could have used Keyboard Maestro to automate the creation of these lines, and I could have even had it prompt me for the number of drawings in each section. But how could I resist the opportunity to use a command to create a bunch of copies of itself?2


  1. I also changed the first append redirection (>>) to a create redirection (>). That wasn’t necessary, since names.txt didn’t exist yet, but it made for a cleaner script. 

  2. Yes, I know this isn’t really recursion, but I liked the title. If you tweet me to explain recursion, I will block you.