Compress context menu item for Windows XP

Just thought I’d share a quick tip. I’m one of the few developers I know who uses Windows almost exclusively (sorry, no Mac here). Lately, I’ve been wanting to see how my JavaScript files would be compressed using Julien’s YUI Compressor. Previously, I was keeping a command window open and typing the command in directly. I figured there must be a simpler way to do this, and I was right.

The first step is download the YUI Compressor and put it somewhere handy. I put it directly in the c: directory for easy access. Then, I created a simple batch file called compress.bat and also placed it in c: for each access. The content of the batch file are as follows:

java -jar c:yuicompressor-2.1.2.jar %1

This line essentially runs the YUI Compressor against a file that is specified as the first command-line argument (%1). You can now simply type the following into a command window:

c:>compress.bat myfile.js

To take this one step further, you can add a “Compress” context menu item. To do so, follow these steps:

  1. Open an Explorer window.
  2. Under the Tools menu, click Folder Options.
  3. Click on the File Types tab.
  4. In the list, select “JS” and click the Advanced button.
  5. Click the New… button.
  6. In the Action field, type “Compress”.
  7. In the Application used to perform field, type “c:compress.bat “%1″”.
  8. Click OK.
  9. Click OK again.
  10. Click Close.

Once that is done, you can right-click on any file ending with a .js extension and see a context menu item called “Compress”. When clicked, the command runs the YUI Compressor and the output is placed in the same directory.

I’ve found this to be a very handy way to use the YUI Compressor. I hope it’s equally as useful for you.

Understanding JavaScript Promises E-book Cover

Demystify JavaScript promises with the e-book that explains not just concepts, but also real-world uses of promises.

Download the Free E-book!

The community edition of Understanding JavaScript Promises is a free download that arrives in minutes.