Archive for the ‘code’ Category

Changing Safari’s Search Engine

Wednesday, December 2nd, 2009

Safari doesn’t allow you to change the search provider without resorting to hacks. If you’d like to use something other than Google or have additional search providers available in Safari, try my bookmarklet tip.

A bookmarklet is a special bookmark that uses JavaScript to add functionality. When you click on a bookmarklet from your Bookmarks Bar, a prompt will appear. Type your search, hit return, and the search results will be loaded in the browser window. If your Bookmarks Bar is not visible, go to View > Show Bookmarks Bar in Safari.

Other Useful Search Engines

I made some bookmarklets of popular searches. Simply drag the links below to your Bookmarks Bar to use.

Quick Keyboard Access

Bookmarks in Safari’s Bookmarks Bar can be accessed through a keyboard shortcut. Press command (⌘) and 1 to load the first bookmark in the Bookmarks Bar. Press 2 for the second, 3 for the third and so on.

Create Your Own

Here’s the base JavaScript for the bookmarklet:

var q = prompt('Search for','');
if (q != null) {
	window.location = 'http://example.com/search?query=' + q;
}

When merged to a single line and URL escaped, it looks like:

javascript:var%20q=prompt('Search%20for','');if(q!=null){window.location='http://example.com/search?query='+q;}

To create your own, replace http://example.com/search?query= with the actual search provider’s query URI. Copy the URL escaped single line of JavaScript with the javascript: prefix. Open a blank Safari window and paste the line. Drag the site icon (the blue sphere in the Address Bar) to your Bookmarks Bar to save.

This is an easy and flexible way to have different and multiple search providers in Safari.

How to Setup OpenID with Google Apps

Monday, September 28th, 2009

Digital Dandelion wanted to provide OpenID accounts for its staff. (I’ll explain why later.) It could have setup its own OpenID server, but it already used Google Apps for Your Domain. Google recently announced that the Google OpenID Federated Login API had been extended to Google Apps accounts: “Individuals in these organizations can now sign in to third party websites using their Google Apps account, without sharing their credentials with third parties.” Brilliant.

Here’s how to set it up for your domain:

1. Sign up for Google Apps for Your Domain and set it up as Google instructs.

Note: The Federated Login Service is disabled by default for Google Apps Premier and Education Editions. The domain admin can enable it from the Control Panel at http://www.google.com/a/cpanel/<your-domain>/SetupIdp. The Federated Login Service cannot be disabled in the Standard Edition, which is to say that it’s already turned on for freeloading Google Apps customers.

2. Add openid file on your server.

Create a file accessible on your site as http://example.com/openid with this inside of it:

<?xml version="1.0" encoding="UTF-8"?>
<xrds:XRDS xmlns:xrds="xri://$xrds" xmlns="xri://$xrd*($v*2.0)">
	<XRD>
		<Service priority="0">
			<Type>http://specs.openid.net/auth/2.0/signon</Type>
			<URI>https://www.google.com/a/example.com/o8/ud?be=o8</URI>
		</Service>
		<Service priority="0">
			<Type>http://specs.openid.net/auth/2.0/server</Type>
			<URI>https://www.google.com/a/example.com/o8/ud?be=o8</URI>
		</Service>
	</XRD>
</xrds:XRDS>

Be sure to replace example.com with your domain.

3. Tell Apache to serve the openid file with the correct MIME type.

You can do this by modifying your .htaccess file. If this file does not exist in your web root directory, create it. Add these lines:

<Files openid>
	ForceType application/xrds+xml
</Files>

4. Add host-meta file on your server.

Create a file accessible on your site as http://example.com/.well-known/host-meta with this inside of it:

Link: <https://www.google.com/accounts/o8/site-xrds?hd=example.com>; rel="describedby http://reltype.google.com/openid/xrd-op"; type="application/xrds+xml"

Again, be sure to replace example.com with your domain.

5. Access your OpenID with the “vanity” URI http://example.com/openid

You’ll be redirected to Google Accounts, asked to login, asked to approve the site for authentication, and on your way to enjoying the many benefits of OpenID.

Bingo.

Atom XML timestamp to PHP Date Time Object

Monday, May 4th, 2009

I often need to read and format the updated node in an Atom formatted XML feed for display. PHP oddly lacks the ability to make a date object from RFC 3339 formatted dates, so here’s how to do it in PHP:


< ?php
$atomFormattedDate = '1984-01-24T12:00:00.000Z';
$phpTimestamp = strtotime(substr($atomFormattedDate, 0, 10).' '.substr($atomFormattedDate, 11, 8));
echo date('m d, y', $phpTimestamp);
?>

Finding the Day within the Year

Monday, August 11th, 2008

Unlike many other great languages, ActionScript 3 does not have a method for finding a date’s day within a year. For example, February 10, 2008, is the 41st day in the year. Here is my solution:

function getDayOfYear(date:Date):Number {
var monthLengths:Array = new Array (31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);

// A leap year is divisable by 4, but not by 100 unless divisable by 400. Seriously.
if (((date.getFullYear() % 4 == 0) && (date.getFullYear() % 100 != 0)) || (date.getFullYear() % 400 == 0)) {
monthLengths[1] = 29;
trace ("leap year");
}

var dayInYear = 0;

// get day of year up to month
for (var i:Number = 0; i < date.getMonth(); i++) {
dayInYear += monthLengths[i];
}

// add day inside month
dayInYear += date.getDate();

// Start counting on 0 (optional)
dayInYear--;

return dayInYear;
}

trace(getDayOfYear(new Date("03/10/2008")));

And for giggles, here are the starting days for months in a non leap year:

var monthStartDays:Array = new Array(0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334);

Quick and Memorable Proportional Resizing in Flash

Saturday, August 9th, 2008

When needing to scale loaded assets in ActionScript so that no side is larger than a particular dimension, you can spare manual proportion calculation by using the scaleX and scaleY display object properties.

Manual proportional scaling code looks like this:

myDisplayObject.height = (originalHeight * newWidth) / originalWidth;
myDisplayObject.width = newWidth;

That’s fine and dandy, but I typically need to write out the proportion math out in my head or on paper. Utilizing the scaleX and scaleY display object properties, we can accomplish proportional scaling in more memorable manner. When you change a display object’s width or height, the scaleX and scaleY properties automatically get updated. To keep the display object proportional, set the smaller side’s scale to the scale as the larger side after the larger side’s dimension is set.

var maxSize:Number = 80;
if (myDisplayObject.width > myDisplayObject.height) { // horizontal asset
myDisplayObject.width = maxSize;
myDisplayObject.scaleY = myDisplayObject.scaleX;
} else { // vertical asset
myDisplayObject.height = maxSize;
myDisplayObject.scaleX = myDisplayObject.scaleY;
}
close