Getting the version number of another application

Discussion in 'Mac Programming' started by Nutter, Nov 23, 2006.

  1. Nutter macrumors 6502

    Joined:
    Mar 31, 2005
    Location:
    London, England
    #1
    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
     
  2. Nutter thread starter macrumors 6502

    Joined:
    Mar 31, 2005
    Location:
    London, England
    #3
    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...
     
  3. HiRez macrumors 603

    HiRez

    Joined:
    Jan 6, 2004
    Location:
    Western US
    #4
    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.
     
  4. Nutter thread starter macrumors 6502

    Joined:
    Mar 31, 2005
    Location:
    London, England
    #5
    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.
     
  5. HiRez macrumors 603

    HiRez

    Joined:
    Jan 6, 2004
    Location:
    Western US
    #6
    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.
     
  6. cowgp macrumors newbie

    Joined:
    Sep 19, 2005
    #7
    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
     
  7. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #8
    Are you using the exact same code as above? If not, post the code.
     
  8. cowgp macrumors newbie

    Joined:
    Sep 19, 2005
    #9
    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
     

Share This Page