Tech Thought

Tech tips, hints, and general musings. PHP, Perl, Mysql, Javascript, AJAX, JSON, Linux, Mac OSX

How-To: Remove Applications from Startup (Mac)

To remove applications that start automatically when you boot up is a simple process on the Mac, however still one that I get asked by friends and family regularly how to control. To manage the applications that launch when you first boot up, follow these steps:

  1. Click the Apple Icon in the top left corner of the screen
  2. Select “System Preferences” from the drop-down menu
  3. Select “Accounts” then click the “Login Items” tab
  4. From here you can now remove or add applications that start automatically when you log in

How-To: Backup all files on your Mac or PC

Okay, I normally don’t come straight out with a non-technical answer to something like “what’s the best way to backup all my files?”, except that there is a really really simple way to do this.

Backblaze is a backup-to-the-cloud service which costs $5 a month for unlimited backups. It deals with external hard drives, duplicate files, etc etc and requires zero configuration. Simply install and you’re done. Thoroughly recommended.

How-To: Check root DNS settings for domain

Using “dig” you can determine what IP address or CNAME your domain is pointing to quite easily. However what I didn’t know was that you can actually check what the root server thinks your domain is pointing at, so you can check if its set correctly when you are migrating to a new IP address.

To do this, simply type:

dig domaintolookup.com +trace

How-To: Flip Rows/Columns in Excel

This is really easy, but something I never knew until I just tried this:

  1. Copy the cells you want to flip
  2. Right-Click “Paste Special”
  3. Tick the Transpose checkbox

You’re done! Best Excel trick ever!

How-To: Create my.cnf file in MAMP

This is a simple process, however not something I could find documented anywhere easy to find. MAMP supplies you with a range of sample configuration files:

/Applications/MAMP/Library/share/mysql/my-huge.cnf
/Applications/MAMP/Library/share/mysql/my-innodb-heavy-4G.cnf
/Applications/MAMP/Library/share/mysql/my-large.cnf
/Applications/MAMP/Library/share/mysql/my-medium.cnf
/Applications/MAMP/Library/share/mysql/my-small.cnf

To setup one of these for MAMP, simply issue the following command:

sudo cp /Applications/MAMP/Library/share/mysql/my-choicehere.cnf /Applications/MAMP/Library/my.cnf

How-To: Convert UIImage to Greyscale Equivalent

Here’s a simple code snippet for converting a UIImage to a greyscale equivalent:

-(UIImage *) convertToGreyscale:(UIImage *)i {

    int kRed = 1;
    int kGreen = 2;
    int kBlue = 4;

    int colors = kGreen;
    int m_width = i.size.width;
    int m_height = i.size.height;

    uint32_t *rgbImage = (uint32_t *) malloc(m_width * m_height * sizeof(uint32_t));
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    CGContextRef context = CGBitmapContextCreate(rgbImage, m_width, m_height, 8, m_width * 4, colorSpace, kCGBitmapByteOrder32Little | kCGImageAlphaNoneSkipLast);
    CGContextSetInterpolationQuality(context, kCGInterpolationHigh);
    CGContextSetShouldAntialias(context, NO);
    CGContextDrawImage(context, CGRectMake(0, 0, m_width, m_height), [i CGImage]);
    CGContextRelease(context);
    CGColorSpaceRelease(colorSpace);

    // now convert to grayscale
    uint8_t *m_imageData = (uint8_t *) malloc(m_width * m_height);
    for(int y = 0; y < m_height; y++) {
        for(int x = 0; x < m_width; x++) {
			uint32_t rgbPixel=rgbImage[y*m_width+x];
			uint32_t sum=0,count=0;
			if (colors & kRed) {sum += (rgbPixel>>24)&255; count++;}
			if (colors & kGreen) {sum += (rgbPixel>>16)&255; count++;}
			if (colors & kBlue) {sum += (rgbPixel>>8)&255; count++;}
			m_imageData[y*m_width+x]=sum/count;
        }
    }
    free(rgbImage);

    // convert from a gray scale image back into a UIImage
    uint8_t *result = (uint8_t *) calloc(m_width * m_height *sizeof(uint32_t), 1);

    // process the image back to rgb
    for(int i = 0; i < m_height * m_width; i++) {
        result[i*4]=0;
        int val=m_imageData[i];
        result[i*4+1]=val;
        result[i*4+2]=val;
        result[i*4+3]=val;
    }

    // create a UIImage
    colorSpace = CGColorSpaceCreateDeviceRGB();
    context = CGBitmapContextCreate(result, m_width, m_height, 8, m_width * sizeof(uint32_t), colorSpace, kCGBitmapByteOrder32Little | kCGImageAlphaNoneSkipLast);
    CGImageRef image = CGBitmapContextCreateImage(context);
    CGContextRelease(context);
    CGColorSpaceRelease(colorSpace);
    UIImage *resultUIImage = [UIImage imageWithCGImage:image];
    CGImageRelease(image);

    // make sure the data will be released by giving it to an autoreleased NSData
    [NSData dataWithBytesNoCopy:result length:m_width * m_height];

    return resultUIImage;
}

Thanks to the friendly people at StackOverflow.com for this snipet.

What does iPhone OS 3.0 Mean for your Apps?

Recently I was involved in a podcast regarding the iPhone OS 3.0 upgrade. We looked at all the new features (and some upcoming ones in v3.1) and discussed how they relate to new features clients can develop in their apps.

You can hear more here:

What does iPhone OS 3.0 Mean for Your Apps (WSP Online)

Google Maps: Zoom Level, Center from points on map

So you’ve created a pretty Google Map on your website.  You place a whole heap of markers on your map, driven from data you’ve got stored in your database.

A nice thing to do when we first show the map, would be to ensure that all the markers are visible on the map.

To do this, we need to set the zoom level and center point of the map dynamically. So how do we do this?  The answer is GLatLngBounds():

// Get your locations from the database
var mapLocations = <? echo json_encode(Locations::Find()) ?>;

// Initialise our map, add some controls and a default center point
var map = new GMap2(document.getElementById("Mall_Map"));
map.setCenter(new GLatLng(15, 20), 2);
map.addControl(new GLargeMapControl());
map.addControl(new GMapTypeControl());

// Now the fun part - create a GLatLngBounds object
	var bounds = new GLatLngBounds();

//
for (var i=0; i<mapLocations.length; i++)
{
        var location = mapLocations[i];
        var point = new GLatLng(location.Latitude, location.Longitude);

        // createMarker() is a custom function which returns a marker for a given GLatLng
        var marker = createMarker(point);

        // extend our bounds to include this point
        bounds.extend(point);

        // add our marker
	map.addOverlay(marker);
}

// Now dynamically set the center and zoom level for our map based on our bounds!
map.setCenter(bounds.getCenter(), map.getBoundsZoomLevel(bounds));

The key is the use of the line:

map.setCenter(bounds.getCenter(), map.getBoundsZoomLevel(bounds));

This uses our bounds (which we’ve dynamically extended to include each of our points) to determine what the center and bounds zoome level should be.

How-To: Recursively remove .svn folders

Okay, so you’ve accidentally adding a bunch of files to SVN.  Or, you need to copy a bunch of files but you don’t want to take the .svn folders with you.  How to get rid of these?  On any *nix machine (Mac included) you can run the following command:

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

How-To: Convert NSData to NSString

How do you convert an NSData object into it’s string representation?  Should be easy, right?  It is, when you know how…

NSString* theString = [[NSString alloc] initWithData:theData encoding:NSASCIIStringEncoding];

iPhone UIScrollView with UIImageView have issues Interface Builder

Ever wanted to have an image view in a scrollview? Sure, you could directly add it using code, but wouldn’t it be nice to do it in Interface Builder?

If you’re like me, you like to leave on the defaults unless absolutely necessary. I had an issue where I added an image view to the scroll view in Interface Builder.
The image view then had an image added to in code. I resized the content view of the scroll view in code to be the same size as the image view (which you have to do
in order to get it to scroll).

Unfortunately, when an image larger than the scrollview was used, it went off the top of the scroll view by about 30px. I thought its origin was using the main view,
instead of the scroll view’s frame, but after modifying various properties I had no luck – and strange results, with the image always returning to about 30px higher than
the scroll view content area.

After trying to resolve things programattically, I resorted to Interface Builder again. I tried any number of properties on the scroll view to get its content
placed at its origin. It turns out that, by default, a UIImageView has a default “Mode” of center. That affects its alignment in its parent view. This ain’t
HTML kids, it is the elements themselves who decide how they are aligned in their parent elements. So, I just change the “Mode” to “Top Left”, and Robert’s your mother’s
brother.

Top Left Screenshot

The code for getting the scroll view to scroll (just FYI) is included below. If thisImage is bigger than the scroll view in any dimension, it will let you scroll
in the corresponding direction – but if it’s the same size or smaller, no scrolling is possible in that direction. Pretty nice. self.scrollView, by the way, has
only the default Interface Builder properties.

CGImageRef imageRef = thisImage.CGImage;
CGFloat width = CGImageGetWidth(imageRef);
CGFloat height = CGImageGetHeight(imageRef);

[self.scrollView setContentSize:CGSizeMake(width, height)];

How-To: Detect when MKAnnotation, MKAnnotationView is selected

I’m using MapKit to display a satellite map in one of my apps.  I create custom annotations – and it all works great.  However I wanted to be able to play sounds when a user touches one of my MKAnnotation’s on my MapKit MKMapView so that the sound matches the display of the callout (and when it disappears too).

There is no delegate method or other built-in mechanism for detecting when your annotation is selected, however it does have a selected property.  So how to detect when the MKAnnotation is selected and then play my sounds?  The answer is key/value observing.

Setup an observer on our imageAnnontationView:

[imageAnnotationView addObserver:self
		 forKeyPath:@"selected"
		options:NSKeyValueObservingOptionNew
		context:GMAP_ANNOTATION_SELECTED];

Then we get a callback whenever the selected property of our annotation changes:

- (void)observeValueForKeyPath:(NSString *)keyPath
                      ofObject:(id)object
                        change:(NSDictionary *)change
                       context:(void *)context{

    NSString *action = (NSString*)context;

    if([action isEqualToString:GMAP_ANNOTATION_SELECTED]){
      BOOL annotationAppeared = [[change valueForKey:@"new"] boolValue];
     // do something
   }
}

The value of annotationAppeared will change based on the state of the annontations selected property.  GMAP_ANNONTATION_SELECTED is a constant string I set at the top of my file.

yum fails: Fixing Fedora Core 5 yum repositories

Fedora Core 5 is currently EOL – it’s not supported any more.  As a result, you might find you can’t use yum to install packages any more – you get an error message about this file not existing:

http://download.fedora.redhat.com/pub/fedora/linux/core/5/i386/os/repodata/repomd.xml

This is because the repositories that yum uses aren’t there anymore.  However you can modify the repositories in /etc/yum/repos.d/ and add these to fix the problem:

http://archive.fedoraproject.org/pub/archive/fedora/linux/core/6/i386/os/
http://archive.fedoraproject.org/pub/archive/fedora/linux/core/6/i386/debug/
http://archive.fedoraproject.org/pub/archive/fedora/linux/core/6/source/SRPMS/

http://archive.fedoraproject.org/pub/archive/fedora/linux/core/updates/6/i386/
http://archive.fedoraproject.org/pub/archive/fedora/linux/core/updates/6/i386/debug/
http://archive.fedoraproject.org/pub/archive/fedora/linux/core/updates/6/SRPMS/

http://archive.fedoraproject.org/pub/archive/fedora/linux/extras/6/i386/

An example change would be to the /etc/yum/repos.d/fedora-core.repo file:

[core]
name=Fedora Core $releasever – $basearch
#baseurl=http://download.fedora.redhat.com/pub/fedora/linux/core/$releasever/$basearch/os/
baseurl=http://archive.fedoraproject.org/pub/archive/fedora/linux/core/5/i386/os/
[core]
name=Fedora Core $releasever - $basearch
baseurl=http://archive.fedoraproject.org/pub/archive/fedora/linux/core/5/i386/os/

iPhone: Bugged UINavigationController? View doesn’t Scroll.

Developing a networked iPhone application, I had two views that were very similar – so similar in fact that they were copies of one-another with “Sending” renamed to “Receiving”. The idea was to get them both working with different XIB files, and then modify them.

The code to load the sending view (from the main app delegate) was:

- (IBAction)sendContact:(id)sender
{
	DataController* dc = [DataController sharedInstance];
	dc.contactRecord = (NSMutableDictionary*) [self.userDefaults dictionaryForKey:@"currentUser"];

	SendingViewController *sendingViewController = [[SendingViewController alloc] initWithNibName:@"ReceiverView" bundle:nil];
	[self setBackButtonText:@"Back"];
	[self.navigationController pushViewController:sendingViewController animated:YES];
	[sendingViewController release];
}

The code for the receiving view was:

- (IBAction)receiveContact:(id)sender
{
	ReceivingViewController *receiverViewController = [[ReceivingViewController alloc] initWithNibName:@"ReceivingView" bundle:nil];
	[self setBackButtonText:@"Back"];
	[self.navigationController pushViewController:receiverViewController animated:YES];
	[receiverViewController release];
}

On load, I could _swear_ that nothing happend with the DataController’s contact record. But something screwy was going on. When I hit “send” first (on app launch), hitting “receive” worked fine (you could navigate back with no worries at all). However, when you hit “receive” first, going back only got the navigation bar to change, while the receive view was still visible.

It took ages, but setting contactRecord in the DataController shared instance fixed the problem.

The thing is, the view loaded just fine, no worries at all. No errors or warnings occured… and I wasn’t using contactRecord for anything on the view. So why did it behave like it encountered an error? I searched the code, and I was setting a label (which didn’t even exist on-screen anymore, it was an unused IBOutlet UILabel) to something contained in the DataController. Why it didn’t error properly, and why that stopped the view from unloading properly is completely beyond me… The NavigationBar operates (somewhat) in isolation from the views inside it, so if a view gets stuck (but not the Nav control) you can find yourself in a very weird situation.

So, my advice to you is:

  • It ain’t the same until it’s _exactly_ the same.
  • Just because it doesn’t error, doesn’t mean it hasn’t produced an error.
  • If you can at all avoid it, don’t copy paste entire files. Best to start empty and include the stuff you need instead.

How-To Fix: MAMP won’t start

Okay, so sometimes our shiny friend and development support buddy MAMP won’t start properly.  I’ve found this often happens when you’ve first booted up your Mac and you try to start getting stuck into your code.

Symptoms are pretty simple to diagnose – you start MAMP, and it’s control panel pops up and shows that everything is running:

MAMP Control Panel

But for some reason you can’t access your webserver or database:

MAMP not working

What to do?  Restarting the servers generally doesn’t solve the problem.  However, there is a trick I’ve discovered.  Open up the control panel and click the “Preferences…” button.  Once this popups up, choose the “Ports” tab, change nothing – and click OK:

MAMP Preferences Pane

MAMP will automatically restart – and you should be up and running!

WWDC 2009 Update: Keynote Summary for iPhone

WWDC: Today’s keynote included many things we already knew about the iPhone, and few things we didn’t. Here’s a quick summary from the team at WSP Online – on site at the Keynote in Moscone Center, San Francisco CA.

[Read the rest of this entry...]

The Hidden Park: iPhone gaming to get your kids off the couch

Bulpadok has just published The Hidden Park, an adventure game with a difference – you!  Developed in association with WSP Online Solutions, and Conduct, The Hidden Park uses the iPhone’s GPS to immerse you in a world within your local park (currently 10 parks around the world are supported, with more promised).  Using the accelerometer, camera, GPS and touch screen, The Hidden Park fully utilises all capabilities of the iPhone.

Hidden Park iPhone Screenshot

As you navigate throughout the park, you receive instructions from a character called ‘Trutton’ and learn that the park is in trouble.  Your challenge is to help Trutton save the park before its too late by navigating the park, solving puzzles and interacting with characters throughout.  The game provides a beautifully illustrated map of the park, with your location being represented by an Avatar, who moves as you move around the map.

Gameplay takes about an hour to complete, making the game an ideal day out for families, and provides a perfect opportunity to get the kids off the couch and out exploring a magical world in their local park.  Currently the Hidden Park supports the following locations:

  • Melbourne Botanical Gardens, Australia
  • Sydney Botanical Gardens, Australia
  • High Park, Toronto, Canada
  • Boston Common, Boston, USA
  • Central Park, New York, USA
  • Kensington Gardens, London, UK
  • Tokyo Ueno Park, Tokyo, Japan
  • Englischer Garten, Munich, Germany
  • Zoological and Botanical Gardens, Hong Kong

The Hidden Park is available on the App Store for $6.99 and comes highly recommended.

More information is available at TheHiddenPark.com.

BudTrap: An easy way to solve iPhone/iPod headphone tangles

The guys at BudTrap have a great idea.  Create a device, which clips into your iPhone/iPod, stops your headphone getting tangled, and costs just $5 – with $1 being donated to charity.  On top of which, you get 5 additional “buddy” devices which you can share with your friends.

BudTrap In Use

They are also into working out how much time you save by using their device – which they use as a global tally to show how much time the world is saving by removing the need to untangle headphones.

This is a really novel idea and a cool device.  Head over to BudTrap.com now to start saving your tangle time.

NRL Live on your iPhone – Finally!

I’m not sure about you, but we’re well into the 2009 NRL season and the lack of a decent dedicated NRL app has been something that has bugged me.  The AFL has had dedicated iPhone Apps since the start of the season, while the NRL has been stuck with rubbish apps that simply link to websites.

Finally, WSP Online has released League 2009 – NRL.  This provides the same beautiful interface as their successful Footy 2009 app, along with live coverage of the NRL season including:

  • Live Scores for all games in the 2009 NRL season
  • News – with the fully article viewable inside the App, fully licensed by the ABC
  • Fixture – full fixture with game time and venue
  • Ladder – ladder is updated live after each game

See screen shots below:

img_0030 img_0029

img_0033 img_0028

It sells for $3.99 in the App Store and is well worth the purchase price.

How-To: Move change the address of a SVN repository

So you’ve changed where you host your subversion repository for a particular project and you want to update your local copy so that it points at the correct address?   How do you do this?  Simple:

svn switch --relocate http://oldserver.com/PROJECT 
                      http://newserver.com/PROJECT

This will recursively modify all the .svn folders contents so they point to the correct location.