RSS AlternateIdea

http://alternateidea.com/

Last checked 34 minutes ago.

39 people have subscribed to this feed.

Feed frequency

post frequency (last month)

PostRank™ filter

latest 15 posts

« older items




Friday October 10th, 2008

2.1

Best Practices For Cocoa and CocoaTouch

AlternateIdea From AlternateIdea, 3 months ago, 0 comments Comment

Best Practices For Cocoa and CocoaTouch—Invaluable tips from Cocoa developers.

Wednesday October 8th, 2008

6.0

Deploying Static Websites With Net::SCP

AlternateIdea From AlternateIdea, 3 months ago, 0 comments Comment

I recently had a couple of projects that consisted of a few static HTML files and a couple images. I could have used a blog or lightweight CMS, but those were overkill. I could use scp, but remembering the scp command every time I made a small change proved to be a hassle. I could have used Capistrano, but that too, felt overboard for my simple needs. Lucky for me, in addition to Capistrano, Jamis Buck wrote Net::SCP which was the perfect tool for the job.

From the Net:SCP documentation:

”Net::SCP implements the SCP (Secure CoPy) client protocol, allowing Ruby programs to securely and programmatically transfer individual files or entire directory trees to and from remote servers. It provides support for multiple simultaneous SCP copies working in parallel over the same connection, as well as for synchronous, serial copies.”

The only thing we’re interested in is uploading, so lets get down to the code which I’ve packaged as a rake task.


REMOTE_DIR = "/home/caged/public/website.com/public"

desc 'Upload the site'
task :deploy do
  Net::SCP.start("hostname", "caged", :port => 3000) do |scp|
    scp.upload! 'site/index.html',  REMOTE_DIR
    scp.upload! 'site/stylesheets', REMOTE_DIR, :recursive => true
    scp.upload! 'site/images',      REMOTE_DIR, :recursive => true
    scp.upload! 'site/article',     REMOTE_DIR, :recursive => true
  end
end

Now lets take a look at the code. If you’re not using a public-key authentication, you’ll need to set :password in the start method. The upload! method is a synchronous (blocking) upload. If you’re uploading a lot of large files, you should consider using upload (no !) which will return immediately and continue processing your script while the upload is in progress. Finally, instead of specifying the name of every file inside a directory, you can set :recursive => true and Net::SCP will create the directory on the remote server and upload the files contained in it.

That’s it, the no-frills way to deploy a small static website.

Tuesday October 7th, 2008

4.9

Programming Politics

AlternateIdea From AlternateIdea, 3 months ago, 0 comments Comment

Could you imagine, for a moment, what the political scene would be like without the social media giants Memeorandum, Reddit, Twitter, Digg, and Youtube? What about the old media websites that started as an experiment, required a registration and charged for access to content? Yes, it’s hard to imagine, but it was only four short years ago that none of these social websites existed and the old media websites were merely second class citizens to their broadcast counterparts. Meanwhile, while we’ve been caught up in the infinite streams of information, there has been a small, but highly motivated community of hackers opening up government in a whole new way, providing APIs ripe for mashups and experimentation.

The Post-Bush vs. Kerry Boom

It’s been a whirlwind of a political season. In 2004, we might never have seen a hoard of angry Redditors opposing the bailout; We’d have to do some real digging to find a video of Jeremiah Wright saying “God Damn America”; We’d be hard pressed to find the speech that was the tipping point for “YES WE CAN” and the subsequent song and video inspired by that speech that went on to win an Emmy. We may have never been able to see Sarah Palin in the Alaskan Gubernatorial Debate in 2006. You can almost conclude that anything that has happened in the last four years of politics can be found online in some form or fashion if your Google skills are up to par.

Unlike today’s modern, up-to-the-minute websites of, in 2004, old media websites such as CNN, MSNBC, Fox News were not so flattering.

CNN - Nov 5, 2004 CNN Nov 5, 2004
Kudos to those who spotted Obama winning the Illinois Senate

MSNBC - Oct 27, 2004 MSNBC - Oct 27, 2004

FOX News - Oct 14, 2004 FOX News - Oct 14, 2004

Pioneering the way. Open APIs and Open Source efforts

I must admit, when I started to research Open Source efforts in government, I was skeptical. I haven’t heard of much in passing, even given my rationale that tech news will always be accompanied by political news. But, I pressed on and I was soon exposed to a small, but extremely active and knowledgeable community of Open Source hackers and organizations committed to bringing transparency to our government.

Govtrack: The Granddaddy of them all

From my research, Govtrack is the data provider for an overwhelming majority of the websites I found. Govtrack describes itself:

An independent, non-partisan, non-commercial website launched in September 2004, GovTrack.us was the first website whose primary goal was to provide comprehensive legislative tracking for everyday citizens and was the first congressional transparency website to embrace Web 2.0 and principles of open data.

Earlier this year, Joshua Tauberer, the creator and primary maintainer of Govtrack decided to open source the Govtrack application.

Sunlight Foundation: The Incubator

Sunlight Foundation is the YCombinator of the open government movement (in spirit). They give small grants to help fund websites whose mission is to open government and provide transparency in some fashion.

A List of Resources

Open Data & APIs

  • Govtrack Congressional Data [XML] - Everything from votes to the full text of speeches.
  • Sunlight Labs API [XML, JSON] - A meta API that links candidates to external services such as Eventful, Govtrack, VoteSmart, etc. Also provides a short bio.
  • Project Votesmart API [XML, JSON] - A wealth of information including votes grouped by issues, state, etc.
  • Watchdog [N3, XML, JSON] - Recently started by Reddit’s Cofounder, Aaron Swartz, contains all types of information relative to a candidate such as Interest Group Alignment, Earmark data, and Money. This project can also be found on GitHub.
  • Metavid US Legislative Footage API [RSS, XML] - A nice search API for public domain legislative videos. Find when John McCain mention’s “my friends” and when Barack Obama mentions “change”.
  • Open Secrects [XML, JSON] - In depth tracking of campaign contributions.
  • FEC Data [TXT, CSV, ZIP] - FTP access to all the data collected by the Federal Election Commission.
  • Follow The Money [XML] - State-level campaign donor data.

Mailing Lists

Other Resources

Friday September 26th, 2008

3.8

Track House And Senate Votes On Twitter

AlternateIdea From AlternateIdea, 3 months ago, 0 comments Comment

Track House And Senate Votes On Twitter. I’ve put together a small script that parses vote data collected by Govtrack.us and posts to twitter.

Monday July 28th, 2008

6.1

Protip: Color grep searches in terminal

AlternateIdea From AlternateIdea, 5 months ago, 0 comments Comment


alias grep='GREP_COLOR="1;37;41" LANG=C grep --color=auto' #use GREP_COLOR=7 to highlight whitespace

Stash this away in your Z Shell (~/.zshenv) or Bash environment (~/.bashrc) and set your preferred ANSI code.

Tuesday June 17th, 2008

9.1

MacRuby: The Path Forward

AlternateIdea From AlternateIdea, 6 months ago, 0 comments Comment

MacRuby is quietly making progress. Destined to replace RubyCocoa as the de facto standard for writing Cocoa applications in Ruby; it’s shaping up to become more than just a fun experiment, it’ll soon be a viable choice.

Burn Baby Burn

Unlike RubyCocoa, MacRuby is based on Ruby 1.9, powered by the YARV bytecode interpreter, resulting in significantly faster execution times. From the “Why MacRuby” page:

In MacRuby, all Ruby classes and objects are actually Objective-C classes and objects. There is no need to create costly proxies, convert objects and cache instances. A Ruby object can be toll-free casted at the C level as an Objective-C object, and the Ruby VM can also handle incoming Objective-C objects without conversion.

Also, the primitive Ruby classes, such as String, Array and Hash in MacRuby have been re-implemented on top of their Cocoa equivalents, respectively NSString, NSArray and NSDictionary. As an example, all strings in MacRuby are Cocoa strings, and can be passed to underlying C or Objective-C APIs that expect Cocoa strings without requiring a conversion. And it is possible to call any method of the String interface on any Cocoa string too.

That’s exciting news. MacRuby applications significantly outperform RubyCocoa applications and aim to enable the creation of full-fledged Mac OS X applications which do not sacrifice performance in order to enjoy the benefits of using Ruby.

Syntax

Considering String, Array, and Hash/Dictionary require no conversion, we get both the Ruby methods and the Objective-C methods without needing to cast back and forth.


#MacRuby
components = "foo/bar/baz".pathComponents

# RubyCocoa
components = "foo/bar/baz".to_ns.pathComponents
#or
components = OSX::NSString.stringWithString("foo/bar/baz").pathComponents

#Objective-C
NSArray *components = [[NSString stringWithString:@"foo/bar/baz"] pathComponents];

And the biggest change is probably keyed arguments. I’ve grown to like keyed arguments in Objective-C because of their descriptive nature. Normally you can look at a method definition and determine what’s what.


def fetch_url(url, timeout)
  ...
end

However, unless you look up the method definition in the source or documentation, you don’t fully understand what the second argument encompasses during invocation.

obj.fetch_url("http://foo.com", 60)

This is where keyed arguments come into play. Take a look at how this method would be composed in Objective-C.


- (void)fetchURL:(NSURL *)url timeout:(float)theTimeout {
  ...
}

[obj fetchURL:[NSURL URLWithString:@"http://foo.com"] timeout:60.0];

And finally, we could write this method in MacRuby with the new keyed arguments syntax.


def fetch_url(url, timeout:timeout)
...
end

obj.fetch_url("http://foo.com", timeout:60.0)

We get the added benefit of clarity at the cost of verbosity. And finally, there is an additional nuance: CamelCase or under_score. I think I’ve settled with camelCase for now, because it’s not possible (practical ATM) to use underscored Objective-C methods, however, I’ve been told it’s being considered.

The Big Macs: macruby, macrake, macirb, macgem

Once you’ve set yourself up with MacRuby, you should know your programs are given a prefix of mac unless you specify a different prefix when compiling by hand. This means we’ll need to use macruby to invoke our Ruby scripts written for MacRuby.

In a completely convoluted example, lets grab the name of every application running on our machine; print it’s application name, and replace “Applications” in it’s path with “FOO”.


framework 'AppKit'

NSWorkspace.sharedWorkspace.launchedApplications.each do |app|
  appname  = app["NSApplicationName"]
  fullpath = app["NSApplicationPath"]
  puts appname
  puts fullpath.stringByReplacingOccurrencesOfString("Applications", withString:"FOO")
end

Save this file and run with macruby thisfile.rb. You should see a list of applications running and their paths, with FOO replacing Applications. Got it? Schweet!

framework is a new method of Kernel that allows you to import any framework, including your custom frameworks. In this example, we’re including the AppKit framework and iterating over the launched applications.

Gems: A ruby in the rough

At the time of writing this article, it’s not possible to use rubygems or macruby gems in the traditional require and run method. However, you can vendor your gems and include them directly which should work perfectly fine. This is a bug that will be addressed very soon.

Get Involved

MacRuby is an official effort by Apple to make Ruby a first class citizen in Cocoa. It’s still very early in development and you’re definitely encouraged to start playing with it, writing about it, and getting involved in the project. And one final note, you can join the very helpful #ruby-osx channel on freenode.net if you’re looking for assistance.

Sunday June 8th, 2008

1.0

ActiveRecord Ported To Objective-C

AlternateIdea From AlternateIdea, 7 months ago, 0 comments Comment

Tuesday May 6th, 2008

3.3

Browser-based Developer Tools Come Full Circle

AlternateIdea From AlternateIdea, 8 months ago, 0 comments Comment

With the release of Opera’s DragonFly, IE 8’s Developer tools, Safari’s/Webkit’s Web Inspector and Drosera; and the Grand Daddy of them all, Firebug – we’ve now come full circle.

Thursday May 1st, 2008

6.3

Automating Rick Rolls with launchd

AlternateIdea From AlternateIdea, 8 months ago, 0 comments Comment

Lets face it: Your coworkers can be a real pain in the ass sometimes. What better way to remind them of this than automated Rick rolls? They’re likely to never know what hit them.

What is launchd?

According to the launchd guide on Apple’s website:

The launchd daemon takes over many tasks from cron, xinetd, mach_init, and init, which are UNIX programs that traditionally have handled system initialization, called systems scripts, run startup items, and generally prepared the system for the user

launchd stores tasks in plist format. You can find your agents in ~/Library/LaunchAgents. Here is a typical launcd file that will open iTunes every 60 seconds:


  <?xml version="1.0" encoding="UTF-8"?>
  <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
  <plist version="1.0">
  <dict>
    <key>Label</key>
    <string>com.alternateidea.article.test</string>
    <key>ProgramArguments</key>
    <array>
        <string>/usr/bin/open</string>
        <string>-b</string>
    <string>com.apple.iTunes</string>
    </array>
    <key>RunAtLoad</key>
    <false/>
    <key>StartInterval</key>
    <integer>60</integer>
  </dict>
  </plist>

Obviously this would be seriously annoying, but for the purpose of this demonstration, save this file as “com.alternateidea.article.test” in your ~/Library/LaunchAgents folder.

This script won’t run automatically, so we need to load it up first using the launchctl program:

launchctl load ~/Library/LaunchAgents/com.alternateidea.article.test.plist

Our agent is loaded up now. After 60 seconds, you should see iTunes.app open. Pretty nice, eh?

Ok, so we don’t want to keep running this agent. Lets unload it:

launchctl unload ~/Library/LaunchAgents/com.alternateidea.article.test.plist

Never gonna give you up. Never gonna automate your Mac.

So, here’s the trick: Wait until your coworker goes to lunch or you know they’ll be out of the office for 10 minutes or so. Then, let the mischief begin.

The first thing we need to do is create our agent:


  <?xml version="1.0" encoding="UTF-8"?>
  <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
  <plist version="1.0">
  <dict>
    <key>Label</key>
    <string>com.youtube.rickroll</string>
    <key>ProgramArguments</key>
    <array>
        <string>/usr/bin/open</string>
        <string>-b</string>
            <string>com.apple.Safari</string>
            <string>http://www.youtube.com/watch?v=eBGIQ7ZuuiU</string>
    </array>
    <key>RunAtLoad</key>
    <false/>
    <key>StartInterval</key>
    <integer>1800</integer>
  </dict>
  </plist>

Everything should be pretty obvious in this plist. The StartInterval is in seconds. I want to be an asshole every 30 minutes (1800) seconds. If you want to be a bigger asshole (or Son of a Bitch) you could set this to run in shorter intervals.

Now, we need to save this and load it up on your coworkers computer. Save this to ~/Library/USERNAME/LaunchAgents/com.youtube.rickroll. And load it up:


  launchctl load ~/Library/LaunchAgents/com.youtube.rickroll.plist

If you want to test and make sure everything is in working order, you can run:

launchctl start com.youtube.rickroll

Question: Did you just Rick Roll yourself?

Now, if your test ran successfully slip back to your desk and wait in anticipation for that awesome intro.

Disclaimer: If you destroy your coworkers computer or end up scrapping in the street, I’m not responsible.

Friday February 8th, 2008

4.3

DoubleClick and Define using Apple Dictionary

AlternateIdea From AlternateIdea, 11 months ago, 0 comments Comment


var selection;
if(window.getSelection)
  selection = window.getSelection();
else if(document.selection)
  selection = document.selection.createRange();

document.observe("dblclick", function() {
  if(navigator.userAgent.include("Macintosh")) {
      location.href = "dict://" + selection;
    }
});

A quick (and probably dirty) Prototype-based hack allowing Mac users to get the definition of any word by double clicking it.

7.2

A strftime for Prototype

AlternateIdea From AlternateIdea, 11 months ago, 0 comments Comment


Object.extend(Date.prototype, {
  strftime: function(format) {
    var day = this.getDay(), month = this.getMonth();
    var hours = this.getHours(), minutes = this.getMinutes();
    function pad(num) { return num.toPaddedString(2); };

    return format.gsub(/\%([aAbBcdHImMpSwyY])/, function(part) {
      switch(part[1]) {
        case 'a': return $w("Sun Mon Tue Wed Thu Fri Sat")[day]; break;
        case 'A': return $w("Sunday Monday Tuesday Wednesday Thursday Friday Saturday")[day]; break;
        case 'b': return $w("Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec")[month]; break;
        case 'B': return $w("January February March April May June July August September October November December")[month]; break;
        case 'c': return this.toString(); break;
        case 'd': return pad(this.getDate()); break;
        case 'H': return pad(hours); break;
        case 'I': return pad((hours + 12) % 12); break;
        case 'm': return pad(month + 1); break;
        case 'M': return pad(minutes); break;
        case 'p': return hours > 12 ? 'PM' : 'AM'; break;
        case 'S': return pad(this.getSeconds()); break;
        case 'w': return day; break;
        case 'y': return pad(this.getFullYear() % 100); break;
        case 'Y': return this.getFullYear().toString(); break;
      }
    }.bind(this));
  }
});

Also, some of my old and new code has been posted on GitHub. You might find something useful.

Monday February 4th, 2008

3.0

Processing in Action

AlternateIdea From AlternateIdea, 11 months ago, 0 comments Comment

<object height="225" width="400"> <param /> <param /> <param /> <param /></object>

A beautiful, organic animation by Flight404 created using Processing.

Tuesday January 8th, 2008

1.0

Calling Ruby from JavaScript

AlternateIdea From AlternateIdea, 1 year ago, 0 comments Comment

Aaron Patterson shows us how to define ruby methods which can be called from JavaScript using RKelly.

1.0

Pimp My JavaScript Skillz

AlternateIdea From AlternateIdea, 1 year ago, 0 comments Comment

Dustin might not surprise you at your house with a book in hand, but he’ll damn sure send you his innermost fu in the form of a shiny yellow and black book. It’s an excellent read if you’re looking to put more funk in your functions and class in your classes. Check out “Pro JavaScript Design Patterns” by Dustin and Ross Harmes.

—I’m Justin Palmer and I approve this message.

Friday December 28th, 2007

6.0

Widgets the YUI Way

AlternateIdea From AlternateIdea, 1 year ago, 0 comments Comment

Why are there so many lightbox implementations? Why are there numerous “versions” of widgets? Do you know which fork is the latest and greatest? Probably not, and for good reason. The current system sucks.

« older items