Archive for September, 2009

Typeset (La)TeX and open pdf with one line command from vi/vim (or Emacs, etc.)

Tuesday, September 29th, 2009

I habitually use vim to edit files, particularly latex code. Ashamedly I only recently realized you could make shell calls without leaving vim just by typing


When I’m editing a TeX file I am constantly typesetting my document to a pdf then opening the pdf. Here’s a one-line vi(m) specific bash command to typeset your current document to a pdf then open that pdf using your default pdf viewer.

:!if pdflatex "%"; then open "`echo "%" | sed "s/\(\.[^\.]*\)$/.pdf/g"`"; fi

If you do not have or want to use the open command just replace it with some other viewer like ghostview or xpdf.

Note: Here’s the multi-line version of the exact same thing if you want to save it in a callable bash script:

if pdflatex $1; then
  open `echo $1 | sed "s/\(\.[^\.]*\)$/.pdf/g"`

If you save the above in an executable file called openpdflatex then you could still call it from vi or vim with

:!openpdflatex %

Update: Occasionally I include .eps figures so I need to first make a .dvi with the latex command then use dvipdf to convert to pdf. Here’s how I do it in one line from vi(m):

:!if latex %; then if dvipdf `echo % | sed "s/\(\.[^\.]*\)$/.dvi/g"`; then open `echo % | sed "s/\(\.[^\.]*\)$/.pdf/g"`; fi; fi

Update: All these if statements can be condensed using the && bash operator. Making these much easier to just type out whenever, wherever you need them. Like this:

:!pdflatex % && open `echo % | sed -e "s/tex$/pdf/"`

Remove all duplicate songs/mp3s iTunes creates using bash script

Tuesday, September 22nd, 2009

I gave up using iTunes to play music about year ago, but I haven’t found a free alternative to iTunes’s exceptional file management based on mp3 ID3 tags (If you know of one — I mean better than iTunes one — let me know). So occasionally I let iTunes organize my music library. I drop in folders containing new music and let iTunes go at it. The problem is that if I have folders containing an mp3 and an m3u playlist I get duplicates. If I don’t notice this right away the duplicates build up.

Here’s a bash script to delete all true duplicates. The files must be exactly the same and have almost the same name (the difference being the number iTunes appends on a copy: “song.mp3” becomes “song 1.mp3”).

Verbose version:

find "$1" -regex ".* [0-9][0-9]*\.[^.]*" -print0 | while read -d $'\0' copy
  original=`echo "$copy" | sed "s/\(.*\) [0-9][0-9]*\(\.[^.]*\)/\1\2/"`
  # check that  the hypothetical original exists
  if [ -e "$original" ];then
    # check that the original is exactly the same file as the copy
    if diff "$original" "$copy" >/dev/null ;then
      rm "$copy"
      echo "$copy deleted..."
      echo "$copy is different..."
      echo "  $copy not deleted..."
    echo "$original does not exist..."
    echo "  $copy not deleted..."

Quiet Version

find "$1" -regex ".* [0-9][0-9]*\.[^.]*" -print0 | while read -d $'\0' copy
  original=`echo "$copy" | sed "s/\(.*\) [0-9][0-9]*\(\.[^.]*\)/\1\2/"`
  # check that  the hypothetical original exists
  if [ -e "$original" ];then
    # check that the original is exactly the same file as the copy
    if diff "$original" "$copy" >/dev/null ;then
      rm "$copy"

Sticky or wobbly space bar on old, white apple keyboard (not working/broken)

Friday, September 18th, 2009

I recently posted about setting up an (old, white, wireless, bluetooth) apple keyboard.

old white apple keyboard

When I bought this keyboard used, I actually had a problem that I’d had with a previous apple keyboard of the same style. In the past occasion I believe now that I accidentally solved the problem. Now I have found the solution.

The problem is that the space bar is “sticky”. When you press it right in the center it works fine but if you hit on either the far right or left side it stays stuck in or at least feels like it’s clicking into place rather than smoothly pressing and releasing.

I tried cleaning it and blowing in it to no avail. Finally I figured out what was going on. If you pop off the space bar (just pull it off gently holding both left and right sides), you will see that there is a little metal bar attached underneath. This bar needs to be attached with the long part of the bar hooked into the space bar itself and the two short ends attached to the keyboard. When aligning to attach the space bar to the keyboard, first attach the long part of the bar to the space bar. Hold the space bar so that when it is attached the part where the long bar meets the actual space bar is at the top (far side from you), with the little ends of the bar pointing at you.

Here’s a video to explain it better:

How much did that meal cost web app

Thursday, September 17th, 2009

I recently developed a ruby web application to tell you how much you spent on a given recipe. Enter in how much you spent for each item and how much you bought and how much you used. Then the app will figure out how much you ate in dollars.

I will post the code soon.

Coining “to coin a phrase”

Sunday, September 13th, 2009

I’m writing this now to be sure that I am the first to coin the phrase, “To coin a phrase you just have to be the first to say it on the internet”.

“See” shell scripts in action with applescript

Sunday, September 13th, 2009

Here’s a simple script to open and execute a shell script.

tell application "Terminal"
	do script "ls"
end tell

Note: Note the subtle difference from another applescript feature you might be used to, namely do shell script. The latter, do shell script, when executed anywhere in an applescript runs a script at root level (but without root permissions). So running a script like:

set contents to do shell script "ls"

The variable contents will hold the return of ls for the root directory, /. On the other had something like the code at the top will execute ls at the default location of a new Terminal window, which (unless you have changed it) is by default the home directory of the current user. So in a Terminal tell block,

do script "ls"

opens a new terminal window and executes ls in that window, presumably at the user’s home directory, ~.

MacBook Pro won’t pair with old, white apple wireless bluetooth keyboard

Saturday, September 12th, 2009

Recently purchased a used wireless apple keyboard (the old white bluetooth ones).

Old white wireless apple keyboard

I had a lot of trouble pairing it with my macbook pro, running mac os x 10.5 leopard. Using the Bluetooth Setup Assistant, I would hit a wall. It seemed that it was able to “see” my keyboard. It showed up as “found” but then when I chose to pair with it, I got stuck with the message: ‘The pairing attempt was unsuccessful. Make sure your keyboard is in range of this computer, turn on and “discoverable.” When ready, click Continue and try again.’ And beneath that “Unable to pair with your keyboard. Click Continue to try again.”

Unable to pair with keyboard

Continuing to try again was useless, so instead I went back to the “Select Device Type” screen of the assistant. Instead of choosing “Keyboard”, I chose “Any device”. It saw my keyboard, but before selecting it I checked under “Passkey options…” to make sure “Use specified passkey” was checked. Then after I selected the keyboard, it sat on “Gathering additional information about…” but eventually finished. I was prompted for a pass key, which I entered and then viola the keyboard was recognized.

Hosting a ruby on rails application on nyu server

Friday, September 11th, 2009

Since setting up a ruby on rails app bluehost is such a disastrous hurdle, I’ve resorted to hosting one of my projects locally on a nyu cims linserv machine and then using ports to access it via the internet.

This is a basically reiterating the information found on the cims

ssh into


ssh again into a server (must be done after ssh-ing into there are three: linserv1, linserv2, and linserv3.


cd into your rails app, something like

cd rails/app_name/

start up your local webbrick server as a daemon sending all output to

script/server -d -p [portnumber] > /dev/null

where portnumber is some number like 25000, but not 25000 because two
apps can’t use the same port. So pick an unusual and high number and don’t tell anybody what it is.

You will now be able to access your rails app at[portnumber]/

When you’re done with your app you will need to kill it. Follow the
instructions I have previously posted about.

I’ve noticed that occasionally these servers “go down”, or at least my
webrick rails app “goes down”, just repeat the steps and restart the
server if this happens.

Security bug in Mac OS X 10.5 Airport: use Airport to retrieve username and password

Wednesday, September 9th, 2009

New solution below

I noticed when I wake up my MacBook Pro running Mac OS X 10.5 from sleep it tries to connect to a preferred wireless network. On failure it presents me with a list:

List of wireless networks

Airport has remembered passwords to networks to which I have previously connected. Even my university’s NYU-ROAM2 which uses LEAP has a remembered password (the LEAP username and password are actually remembered together as a WEP password of the form <username/password>).

AirPort reveals previous network's password

By selecting a protected network then checking “Show password” I can not only see passwords to previous wireless networks, but if I select some LEAP protected wireless network like NYU-ROAM2 I can see my username and password! This means anyone with access to your laptop could potential get your username and password, which in turn could allow the fiend to access other private information stored elsewhere using the same username and password.

I guess the lesson is to require a password upon waking up. You can do this by going to System Preferences > Security then making sure “Require password to wake this computer from sleep or screen saver” is checked.

Security preference pane

Though it does seem odd for AirPort to show these passwords without prompting at least for the current user’s password.

Update: It looks like just adding a prompt for the computer’s password at wake up is not enough. A user can bring up the list of previous wireless networks (and the ability to see remembered passwords) just by turning on and off AirPort. Any ideas for solutions to this problem?

New (stronger) solution:
Adding a password on wake up is not quite good enough. Mac does let you (as of some security update?) require administrative privileges to modify airport settings (turn ON of OFF, change/create networks).

Open Network Preferences (either through System Preferences) or by clicking on the airport symbol:
open network preferences using airport icon

In Network Preferences select AirPort on the left and click “Advanced”:
advanced button in network preferences

Under the (default) Airport tab in the advanced window, make sure “Require Administrator password to control AirPort” is selected.
require administrator password to control airport

Be sure to finish by applying the changes.

Crunk hunting gear

Tuesday, September 8th, 2009

I am going to begin producing lil jon-inspired crunk-themed deer, duck and quail hunting gear. Leather jackets will come in blaze orange with bedazzled accessories like a 100-ct platinum duck call or tree stand with cup-holders shaped specifically for purple drank.. Here’s my first promo image:
blaze orange crunk jackets