Posts Tagged ‘code’

Text a reminder to the members of your houseshold

Thursday, October 4th, 2012

Every week we forget trash day, so I’ve been meaning to write a script to remind all of us the night before to put the trash cans out.

I only wanted to type the phone numbers once, so I hard-coded them as variables into the script. Turning the command line argument ‘kj’ into the value of $kj was by far the most difficult because I had to look up how to do indirect references in bash, which, as it turns out, is kind of a pain.

In an effort to spare you that same pain, I’m posting the code here.

Usage

If you call the file text_reminder.sh, the usage is as follows:
text_reminder.sh [path to reminder] [recipient(s)]

[path to reminder]

This script can be used to send you several reminders. It looks for a file at the specified path, then uses the text of that file as the body of the e-mail/text. Be sure to create the reminder file before you run this script. For the purposes of example, let us assume it’s in ~/reminders/reminder.txt. Remember to keep it brief – it will be split into 140-character increments, so if you send an 1,000-character reminder, it’s going to arrive as eight annoying, disjointed texts.

[recipient(s)]

I hard-coded shortcuts into the code so I can use aliases instead of relying on myself to having to type a phone number more than once. You can set up as many recipients you want, but note that the code, as it is pasted below, will fail because there are no real recipients set up. You’ll need to add at least one to get any use out of this code.

Setting up a recipient is fairly simple: most (all?) cell phone companies allow you to text message customers via e-mail with an address following the format [10-digit phone number]@[domain]. Finding the value of [domain] is the most time-consuming part, because each company has a different domain, so you need to ask each potential recipient who their cell phone company is, then dig up the corresponding domain. I used this page as a reference, but it may be out of date by the time you read it. If your phone number is (503)555-1234, and you’re a T-Mobile customer, the address at which my phone receives texts is 5035551234@tmomail.net.

As always, send several tests before using it for real.

Code


kj='number@provider.com' # CHANGE THIS
eric='number@provider.com'
# etc, you can set up as many aliases as you like.
 
if [ "$#" -lt 2 ]; then

    echo Usage $0 [path to reminder] [recipients]
    echo Recipients defined: kj, eric # CHANGE THIS

    exit
else
    reminder=$1
    if [ ! -f $reminder ]; then
        echo The reminder you specified, $reminder, does not exist.
        exit
    fi
 
    let i=2;
    while [ $i -le $# ]; do
        numvar=\$$i
        varname=`eval echo $numvar`
        vartoeval=`eval echo $varname`
        recipient2=\$$vartoeval
        recipient=`eval echo $recipient2`
 
        if [ "$recipient" == "" ]; then
            echo I do not understand the recipient $recipient
        else
            echo mail "$recipient" -s 'Friendly reminder' -r kj@kjcoop.com -a $reminder
            mail "$recipient" -s 'Friendly reminder' -r "kj@kjcoop.com" < $reminder
        fi
    let i=i+1
    done
fi

Cron job

In order to make this run regularly, you’ll need to create a cron job. You can do that by running “crontab -e”. On most systems, this will default to vi, which can be kind of scary to the uninitiated, so here’s a cheat sheet.

I’m going to assume you’re uninitiated: Copy and paste the following into a text editor you feel comfortable with:
0 21 * * 1 ~/bin/text_reminder.sh ~/reminders/reminder.txt kj eric

This will send a text to each recipient listed reminder at 9 PM every Monday night contain. Change the path and recipient(s) to your liking, then copy the whole thing. Switch to the window where you ran “crontab -e” and vi awaits you. Type “i” to enter “insert” mode – this will allow you to use vi like a normal text editor. Paste.

Hit the “esc” key (top left side of your keyboard) to exit “insert” mode. Type “:wq” to save your changes and exit. If you screw up and you don’t want to save your changes, you can exit with “:q!”.

Potential improvements

This was pretty quick and dirty, but a couple glaring improvements stand out:

  • Allow the user to specify a subject. At the very least, make the hard-coded subject a variable so that it remains uniform between the line that sends the mail, and the line below that prints the send command used.
  • I could have put the aliases in some kind of array, which would have allowed for more robust error-checking and prevented the user from having to manually change the usage instructions.
  • Some of the variable names in the while loop are awful.

Upgrading to WordPress 3.3 – the missing “Format” box

Friday, December 16th, 2011

I recently installed WordPress 3.3 for a client, and I was very impressed. Specifically I was excited about tinkering around with the “Format” box, which allows you to post in a variety of formats, including, “Status”, “Gallery” and the regular post format “Standard”.

I wasted no time upgrading this site to WordPress 3.3, but no fun format box. I poked around at options and Googled, to no avail. Most suggestions seemed to revolve around some browser problem, but I could see it on the other site, plain as day, so my browser was obviously capable of displaying it.

For lack of a better idea, I started grepping. I used Firebug to get the id attribute (“formatdiv”), figuring there would be a finite number of instances in the code. Indeed, there was exactly one. It was in wp-admin/edit-form-advanced.php. There were two relevant lines:
if ( current_theme_supports( 'post-formats' ) && post_type_supports( $post_type, 'post-formats' ) )
    add_meta_box( 'formatdiv', _x( 'Format', 'post format' ), 'post_format_meta_box', null, 'side', 'core' );

This alerted me that it was a problem with the theme, which unsurprising considering that I’m still using a customized version of the 2009 default theme. I knew it worked in the latest theme, twentyeleven, so I went to that directory and grepped for ‘format’ and braced myself for the worst. Once I eliminated the CSS files, it was easy to hone in on line 104 of functions.php
add_theme_support( 'post-formats', array( 'aside', 'link', 'gallery', 'status', 'quote', 'image' ) );

This takes place in a function called setup. I went over to my theme and looked for a similar function in the functions.php file. It did not have one. I pasted it in toward the middle of the file and hoped for the best. The format box appeared like magic. I haven’t actually tinkered with it yet, but I wanted to write up my experiences – before I forget them – so that someone else with an old theme can bring some new magic into their WordPress experience.

Improved GPS removal

Friday, August 13th, 2010

I recently posted about a way to remove the GPS information from pictures. I wrote a script that will retain the Date/Time information. It requires jhead.
#!/bin/bash
# Will remove non-essential EXIF from a jpeg
# while retaining Date/Time value. I use it to
# remove GPS information from pictures I
# want to put on the internet.
 
# Created Wed Aug 11 18:43:03 PDT 2010
 
# For handling spaces. See:
# http://www.cyberciti.biz/tips/handling-filenames-with-spaces-in-bash.html
SAVEIFS=$IFS
IFS=$(echo -en "\n\b")
 
for img in $@; do
    echo Working on "$img"
 
    # Grab the original date/time
    DT=`jhead "$img"|grep 'Date/Time'`
 
    # Current format: " Date/Time : yyyy:mm:dd hh:mm:ss"
    # jhead requires: yyyy:mm:dd-hh:mm:ss
    DT=${DT:15:25}-${DT:25}
 
    # Strip non-essential information
    jhead -purejpg "$img"
 
    # Put the date/time back
    jhead -mkexif -ts$DT "$img"
done

It accepts image files as arguments. You can pass as many or as few as you feel like. If you’re interested in keeping other non-essential exif data, you might want to look into the jhead argument “-te”.

Removing GPS data from an image

Wednesday, August 11th, 2010

I constantly forget how to remove the geotags from my pictures before I put them on the internet. Here’s an imperfect way to do so:
jhead -purejpg /path/to/image.jpg
Source

This will get rid of timestamps and other ancillary information that you may want to keep attached to the image. It seems like it would be pretty simple to write a script that uses jhead with no arguments to grab the Date/Time field, then run it again with -purejpg to remove all extraneous data, then run it a third time with the appropriate arguments to re-set the Date/Time field.