Created
March 31, 2010 00:06
-
-
Save atr000/349767 to your computer and use it in GitHub Desktop.
cli tool to display birthdays. good for geektool
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/**************************************************************************************/ | |
// Copyright 2004 by Sven Janssen. | |
// Small app to get the birtday out of Apple Addressbook. | |
// Used to show them with GeekTool on my desktop | |
// gcc -framework Foundation -framework AddressBook -O2 -o birthday source/main.m | |
// | |
/**************************************************************************************/ | |
#import <Foundation/Foundation.h> | |
#import <AddressBook/AddressBook.h> | |
#include <unistd.h> | |
void showOptions() { | |
printf( | |
"Birthday Reminder 1.0 by Sven Janssen <[email protected]>\n" | |
"Small application to show your Apple Address Book birthdays on the command line\n" | |
"Best used with GeekTool\n\n" | |
"Options:\n" | |
"-O show your own address book card\n" | |
"-p num display n persons. Default = 5\n" | |
"-h/-H/-? show this help\n" | |
); | |
} | |
int main(int argc, char *argv[]) | |
{ | |
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; | |
NSArray *people = [[ABAddressBook sharedAddressBook] people]; | |
NSMutableArray *birthdayArray; | |
int myc,i,j,age; | |
int personCount = 5; // How many person shoud I display. Default = 5 | |
BOOL showOwnCard = false; // Do not show your own card. | |
birthdayArray = [NSMutableArray arrayWithObject:@""]; // Clear Array | |
//Check arguments | |
while ((i=getopt(argc,argv,"hH?Op:"))!=-1) | |
switch (i) { | |
case '?': | |
case 'H': | |
case 'h': | |
showOptions(); | |
[pool release]; | |
return 0; | |
case 'p': | |
personCount = strtol(optarg,NULL,0); | |
break; | |
case 'O': | |
showOwnCard=true; | |
break; | |
default: | |
break; | |
} | |
//get today | |
NSString * today=[NSString stringWithString:[[NSDate date] descriptionWithCalendarFormat:@"%m%d" | |
timeZone:nil locale:[[NSUserDefaults standardUserDefaults] dictionaryRepresentation]]]; | |
myc=0; | |
for (i = 0; i < [people count]; i++) { | |
ABPerson *person = [people objectAtIndex:i]; | |
NSDate *bday = [person valueForProperty:kABBirthdayProperty]; | |
if(bday == NULL ) continue; // Person has no birthday | |
if(person == [[ABAddressBook sharedAddressBook] me] && !showOwnCard) continue; // Do not show own card | |
NSString * bdaystring=[NSString stringWithString:[bday descriptionWithCalendarFormat:@"%m%d" | |
timeZone:nil locale:[[NSUserDefaults standardUserDefaults] dictionaryRepresentation]]]; | |
// Calculate the age | |
age = [[[NSDate date] descriptionWithCalendarFormat:@"%Y" timeZone:nil locale:[[NSUserDefaults standardUserDefaults] dictionaryRepresentation]] intValue]-[[bday descriptionWithCalendarFormat:@"%Y" timeZone:nil locale:[[NSUserDefaults standardUserDefaults] dictionaryRepresentation]] intValue]; | |
//Enter persopn to birthday Array | |
[birthdayArray addObject:[NSString stringWithFormat:@"%02d-%@ (%2d) : %c %@ %@ %c", | |
[bdaystring compare:today] == NSOrderedAscending ? 1 : 0, //Check if birthday is befor or after today. Need to sort correct | |
[bday descriptionWithCalendarFormat:@"%m-%d" timeZone:nil locale:[[NSUserDefaults standardUserDefaults] dictionaryRepresentation]], | |
age+([bdaystring compare:today] == NSOrderedAscending ? 1 : 0), | |
[bdaystring isEqualToString:today] ? '*' : ' ', // Check if persons birthday is today and mark it | |
[person valueForProperty:kABFirstNameProperty] == NULL ? @"" : [person valueForProperty:kABFirstNameProperty], | |
[person valueForProperty:kABLastNameProperty] == NULL ? @"" : [person valueForProperty:kABLastNameProperty], | |
[bdaystring isEqualToString:today] ? '*' : ' ' // Check if persons birthday is today and mark it | |
]]; | |
myc++; | |
} | |
// Sort the person array with a simple Bubblesort | |
NSString *tmp; | |
for (i=[birthdayArray count]; i>=0; i--) | |
for (j=1; j<i; j++) | |
if(strcmp([[birthdayArray objectAtIndex:j-1] cString],[[birthdayArray objectAtIndex:j] cString])>0) { | |
tmp = [birthdayArray objectAtIndex:j-1]; | |
[birthdayArray replaceObjectAtIndex:j-1 withObject:[birthdayArray objectAtIndex:j]]; | |
[birthdayArray replaceObjectAtIndex:j withObject:tmp]; | |
} | |
// Remove first three chars, which we used to sort the array | |
for (i=1; i<[birthdayArray count]; i++) | |
if([(NSString *)[birthdayArray objectAtIndex:i] length] >3) | |
[birthdayArray replaceObjectAtIndex:i withObject:[[birthdayArray objectAtIndex:i] substringFromIndex:3]]; | |
//for (i=[birthdayArray count]-1;i>=personCount+1;i--) [birthdayArray removeObjectAtIndex:i]; // Remove not needed persons | |
if(myc>personCount) myc=personCount; | |
for(i=1;i<=myc;i++) printf("%s\n",[[birthdayArray objectAtIndex:i] UTF8String]); // Display Person | |
// clean up | |
[pool release]; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment