2011-07-01

OS X Open With Vim

Edit: After my second OS X post, I went back and created a service to perform Edit with Vim. I actually forgot about the .app that I made with automator. Then I upgraded to Lion and was all sorts of confused when two entries for Edit with Vim popped up in my context menu. Wouldn't you know it? Lion puts all automator Services and Applications into the right click context menu. So I've actually deleted this script and am using the one mentioned here.

OS X comes with vim preinstalled. Nice. It's not that retarded version Ubuntu comes with by default, either. It's the full vim. So then, why the hell can't I right click something, choose open with, and then choose vim? Why can't I pass arguments to the terminal to tell it to run arbitrary commands (such as vim) from the open with dialog?

It appears that OS X only lets you pick Apps as the target application for performing an "Open with". So, to get what I want, I have to use Automator in conjunction with a bash shell script in order to run osascript in order to pop up a new terminal with vim in it and whatever files are selected (or dropped on the app).

Here's what I have in my bash script, aptly named TellTerminal:


This script sends whatever it's arguments are to the Terminal.app and tells it to run them. Then it tells the Terminal.app to activate itself so that it jumps to the foreground. Without this, the terminal will still open, but there's a chance that it will be sitting in the background somewhere. A really, really good chance.

To invoke this script, I had to create an Automator Application (not a workspace, only the .app can be invoked as an application) and add an action 'Run shell script'. This is the shell script that it runs:



This script (which only exists in the Automator app that I've named EditWithVim.app,) loops over all the arguments that were passed to it in order to invoke vim and pass it the quoted file names. This is to avoid having a space within a file name break my script. Also, this script looks for a BashScripts directory in my user's Library directory within the user's home.

Now I can set things to be opened with vim. It's extremely convoluted, but it does seem to be working.

2011-04-20

Thanks, FCC!

Apparently you can just download the FCC database of Ham users. Then you can just grep the list instead of doing something lame like scraping a 3rd party website. Thanks, FCC.

Link here.

2011-04-17

Ham Lookup Bash Script (fragile)

Earlier today I wanted to quickly find some info on some Hams, but I couldn't remember the FCC link. Then I found the QRZ site. Not bad, but I didn't want to have to launch Firefox just to look someone up, so I thought maybe I'd write a python script. Except then I couldn't think of an easy Regex to parse the HTML So I fetched the page source, found the form that looks up names and callsigns, pulled down my own info, and before I knew it, I had a series of ugly greps and seds pulling out the relevant information. Then something strange happened. I lost interest in pursuing the python angle. I dumped my two queries into two separate bash scripts named lookupname and lookupsign. It was quick and painless, even though the "code" is horribly fragile and entirely dependant on the HTML coming back, but it (currently) works so I'm calling it good for now.

A few minutes ago I was sitting on a different computer and wanted to lookup a callsign, again, so I dumped the two separate bash scripts into a single script and I guess I'll share that in case I want it again in the future. Here's the script:

 1 #!/bin/bash
 2
 3 SIGN="`echo $@ | sed 's/\ /+/g'`"
 4
 5 echo "$SIGN" | grep +
 6
 7 if [ "$?" -eq 0 ] ; then
 8         echo "Looking for name $SIGN"
 9         wget --post-data="callsign=$SIGN" http://www.qrz.com/db/ -O - 2>/dev/null | egrep -o '<td class="(rc|ra)">.*</td>' | egrep -o '>(([A-Z]|[a-z]|[0-9])|([A-Z]|[a-z]|[., ]))*<' | sed 's/<//g' | sed 's/>//g' | grep -v '^$'
10 else
11         echo "Looking for callsign $SIGN"
12         wget --post-data="callsign=$1" http://www.qrz.com/db/ -O - 2>/dev/null | egrep '<p>.*</p>' | sed 's/<b.*">//g' | sed 's/<\/b>//g' | sed 's/<p>//g' | sed 's/<\/p>//g'
13
14 fi



Here's the same script. Invoke it with either a callsign or a person's name. Basically, if you want to do a callsign lookup make sure there are no spaces in the invokation, ex: lookupham AB2CDE. If you want to do a name lookup then make sure you provide a first and last or if you're clever make sure there is a space in the parameter list. ex: lookupham John Doe.

2011-03-27

vim + xxd = easy binary patching


I know this is somewhat common knowledge and can be found elsewhere, but it's just so awesome that I have to repost it. I had always used xxd to generate a listing, edited that listing with vim, and then used xxd -r to re-create the binary file. Instead, you can use vim to open the binary file, shell out to xxd using the current buffer as input, modify the binary content by editing the hex digits (that's right, no need to edit the ascii, that's just for show,) and then shell out to xxd with the current buffer again to get the binary representation.

Once more for posterity...

vim -b binaryfile

:%!xxd

Edit the hex column in the center.

:%!xxd -r

Save the file and it's patched.

For some extra goodness (prettier columns):
set syntax=xxd

So, that was pretty cool, but also check out :help xxd. Vim's built-in help has a nice bit of code to put in your .vimrc and then vim -b will automatically handle shelling out to xxd when you open a binary file and then shell out to xxd when you save the modified buffer.

2011-03-15

Revised Rich Header Parser

I've revised the rich.py script. It's much better but doesn't actually do anything more than it used to. I'll be making some more additions in the very near future, but I wanted to post this before I misplaced it.

New script here: rich.py

2011-01-08

Fujitsu Lifebook B3020D Touchscreen and Linux

(I actually did this at the tail end of 2009, then I revised the python script it in 2010. I've been thinking of attacking the problem with un-smooth lines when the cursor is dragged...)

A friend gave me one of these Fujitsu B3020D Lifebooks. It's quite beat up. No, really, the screen is cracked in the lower left corner and a failing hard drive or bad RAM requires that I reboot the thing periodically. It's currently running Windows 7 and Skype with a Zoom adapter so I can plug cordless telephone bases into it. But before that, it was running Ubuntu. It ran Ubuntu for a good week to a week and a half. The touchscreen didn't work on it, and the screen was cracked even then, but it wasn't a bad little system. The battery life was good, the wireless worked without having to fight with it, and I thought it would be fun to reverse engineer the touchscreen and write some kind of mouse driver.

It didn't take me long to see that the touchscreen is actually exposed to the system as a serial device. That's damn cool. If you just start cating out the serial ports at the console and then touching the screen, you'll eventually see it spew a bunch of numbers. That's how I figured out which serial port it was using. After that you can pretty much tell what the values mean just by trial and error. There's a byte that indicates the type of event (push/release/drag) and then a series of bytes indicating the x and y coordinates. I suggest piping the output of cat through xxd in order to see the byte values in hex. For example: cat /dev/ttyS2 | xxd -

I don't recall from memory what the different byte values were, but you can kind of tell from the python script I wrote for it. This script reads the touch screen and will tell X11 to move the mouse around the screen and click the left mouse button in pretty much the way you would expect it. However, if you run the gimp or some other paint program, you'll notice that touching and dragging results in a rather imperfect line. Instead of nice diagonal lines, you get these crazy little semi-circles. If I ever get a second system set up to run my Skype phone (or can borrow another one of these laptops,) I'll revisit the script and see if I can fix it, but otherwise it works okay.

Here's the script via pastebin: touchmy.py

SEN-08634 Magnetic Card Reader/Writer

The SEN-08634 Magnetic Card Reader/Writer is exactly what it sounds like: it reads and writes to magnetic strip cards. SparkFun has them.

This device will connect to your host as a serial device (/dev/ttyUSB0, for example) but if you try to cat it out, it will immediately return. The damn thing doesn't buffer! Well, okay, that's not true, it will buffer, but it throws the buffer away after a very short period of time. So, if you wanted to use the device with some clever bash scripting, you would have to do something like while [ "1" ] ; do echo -n `cat /dev/ttyUSB0` ; done

I didn't actually try that command while I had the device, it didn't occur to me until just now.

Anyway, I borrowed one of these and wrote a python script that can use it. It reads high coercivity cards and reads and writes low coercivity cards, but that's about the only thing that works. For some reason, all the commands that the documentation claims should work don't return anything. My script is heavily commented and I don't recommend simply running it unless you're ready to lose the data on whatever card you swipe through it. The main body of the script will attempt to write hello world to track 1 and then a couple strings of numbers to track 2 and 3. It will not backup what's on the card before writing. That didn't occur to me either. I don't have the device any more, so I'm not going to update the script.

Here's the script via pastebin: magstripe.py.
If you want to see documentation for it, you can do something like this: doxygen -g magstrip.doxy ; doxygen magstrip.doxy

2009-11-13

Diablo 2 Cloner GUI

I threw this together just for practice, though not very good practice since it spits most of the output to the terminal... It uses GtkBuilder under python so it requires the Gtk runtime in addition to python.

This is the file to run: d2gui.py
Here's the file that does the actual work:d2functions.py
And here's the interface xml document: d2gui.glade

Wordpress is no better

Wordpress didn't turn out to be any better. Perhaps if I had my own domain it would be more configurable, but I'm far too lazy for that.

So now I have both blogs and I'll have to make a few more posts here to see which one I want to keep. After that I'll consolidate my posts to one location and tear down the old one.

2008-02-23

So long and thanks for all the fish

Blogger was a pain. I'm still not satisfied with the layout and complete lack of ability to upload files. I'm going to try wordpress for a while. If google ever fixes things, maybe I'll return. Or, if it turns out that WordPress is even worse, I might return.

WordPress Blog