Resize to a Specific Element’s Dimensions

November 26th, 2008  |  Published in Development

Here’s a snippet to resize a background image to the dimensions of an element. In this case, #bkgImage.

var resizeBkg = function() {
    var h = self.innerHeight || document.documentElement.clientHeight || document.body.clientHeight;
    var w = self.innerWidth || document.documentElement.clientWidth || document.body.clientWidth;
    var e = document.getElementById('bkgImage');
    if (w > h) {
        e.width = w;
        e.height = w;
    } else {
        e.height = h;
        e.width = h;
    }
} 

Delete .SVN Directories

September 16th, 2008  |  Published in Development

Ever accidentally do an svn checkout instead of an export? Here’s a command-line snippet to take care of those hidden .SVN directories:

rm -rf `find . -type d -name .svn`

Warning: Take great care when using “rm -rf” always.

Find Duplicates in Address Book

August 15th, 2008  |  Published in Technology

Ever had the situation where you have synced your iPhone (to iTunes OS X) and happen to sync both the contacts from your Address Book and Google Contacts? Or, just happen to add someone’s new contact info as another contact-card? Well, here’s how I cleaned and merged duplicate contacts so that my Address Book performs like a champ.

  1. Launch Script Editor (Applications/AppleScript/Script Editor)
  2. Paste the following code snippet
    tell application "Address Book"
        set biglist to {}
        set theGroup to count every person
        if not (exists (group "Duplicate Entries")) then
            make new group with properties {name:"Duplicate Entries"}
        end if
        set the_names to the name of every person as list
        repeat with i from 1 to number of items in the_names
            set this_Name to item i of the_names
            set theName to name of person this_Name as string
            if this_Name is not in biglist then
                copy this_Name to end of biglist
            else
                add (people whose name is theName) to group "Duplicate Entries"
            end if
        end repeat
        save addressbook
    end tell
  3. Save your newly created script (would usually default to ~/Documents/AppleScripts/), and just open/run it

Now, the easiest way to figure out if its running or not (at least for me) was to run the Activity Monitor and sort by CPU. I simply just waited to see if Address Book would climb to the top doing 90+, and back down to nil. When this is done, you should be able to see a group called “Duplicate Entries”. From there, you may now sort through the details and delete the ones that you don’t need.

Note: You can also do a quick merge via Address Book itself from duplicate contacts. However, you won’t be able to see which contacts are being merged as its just a set-it-and-forget kind of deal. If you do not care and simply just want to get rid of those dupes, you can do so by doing the following:

  1. Launch Address Book
  2. Select Card from its menu
  3. Then, select Looking for Duplicates… and just go through the following prompts after search results finishes

Detect Operating System with JavaScript

August 12th, 2008  |  Published in Development

Here’s a snippet of JS code that might come in handy if you, let’s say, would like to filter out what type of content to display depending on your website user’s OS. A real-life example would be having a download link show up to user’s who have a client that was only published on Windows.

// This script sets OSName variable as follows:
// "Windows"    for all versions of Windows
// "MacOS"      for all versions of Macintosh OS
// "Linux"      for all versions of Linux
// "UNIX"       for all other UNIX flavors
// "Unknown OS" indicates failure to detect the OS

var OSName="Unknown OS";
if (navigator.appVersion.indexOf("Win")!=-1) OSName="Windows";
if (navigator.appVersion.indexOf("Mac")!=-1) OSName="MacOS";
if (navigator.appVersion.indexOf("X11")!=-1) OSName="UNIX";
if (navigator.appVersion.indexOf("Linux")!=-1) OSName="Linux";

document.write('Your OS: '+OSName);

Note: An alternate way can be found via PPK’s quirksmode.org

jQuery onLoad for Body-tag

April 15th, 2008  |  Published in Development

<script type="text/javascript">
$(document).ready(function() {
    someFunction();
    ...
});
</script>