Code


7
May 10

Improving Instapaper in Fever˚ part deux

I recently wrote about improving the Instapaper function in Fever˚.

It occurred to me that a small pop-up window would be much better than having a full-size browser window come up just for a second. Then I wondered if I could put a javascript: URL into the Fever˚ settings. And whaddya know, it works!

The new link I’m using (wrapped for readability):

javascript:window.open(
  'http://mysite.com/instapaper.php?url=%u&title=%t&selection=%e',
  'instapaper',
  'width=300,height=200,toolbar=0,scrollbars=0,resizable=1'
);

Right-click this link to copy it: Instapaper


28
Apr 10

Improving Instapaper support in Fever˚

I recently switched from Google Reader to Fever˚. I am happy I switched, but I quickly missed the ease with which I could save articles to Instapaper. The Instapaper bookmarklet has some way of divining which article you’re focused on in Google Reader, so you can add the article right from the feed page, instead of opening the article itself, then adding it to Instapaper. It’s very convenient, and fast.

Fever˚ also has Instapaper support, but it works a little differently. When focused on an article, you tap ‘i’ to open a new window containing a pre-filled Instapaper form, then click the ‘Add’ button. The item is added, and you’re left with a window showing your Instapaper unread items.

That’s not bad, but I didn’t want that window hanging around. I had to wait for it to load, then get rid of it. I had to take my hand off the keyboard to click the ‘Add’ button. It worked, but was less graceful than what I had before.

I spent a few minutes with PHP and came up with a solution. It’s not as pretty as what I had with Google Reader, but it’s just as fast, and works without any mousing, clicking, or lingering windows. I tap ‘i’ on an article, and a window pops up, says ‘Saved,’ and disappears on its own after a second or so. (If there’s a problem, you see an error instead and the window does not go away.)

First, create a script on your site. I named mine instapaper.php and put it at the root of my Fever˚ site. But you can put it anywhere, including on another domain entirely. You just need to know the URL to configure Fever˚ later.

<?php

$api = 'https://www.instapaper.com/api/add';
$close_timer = 750; // how many ms to display 'Saved!'

// Your instapaper.com credentials
$user = 'my_username';
$pass = 'my_password';

$u = urlencode( $_GET[ 'url' ] );
$t = urlencode( $_GET[ 'title' ] );
$s = urlencode( $_GET[ 'selection' ] );

$curl_url = "$api?username=$user&password=$pass&url=$u&title=$t&selection=$s";

$ch = curl_init( $curl_url );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, TRUE );
$ret = curl_exec( $ch );
curl_close( $ch );

if ( $ret == '201' ):
?>

<h2>Saved!</h2>
<script language="javascript"> 
setTimeout("self.close();",<?php echo $close_timer ?>) 
</script>

<?php else: ?>

<p>ERROR: instead of response code 201, we got:<br />
<?php echo $ret ?></p>

<?php endif ?>

Now, go into your Fever˚ preferences and click the ‘Sharing’ tab. I suggest removing the ‘i’ key from the Instapaper item, and changing its name (I used ‘ORIGINAL Instapaper’). That way it’s still there if you ever want to switch back.

Click the ‘+’ icon to add a new service. Name it ‘Instapaper,’ set the key to ‘i’ or whatever you prefer, and set the URL as follows. You’ll need to substitute the path to your script.

http://mysite.com/fever/instapaper.php?url=%u&title=%t&selection=%e

Click ‘Save’ and you’re done.

(I’ll write more about Fever˚ itself after I’ve used it for a while longer.)


5
Mar 10

Indenting Ruby in Vim

Whenever possible, I code in TextMate. TextMate is good at a lot of things; one of them is remembering that in Ruby I want a tab stop of two, not four like in most other languages.

But frequently, it’s easier for me to code in Vim on my servers, over an SSH connection. Here’s where I run into trouble. My .vimrc file has the general coding defaults I prefer:

set autoindent
set expandtab
set tabstop=4
set shiftwidth=4
set textwidth=0

I have mappings to easily swap syntaxes in-buffer, such as these.

let mapleader=","
map <leader>spe :set syntax=perl   ai et ts=4 sw=4 tw=0<CR>
map <leader>spy :set syntax=python ai et ts=4 sw=4 tw=0<CR>
map <leader>sr  :set syntax=ruby   ai et ts=2 sw=2 tw=0<CR>

I have autocommands based on filenames (for cases where the syntax isn’t autodetected).

" .t is generally a perl test script
au BufEnter *.t   set syntax=perl ai et ts=4 sw=4 tw=0
" .inc is generally PHP
au BufEnter *.inc set syntax=php  ai et ts=4 sw=4 tw=0

Yet I still have the issue where I edit a Ruby file, the syntax is autodetected, and I end up with shiftwidth=4 because that’s the default. I can tap ,sr but that’s inconvenient to do every time. I can’t use an autocommand because these files don’t end in a predictable extension.

It turns out that Vim has a way to let you define settings per syntax at buffer load time, using Vim scripts inside the ~/.vim directory. I added this to the global .bash_profile that I use everywhere. This Vim script is automatically created whenever I log in.

# Ruby-specific vim settings
mkdir -p $HOME/.vim/after/syntax
echo "set ai et ts=2 sw=2 tw=0" \
    > $HOME/.vim/after/syntax/ruby.vim

Now, these settings are applied whenever Vim detects I’ve entered a file with Ruby syntax, regardless of filename. Works like a charm.

You can use this trick to do anything you want using any supported Vim syntax; just name a file after the syntax in question (perl, php, m4, etc).


28
Feb 10

Translate Youtube Mobile (bookmarklet)

For some reason I run into this issue a lot. I get to a YouTube mobile page on my iPhone, and not wanting to view the video that moment, I save it to Instapaper. But when I go back to view it, it doesn’t work. I get a generic YouTube Mobile page.

But I noticed that the video ID is embedded in the URL. The browser just doesn’t want to see it for some reason. Luckily, Javascript can fix this easily.

Drag the bookmarklet to your Bookmarks Bar. When you encounter a YouTube Mobile page that shows up as a generic “front page,” click the bookmarklet, and it should load the original video you had saved.

Translate YouTube Mobile ← Drag this to your Bookmarks Bar

Here is the source code for those who might be interested.

url = document.location.href;
re = /desktop_uri=(http%253A%252F%252Fwww.youtube.com)?%252Fwatch%253Fv%253D([^%25%26]+)(%25|%26)/;

if ( re.test( url ) ) {
    location = 'http://youtube.com/watch?v=' + RegExp.$2;
} else {
    alert( 'Unrecognized URL format' );
}

UPDATE 28 Feb 2010: new version, fixed regex bug.

UPDATE 08 Jul 2010: new version, improved regex for another case.


25
Feb 10

Shorten Amazon URL (bookmarklet)

Do you find Amazon’s product page URLs ridiculously long? They’re a pain to paste into an IM window or an email. For most product pages, a much shorter version can be used. For instance, you can turn this:

http://www.amazon.com/gp/product/014311638X/ ref=s9_simh_gw_p14_i1?pf_rd_m=ATVPDKIKX0DER &pf_rd_s=center-2&pf_rd_r=1V6X9CF9300FXVK16QJY &pf_rd_t=101&pf_rd_p=470938631&pf_rd_i=507846

into this:

http://www.amazon.com/gp/product/014311638X

Drag the bookmarklet to your Bookmarks Bar. When you’re on an Amazon product page and want to shorten the URL, click it. You’ll be taken to the same page, with the short URL instead.

Shorten Amazon URL ← Drag this to your Bookmarks Bar

For those who are interested, here is the source code.

var url = document.location.href;

var dp_re = /\/dp\//;
var gp_re = /\/gp\/product\//;

if ( dp_re.test( url ) ) {
    location = url.replace( /^(http:\/\/[^\/]+).*(\/dp\/[^\/]+)\/.*$/, "$1$2" );
} else if ( gp_re.test( url ) ) {
    location = url.replace( /^(http:\/\/[^\/]+).*(\/gp\/product\/[^\/]+)\/.*$/, "$1$2" );
} else {
    alert( "Unrecognized URL format" );
}

8
Mar 07

Resize Terminal

Before I switched to Mac OS X, I was using a Sun Solaris machine as a desktop, and fvwm2 as my window manager. Fvwm has a nice feature called “grow window” which lets you grow a window to the full height of the display. This was occasionally useful for other programs, but I used this feature constantly for terminal windows, particularly when writing code. The extra vertical space really makes it easier to maneuver around a file. So when I came to OS X, naturally my first foray into Applescript was to port this feature so I could keep my lovely “tall” terminal windows.

This script will cycle through three sizes:

  • “normal” (default: 80×24)
  • “tall” (default: 80×50)
  • zoomed

If the window isn’t in any of those states, it is set to normal size. The sizes are all customizable by setting property values at the top of the script.

Download resize_terminal.zip


2
Aug 06

txp:dml_rand_masthead

I wanted to use a random, rotating image as my site’s masthead and there was no simple way to do this in Textpattern. Initially I wrote a form with custom PHP code, including the Textpattern config, connecting to the database, doing a query… it seemed messy. So I started over and decided to do it as a plugin.

To install:

  1. Download the plugin file (see link below)
  2. Go to Textpattern’s admin tab
  3. Go to the plugins tab
  4. Paste the contents of the downloaded file into the text box and click upload.
  5. Once the plugin shows up in the list, you’ll have to click ‘no’ in the Active column to change it to ‘yes’; then the plugin will be active.

To use:

  1. Upload your masthead images using Textpattern’s image system. Put them all into the same category. I’ll use “site-masthead” as an example.
  2. Where you want the masthead to appear, add the tag <txp:dml_rand_masthead category="site-masthead" />

By default, the link will be built so that the tool tip is “Go to main page.” You can change this to any value you want by adding the linktext attribute, e.g.: <txp:dml_rand_masthead category="site-masthead" linktext="Return to home page"/>

You can also give the class="classname" attribute to attach a CSS class to the masthead.

Download dml_rand_masthead-0.5.txt


19
Feb 06

txp:dml_article_thumb

Textpattern provides the <txp:article_image /> tag, which I use in the templates for the majority of posts on my site. However, for some reason it doesn’t provide an equivalent tag for the thumbnail version of an article image. Since I use thumbnails in my RSS feed, this was problematic for me. This plugin was my answer to the problem.

To install:

  1. Download the plugin file (see link below)
  2. Go to Textpattern’s admin tab
  3. Go to the plugins tab
  4. Paste the contents of the downloaded file into the text box and click upload.
  5. Once the plugin shows up in the list, you’ll have to click ‘no’ in the Active column to change it to ‘yes’; then the plugin will be active.

To use:

  1. Make sure your article has an article image assigned. The article image is listed under the advanced options.
  2. Put a <txp:dml_article_thumb /> tag where you want the thumbnail to appear.

The article image should be the image’s ID number only. Don’t add a file extension or anything, just the ID, i.e. 52. To put the full size image into the article, use <txp:article_image />.

Download dml_article_thumb-0.6.txt


10
Feb 06

iPhoto batch add comments

UPDATE: As the first comment below indicates, one can now do this natively in iPhoto. A little Googling suggests that Apple added this feature in iPhoto 4. So check out the Batch Change feature (found in the Photos menu), it appears to do the same thing that my Applescript does. Actually, it does one thing my script can’t do — it is able to replace comments entirely, if you want, instead of appending to the existing comment.

Thanks to Michael Browne for pointing this out.

(Original post follows…)

I use iPhoto to manage my digital photo collection, but I find its keyword feature to be very difficult to deal with. I wanted a free-format searchable keyword system, not some predefined list of tags I could add to my pictures.

iPhoto has a comment facility so you can put remarks on your images. However, you can only do so one image at a time using a text entry box. When I have a newly imported batch of 200 photos and want to apply the same keyword(s) to all of them, that just isn’t going to work.

Enter Applescript. This script will let you add a random string of word(s) to any selection of images in iPhoto. Simply select the desired photos in your current iPhoto window, run this script, enter your keyword(s) in the dialog box and click OK. The script will run and pop up another dialog when it’s finished.

The script will recognize existing comments and preserve them. If an image has no comment, the keyword(s) are set as the comment. If an image already has a comment, the keyword(s) are appended after adding a space.

Now, I can go to iPhoto, type “hubbard glacier” into my search box, and find all the pictures I took of a particular glacier in Alaska. Voila! And I don’t have a zillion pre-defined keywords in the iPhoto preferences, either.

One final note. If you apply your keyword(s) to a lot of images, it might take a while to get back the final OK window. Don’t worry, Applescript is still working for you. Be patient. On my PowerBook G4 1.25GHz laptop, it can take a couple of minutes to get the result back if I’m marking 200 photos.

DISCLAIMER: Use this software at your own risk. If it does something bad to your iPhoto database, it’s not my fault.

NOTICE: I have used this script with iPhoto 5 and can claim it will work. It has not been tested with iPhoto 6 to my knowledge. If you decide to try it with iPhoto 6, please drop me a note and let me know what happened.

Download iphoto_batch_add_comments.zip


21
Sep 05

Resize Safari

Resize Safari lets you easily switch the front-most Safari window between various common monitor resolutions (640×480, 800×600, and 1024×768). I find this of use to check how a page will display under those common video resolutions.

Download resize_safari.zip