Skip to content

Instantly share code, notes, and snippets.

@d0k
Created April 29, 2009 13:47
Show Gist options
  • Save d0k/103780 to your computer and use it in GitHub Desktop.
Save d0k/103780 to your computer and use it in GitHub Desktop.
Safari history cleaner written in Objective C 2.0
/* Safari History.plist cleaner
* compile with:
* gcc historyclean.m -o historyclean
*/
#import <Foundation/Foundation.h>
int main(int argc, char *argv[]) {
if (argc != 3) {
fprintf(stderr,
"Usage:\n"
"\t %s [history] [keyword]\n\n"
"example: %s ~/Library/Safari/History.plist google\n"
"\t removes all URLs containing google from the history\n",
argv[0], argv[0]);
return 1;
}
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSString *path = [NSString stringWithUTF8String:argv[1]];
NSString *arg = [NSString stringWithUTF8String:argv[2]];
NSData *plistData = [NSData dataWithContentsOfFile:path];
NSPropertyListFormat format;
NSMutableArray *plist = [NSPropertyListSerialization
propertyListFromData:plistData
mutabilityOption:NSPropertyListMutableContainers
format:&format
errorDescription:nil];
NSMutableArray *filtered = [[NSMutableArray alloc] init];
NSArray *webHistoryDates = [plist valueForKey:@"WebHistoryDates"];
NSUInteger oldentries = [webHistoryDates count];
fprintf(stderr, "Old size:\t%lu entries\n", oldentries);
for (NSArray *element in webHistoryDates) {
if ([[element valueForKey:@""] rangeOfString:arg].location==NSNotFound)
[filtered addObject:element];
}
[plist setValue:filtered forKey:@"WebHistoryDates"];
plistData = [NSPropertyListSerialization dataFromPropertyList:plist
format:format
errorDescription:nil];
[plistData writeToFile:path atomically:YES];
NSUInteger newentries = [filtered count];
fprintf(stderr, "New size:\t%lu entries\n", newentries);
fprintf(stderr, "Delta:\t\t-%lu\n", oldentries-newentries);
#ifndef NDEBUG
[filtered release];
[pool release];
#endif
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment