Category Archives: Tech

choosing smtp servers for selected accounts in Mail.app with location sensing

A problem I’ve been having is that everywhere I go I need to change the SMTP server settings for my personal and consulting accounts. The GMail IMAP accounts have authenticated SMTP and they work great everywhere, but for my personal IMAP accounts, I use the SMTP server provided by my ISP as I don’t want to set up authenticated SMTP over SSL or POP/IMAP before SMTP on my server and I definitely don’t want to run an open relay.

To solve this, I found Marco Polo, with it you can define a set of Contexts, which could be something like Office, Home, or anything else. You then define a set of Rules to figure out which Context you’re in based on Evidence Sources such as the mac address or SSID of the wireless access point you’re connected to, presence of Bluetooth/USB keyboard/mouse, external monitor, ethernet cable plugged in or unplugged, etc. Base on these you can setup rules

Here are screenshots of my setup:

Contexts:

Evidence sources:

Rules:

Once you have these setup it will guess where you’re at based with a confidence score, and you can trigger Actions on entering or leaving a Context.

Here are my actions to set SMTP servers:

Notice the first 3 MailSMTPServer actions were disabled, this is because that action actually changes the smtp server of ALL your accounts in Mail.app. I have 2 GMail over IMAP accounts (GApps for domains) and I want to use GMail’s authenticated SMTP server for those because they manage your sent mail properly when you do that. There is a “User only this server” checkbox in Leopard’s Mail.app but that doesn’t prevent the MailSMTPServer action from changing it!
So to get what I want, which is to only set the SMTP server for my personal and consulting accounts, I had to do a little bit of scripting, I like Ruby so I write a rb-appscript like this:

#!/usr/bin/env ruby
 
begin; require 'rubygems'; rescue LoadError; end
require "appscript"
include Appscript
 
['Personal', 'Consulting'].each do |account_name|
  #tell application "Mail" to set smtp server of account_name to argv[0]
  app('Mail').accounts[account_name].delivery_account.set(app('Mail').smtp_servers[ARGV[0]])
end

Marco Polo however can only execute shell scripts, so I had to compile the script into a Mac application to use the Open action in Marco Polo. To do that I had to write a shell script for each SMTP, something like this:

#!/bin/sh
/Users/ayn/bin/setSMTP.rb comcast

To make this script into an OS X application, I used Platypus:


It didn’t work at first, I ran it with error outputs and turned out the Ruby library wasn’t setup correctly, so to get it working I had to go add the path for Ruby:

Now you can compile the shell script, which calls the ruby script with an argument, into a Mac application. In Marco Polo you can then use the Open action to call that .app.
Kindda a long way to do something relatively simple, I wish the “Use only this server” account option in Mail.app actually works as expected.

Pair your mac with just 1 remote

If you have a Mac with a FrontRow remote, by default any remote will work with your Mac. I was setting up my security preferences on my new Mac and saw that you can pair your Mac with just 1 remote, pretty neat.

I actually haven’t even opened my remote coz I’m just gonna use the one from Sherry’s MacBook.

Pair with just 1 remote! (by AndrewNg.com)

Been a while since I’ve blogged, moving on, again…

Sorry for the lack of updates and daily photos, I’ve been busy with work, new puppy, and then the holiday. I got back to San Francisco and decided I’ve had enough unpaid employment (well, paid with equity that is pretty much worth nothing until proven otherwise), so I updated my resume and started a series of interviews with the a number of startups in and around SF. After sorting through a lot of bullshit, strange business models, or just stupidity in general, I found a startup I really like: Context Optional. I am starting next Tue. The founders are really chilled and the work is really exciting. We work with some of the top brands to use social applications for promotions in social networks. You can read more about what we do here. So you can look forward to new and cool Facebook and Open Social apps by me. I will continue to be using RoR and RFacebook, and probably also something like Shindig and Caja for OpenSocial.

I will move into a background role at OnMyList, we have yet to figure out what to do with it. We plan to keep the site running coz we do have people who are really into the site. I had a great time doing OML, learned a shit load of stuff, and worked with some awesome people.

How to use attachment_fu in a rake task (i.e., without using a form)

We’re launching a new Facebook app soon at OnMyList, and we need to pre-populate hundreds of celebrity images, so instead of doing it manually I wrote a cute little rake task to do them all. Took me a little while to figure this out, the trick is to do a class_eval to add the required fields to make attachment_fu happy. This works pretty sweet, the images are resized and uploaded to Amazon S3.

Thought I’ll blog it hoping someone out there will find this useful. 🙂

desc 'adding celebs'
task :add_celebs => :environment do
  dir = '/tmp/celeb_images'
  Dir.foreach(dir) do |img|
    path = File.join(dir, img)
    if File.file?(path)
      f = File.new(path, "r")
      (class < < f; self; end).class_eval do
      alias local_path path
        define_method(:original_filename) {img}
        define_method(:content_type) {"image/jpeg"}
        define_method(:size) { File.size(path) }
      end
      u = User.new
      u.celeb_name = File.basename(img, '.jpg').titleize
      u.picture = Picture.new
      u.picture.uploaded_data = f
      u.save!
      p "added user #{u.celeb_name}"
    end
  end
end

Technorati Tags: , , , , ,

Flickr stats!

Stats for your account | flickr.com

I’ve used Flickr exclusively for my pics for years now even though I have my own server and can do it myself. One thing I really miss is web stats and logs to see referrers and where my images are linked from. Flickr just announced yesterday that they added stats! You have to activate it though, they don’t collect data for you by default. I just activated mine and should see some information tomorrow.

Activate your stats today!

Technorati Tags: ,

FBJS autocomplete/typeahead in Rails

Facebook released FBJS a while back to allow some JavaScript in Facebook applications. It is basically a JavaScript parser that modifies the scripts to make them a little bit safer. Ray and I have been working on a new app for OnMyList and we want an text input box with autocomplete to suggest names. If you’re using Rails outside of Fb, or if you’re using IFRAME for your Fb app, then autocomplete is really easy, you basically only need to add 2 lines, one like this in the controller:

auto_complete_for :user, :name

where user is the model name and name is the field you want to autocomplete for inside the users table. And in the view, you add something like this:

< %= text_field_with_auto_complete :user, :name %>

This will generate everything you need to have an autocomplete textbox. However, this assumes that you can load the Prototype and Script.aculo.us js libraries, and if you want your app to use FBML, these libraries do not work as FBJS.

Autocomplete with FBML is a little bit more work. Facebook’s Developer Wiki has an FBJS typeahead with AJAX example, I don’t love hacking JavaScript so I just borrowed the code there to use in my view. That page shows you how to write a PHP AJAX endpoint, to make it work with Rails, I tried to make it so it would take the data returned by the auto_complete_for method, it turned out to be a bit too much JS changes for me, I’d like to leave the FBJS as close to original as possible. I also prefer writing Ruby more than JS so I ended up implementing the endpoint in the controller, it took a while to get it working, so to possibly save you some time, here’s how it looks like at the end:

def auto_complete_for_user_name
  names = User.find(:all,
    :conditions => [ 'LOWER(name) LIKE ?', params[:suggest_typed].downcase + '%'],
    :order => 'name ASC',
    :limit => 10).map { |n| n.name }
  render :text => "{fortext:#{params[:suggest_typed].to_json},results:#{names.to_json}}"
end

I tried render :json but it didn’t work, coz the typeahead JS is expecting a certain format, but render :text works just fine. Hope you find this post helpful. I will also edit that Wiki entry to include the Rails version of the handler.

Technorati Tags: , , , , , , ,