Threads in Cocoa using ThreadWorker

ThreadWorker is a class makes it easy to offload a task to another thread and notify your application when you’ve completed the task. Creating multithreaded applications with ThreadWorker it’s easier than Objective-C in Mac OS X’s Cocoa framework raw code.

Calling ThreadWorker looks something like this:

[ThreadWorker workOn:self
                  withSelector:@selector(longTask:)
                  withArgument:someData
                  didEndTarget:self
                  didEndSelector:@selector(longTaskFinished:)];

In this case, longTask would be run in another thread, and longTaskFinished would be called on the original calling thread when longTask is finished.

Download example XCode Proj + API Documentation

ThreadWorker was release under Public Domain!

Clean string from HTML, removing all HTML tags

Here is a very nice and usefull function made by the guys back at cocoa.karelia.com that simply let you extract all the strings/text inside HTML tag…in other words it strips out the text from whithin the code.
Well now you may ask…”Why would I need something like this?”. For example: you want to create an app that geathers information from a website…lets say www.somerandomwebsite.com/index.html. Inside the index.html file there is a div tag with id: “news” that I would like to grab, but this div tag has a lot of HTML tags in it (like span, b, ul/li and so on) and it would take me a lot of time and effort to clean it out. Now this is the part where this function comes in handy…it does all the work for you.

Code:

- (NSString *) flattenHTML {
	NSString *result = self;
	if (![self isEqualToString:@""])	// if empty string, don't do this!  You get junk.
	{
		// HACK -- IF SHORT LENGTH, USE MACROMAN -- FOR SOME REASON UNICODE FAILS FOR "" AND "-" AND "CNN" ...
		int encoding = ([self length] > 3) ? NSUnicodeStringEncoding : NSMacOSRomanStringEncoding;
		NSAttributedString *attrString;
		NSData *theData = [self dataUsingEncoding:encoding];
		if (nil != theData)	// this returned nil once; not sure why; so handle this case.
		{
			NSDictionary *encodingDict = [NSDictionary dictionaryWithObject:[NSNumber numberWithInt:encoding] forKey:@"CharacterEncoding"];
			attrString
				= [[NSAttributedString alloc]
					initWithHTML:theData documentAttributes:&encodingDict];
			result = [[[attrString string] retain] autorelease];	// keep only this
			[attrString release];	// don't do autorelease since this is so deep down.
		}
	}
	return result;
}

For more info and license information please visit http://cocoa.karelia.com

iPhone UI in Cocoa desktop applications using UMEKit

Well now… how many of you ever wanted to use the iPhone UI in a Cocoa desktop application….well I did! For all of you out there (like me) let me present you UMEKit (pronounced “you, me kit”). UMEKit  is an open source (liberal Apache-license) Mac OS X Framework written by Todd Ditchendorf for creating iPhone-like user interfaces.

UMEKit is cool because the API is almost completely identical to Apple’s UIKit from iPhone OS, meaning iPhone developers already know the API, and can easily port existing iPhone UI code over to the desktop.

Where’s the source?

Here: UMEKit on GitHub.

What are the requirements?

  1. Runs on Mac OS X Leopard or later

Simple drawing tutorial in Cocoa

Hello guys !
Here is a nice tutorial on how to draw and fill with gradient a simple object.

More >

Vampire Power…stop the sucking !!!

Just as Count Dracula preys upon the innocent, Vampire Power or Vampire Energy, or the energy drawn from items like electronic devices that are plugged in but not in use, drains “blood” from the energy grid wasting 10 billion dollars annually in the U.S. alone. So check out this video and make sure that the “sucking” in your house stops from now on :) .

Creating a toolbar item with drop-down menu in Interface Builder

I found a very nice tutorial to create a simple but yet very usefull drop-down menu in the toolbar. It’s a nice trick oh and yeah….no code needed :)

SwarmQuery 1.2 is now available!

swarmqueryFinaly after more then a month since the last release we managed to release another version of SwarmQuery. We did a lot of improvements in this version. Please feel free to download and test it, and let us know if you find bugs or a good ideas to improve even better this app.

Release log:

  • [ADD] If torrents size is bigger then 1000Mb is transformed in Gb
  • [ADD] New tracker: Newtorrents.info, Vertor.com, RARBG.com
  • [ADD] Progress animation while searching and indexing found items
  • [FIX] Some bugs with the updates version checker
  • [ADD] Threaded torrent searches and displaying (runs faster)
  • [FIX] UI redesign
  • [ADD] Now you can download your favourite torrents using the Magnet link
  • [ADD] Slider to increase or decrease the found items

1266754682_kthememgrDownload

PC EFI v10.1

Just a small fix for booting system without DSDT.aml

system was stalling on motherboards like gigabyte, where bootloader fails to find pointer to acpi 2.0 table, fixed.

if you dont have the problem – no need to update.

Download bootloader – here

Download source patch from previous version – here

Adding Growl Notifications

It’s pretty easy adding growl notifications to your app. Download the SDK from growl.info. Set up your Xcode project to copy the Growl.framework into your application bundle.

Pick a class to be the contact point with Growl. Your AppController class is a good place. Import

Set the delegate to the GrowlApplicationBridge:

?View Code OBJECTIV-C
1
    [GrowlApplicationBridge setGrowlDelegate: self];

Doing this will eventually have the registrationDictionaryForGrowl delegate message called. Return a dictionary with two arrays (Which can be the same). These are the names of the alerts you will be posting. These are human-readable, so you’ll want to use a localized string (which I’ve already set in the global variable here:

?View Code OBJECTIV-C
1
2
3
4
5
6
7
8
9
10
11
12
13
14
- (NSDictionary *) registrationDictionaryForGrowl
{
    NSArray *notifications;
    notifications = [NSArray arrayWithObject: g_timesUpString];
 
    NSDictionary *dict;
 
    dict = [NSDictionary dictionaryWithObjectsAndKeys:
                             notifications, GROWL_NOTIFICATIONS_ALL,
                         notifications, GROWL_NOTIFICATIONS_DEFAULT, nil];
 
    return (dict);
 
} // registrationDictionaryForGrowl

And use this to post a notification:

?View Code OBJECTIV-C
1
2
3
4
5
6
7
[GrowlApplicationBridge notifyWithTitle: @"Woop!  Time has expired!"
                        description: @"You have been waiting for 37 minutes"
                        notificationName: g_timesUpString
                        iconData: nil
                        priority: 0
                        isSticky: NO
                        clickContext: nil];

Consult the SDK documentation for more explanations of the features, but they are pretty self-explanitory.

Finding things like ~/Library, and ~/Library/Application Services & current user’s name

NSSearchPathForDirectoriesInDomains is how you find the location of things like Library directories, or User directories, and the like (this is the NSSearchPathDirectory). The NSSearchPathDomainMask is what domains to find things in. For instance for a NSLibraryDirectory, a NSUserDomainMask will give you the path to ~/Library, NSSystemDomainMask will give you the path to /System/Library, and so on.

The directories inside of Library, like “Preferences” and “Application Support” are in English in the file system, and the Finder presents localized versions to the user. If you need ~/Library/Application Support/Borkware, you can construct it like

?View Code OBJECTIVE-C
1
2
3
4
5
6
7
8
9
10
11
12
13
14
   NSMutableString *path;
    path = [[NSMutableString alloc] init];
 
    // find /User/user-name/Library
    NSArray *directories;
    directories = NSSearchPathForDirectoriesInDomains (NSLibraryDirectory,
                                                       NSUserDomainMask, YES);
 
    // if you had more than one user domain, you would walk directories and
    // work with each path
    [path appendString: [directories objectAtIndex: 0]];
 
    [path appendString: @"/Application Support"];
    [path appendString: @"/Borkware"];

As for the curent user’s name:

?View Code OBJECTIVE-C
1
NSUserName() or NSFullUserName()