Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Nutter

macrumors 6502
Original poster
Mar 31, 2005
432
0
London, England
Hi all, I have a quick question,

I want to find the version number of another application (iTunes). At the moment, I'm using this code:

Code:
NSBundle *iTunesBundle = [NSBundle bundleWithPath:MBiTunesPath];	// nil if iTunes isn't at the expected path
NSDictionary *iTunesInfo = [iTunesBundle infoDictionary];
return [iTunesInfo objectForKey:@"CFBundleVersion"];

Is there any way of doing this without making an assumption about the path of the application? I'd love to use the bundle identifier for this, but [NSBundle bundleWithIdentifier:] apparently only works if an instance of NSBundle representing that bundle has already been created.

If anyone can help I'd be grateful!

Thanks,

Milo
 

Nutter

macrumors 6502
Original poster
Mar 31, 2005
432
0
London, England
Thanks a lot. Here's what I've whipped up:

Code:
NSURL *iTunesURL;
if (LSFindApplicationForInfo(kLSUnknownCreator,(CFStringRef)MBiTunesBundleIdentifier,NULL,NULL,(CFURLRef *)&iTunesURL) == kLSApplicationNotFoundErr)
	return nil;
NSBundle *iTunesBundle = [NSBundle bundleWithPath:[iTunesURL path]];
NSDictionary *iTunesInfo = [iTunesBundle infoDictionary];
[iTunesURL release];
return [iTunesInfo objectForKey:@"CFBundleVersion"];

I'm afraid my C is a bit rusty and I know nothing about CF, but it seems to work. I'm releasing iTunesURL because the documentation for LSFindApplicationForInfo says "Despite the absence of the word Copy in its name, this function retains the URL reference object on your behalf; you are responsible for releasing this object." Am I doing that right?

Thanks,

Milo

Edit: Now it actually works...
 

HiRez

macrumors 603
Jan 6, 2004
6,250
2,576
Western US
That should work fine, but you could stay all in Cocoa and get it done as well with something like this:
Code:
+ (NSString*)versionStringForApplication:(NSString*)appName {
	NSBundle *iTunesBundle = [NSBundle bundleWithPath:[[NSWorkspace sharedWorkspace] fullPathForApplication:appName]];
	NSDictionary *iTunesInfo = [iTunesBundle infoDictionary];
	return [iTunesInfo objectForKey:@"CFBundleVersion"];
}
And then call it with NSString *versionString = [myAppController versionStringForApplication:mad:"iTunes"];

where "myAppController" would be whatever object you decide to put it in. I made it a class method, but you could make it an instance method. Not sure what would happen if you happened to have two different versions of the same application on your drive though.
 

Nutter

macrumors 6502
Original poster
Mar 31, 2005
432
0
London, England
Hey thanks, that's exactly what I was hoping to find! I was looking in the NSWorkspace documentation for just such a method ... obviously not hard enough...

I think the API takes care of choosing the most appropriate copy of the application to launch, based mainly on which is the latest version, in the same way that a copy of the application is chosen when you open a document in the finder. At least, I hope so.
 

HiRez

macrumors 603
Jan 6, 2004
6,250
2,576
Western US
Glad that works for you. I did realize I messed up because myAppController sounds like an instance, so it probably should be AppController or something. Even better, you could just add the method as a category onto NSWorkspace.
 

cowgp

macrumors newbie
Sep 19, 2005
6
0
Looking for an updated answer on this thread... I'm a newbie at cocoa programming but this thread is exactly what I want to get done. However I can't ever seem to get the bundleWithPath to return something other than nil.

I know I have a valid path for iTunes cause I can launch it...

Anyone have any corrections to this example, or other suggestions on how I might find out the version number of iTunes?

thanks a bunch.

_cowgp
 

cowgp

macrumors newbie
Sep 19, 2005
6
0
OK, I swear it didn't work.... I was just removing some commented lines I had put in to clean it up to post here, and ran the app, and now it works as advertised. I dont get it... but it now works. now I just have to hope reliably, but given my hours last night of it not working, I'm nervous about that... but I guess I just have to chock that up to me being a newbie and assume I was in fact doing something wrong.

Thanks for at least piping in for support kainjow.

_cowgp
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.