Saturday, June 13, 2015

Beautify javascript on your clipboard on osx with a keyboard shortcut

Working with people's websites, it's fairly common to want to know what a minified script on the page is doing. Usually, this involves copying the script, pasting it into a minifier, copying the result, and pasting it again.

To beautify the contents of the clipboard using a keyboard shortcut, you'll need to install a command line minifier, set up an Automator service that uses the minifier, and assign it a keyboard shortcut.

The following instructions are based on osx 10.10 with pip already installed.

Install the minifier

You can use whichever minifier you want, as long as it takes input from stdin. I use js-beautify. The easiest way to install it is pip install js-beautify.

To minify code provided to stdin, run it like this:

$ echo "function(  ) { return   3;  }" | js-beautify --stdin

which returns

function() {
    return 3;
}

Minify clipboard contents

To minify the contents of the clipboard, you can use the pbpaste and pbcopy commands.

pbpaste | js-beautify --stdin | pbcopy

Create an Automator service

Open Automator, and you'll be prompted with these choices. Choose "Service".


Now, add an action that runs a shell script, set it up to receive "no input" in "any application" and add the command that minifies the contents of the clipboard.


Copy some minified javascript to the clipboard, and click "Run" in the upper right corner to test the script out. You'll probably get an error telling you that the js-beautify command isn't recognized. You need to find the path to the js-beautify command, and include it explicitly.

Scripts that are installed with python packages are installed in the same folder as the python executable, by default. That folder can be found by starting up python and checking sys.executable.

$ python
>>> import sys
>>> sys.executable
'//anaconda/bin/python'

This tells me that the script is located at anaconda/bin/js-beautify. If you use your system's default python installation, it will be different.

Update the Automation path command to include the full path to js-beautify, and the service should work.

pbpaste | /anaconda/bin/js-beautify --stdin | pbcopy

Save the service with a meaningful name. You'll use the name to set up a keyboard shortcut.

Set up a keyboard shortcut

At this point, your service is available in all applications' menus (e.g. Chrome > Services > my javascript beautifier), but can't be run using a keyboard shortcutt.

Open up system preferences, and go to the "Shortcuts" tab of the "Keyboard" section. Select "Services" from the list on the left, and scroll to the bottom, where you should see all user-created services. Assign your clipboard beautifier a shortcut, and you're done!


I went with control-option-command-B, since it's unlikely to clash with any other keyboard shortcuts, it's easy to press all of the buttons at once, and B stands for Beautify.

Sunday, August 17, 2014

Project Euler (problem 1)

I just discovered Project Euler and I thought I'd start going through the problems, in order, to keep up my math and programming knowledge. Here's the text of the first problem:
If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.
Find the sum of all the multiples of 3 or 5 below 1000.
There seem to be two straightforward ways to solve this problem. You could write a program that goes through every integer below 1000, adding the ones that are a multiple of 3 or 5. You could also come up with an expression for the sum, and then carry out the necessary calculations by hand. I'll try both methods here.

First, the program, since I think it will be faster for this problem. This code was written in an IPython notebook.
sum = 0
for i in range(1000):
    if (i % 3 == 0) or (i % 5 == 0):
        sum += i
print sum
Then, by "hand": The solution can be found using fewer operations by applying some logic. We want to sum every multiple of 3 or 5. We can do this by summing the multiples of 3, adding the sum of the multiples of 5, and then subtracting the sum of the multiples of 15, since multiples of 15 would be counted twice otherwise.

1000/3 is 333 remainder 1. This means that the sum from threes is 3 * (1 + 2 + ... + 332 + 333). Similarly, the sum from the fives is 5 * (1 + 2 + ... + 198 + 199). The sum that needs to be subtracted because of the doubly-counted fifteens is 15 * (1 + 2 + ... + 65 + 66). Adding together the first two expressions and subtracting the third gives:

-7 * (1 + 2 + ... + 65 + 66) + 8 * (67 + 68 + ... + 198 + 199) + 3 * (200 + 201 + ... + 332 + 333)

The first set of parentheses contains 66 terms. It's apparent that they can be simplified: 1 + 66 = 67, 2 + 65 = 67, and so on, until 33 + 34 = 67. Therefore, the value contained is just 33 * 67. A similar approach can be used to simplify the other sums in parentheses, giving:

-7 * (33 * 67) + 8 * (66.5 * 266) + 3 * (67 * 533)

This result matches the brute force method, so everything worked out.

Most of the problems in Project Euler can be easily brute-forced with a fairly simple program, but by coming up with better algorithms, every problem should be solvable in under 1 second on an everyday computer. This encourages the use of elegant solutions that save computer time, and provides a more interesting challenge.

Friday, May 23, 2014

First Post

I'll use this to keep track of the things I'm working on. I just bought an arduino after thinking about it for years, and right before I'm supposed to graduate. I probably won't get much homework done for the last couple weeks of the quarter. Anyway, I'm still at school and don't have many electronic components. Here's my first project.

A blinking LED to simulate a heartbeat. All I needed was a resistor and an LED. Here's what it looks like in operation. It's kind of hard to see in the video, so try it yourself if you can


Here's the code. If you know which COMS port your arduino is on, and you have the codebender plugin installed, you should be able to load it directly onto your arduino from this page.


Some interesting things to note are that the LED's brightness is very nonlinear with pwm value. Changes on the low end of the scale have a much larger effect than changes on the high end. The difference between 5 and 6 (out of 255) is much larger than the difference between 200 and 255, at least to our eyes. Also, to make the pulsing look like something living, it has to be pretty different from an actual heartbeat. Much slower, and smoothed out. It just doesn't seem right otherwise.