is now part of CreativePro.com!

Fixing a Broken JavaScript (and How To Avoid Breaking It in the First Place)

First, I freely admit this post might come across like an ad for Peter Kahrel’s book, JavaScript for InDesign. It also happens to be 100% true. And I’m writing it not because I felt the need to sell some books but because as a non-scripter, I was so excited that I actually took a broken script and got it working that I had to share the story. Plus, I know that someone somewhere will benefit by me sharing it. As a bonus, I’ll also share how I broke the script in the first place as a cautionary tale. So here goes.
Recently, I came across the topic of sizing text characters to exact physical heights, something that requires a bit more effort than you might expect—or a script. Specifically, a script written by Gerald Singlemann, and linked in this post. The script was “ancient” having been written over a decade ago, but I was willing to see if it still worked in InDesign 2020.
So I downloaded it, installed it, and ran it.
And I got an error.

Not being someone who understands JavaScript, I was a bit tempted to give up at this point. But the error message offers some important clues. It gave me the exact location of the problem, with the line number (20), source, and the offending character, in this case an exclamation point.

I know an exclamation point is used to represent “not” in code, and it made sense in context that the documents length would have to not be zero in order for the script to proceed. But in order to fix it, I needed to know how to write “is not equal” correctly. For that, I opened a copy of Javascript for InDesign and searched. I quickly found what I needed, with an example of what the code should look like.


I saw right away that the exclamation point was simply in the wrong place. It needed to come right before the equals sign. So I moved it, saved the file, re-ran the script…and got another error.

It was the same error, but at least it was on a different line number (99).

So the script had gotten past the line I fixed. Hooray for small victories! So I repeated the same process of tweaking the code, saving, and re-running the script. And this time it worked!

Just having Peter’s book made all the difference. First, it gave me the confidence to try, even as a scripting ignoramus. And then I found the answer very quickly, just by searching. Of course I did not learn how to write code or troubleshoot it like an expert would. But the fact is, in 5 minutes I went from having a broken script to a working one, with the help of the book. So I think this little episode was instructive because it proved that to accomplish an otherwise impossible task, sometimes all you need is a little guidance and the willingness to give it a shot.

How to Break a JavaScript Without Even Trying

Here’s how I got into trouble in the first place. The link to Gerald’s script in the blog post was to the actual JavaScript file, not a ZIP file. So when I clicked on it, it opened in the browser I was using (Chrome). I thought, no big deal, I’ll just copy all the text into a new file and save it with a .JSX extension. Turns out to be a very big deal because Chrome decided to autocorrect what it thought were typos in the text! This included putting those “not” exclamation points directly after the text that preceded them, breaking the script. It all happened in the blink of an eye, so I didn’t notice it until I retraced my steps to write this post. So, boys and girls, if you’re trying to download a script and the code opens in the browser window, don’t copy and paste! Instead, go back and right-click on the link and choose Save Link As.

That way, you can save it in the correct format and not have to worry that it got mangled somewhere along the way to your computer.

Editor in Chief of CreativePro and InDesign Magazine. Instructor at LinkedIn Learning with courses on InDesign, Illustrator, GIMP, Inkscape, and Affinity Publisher.
>