associate file types

Discussion in 'Mac Programming' started by john903, May 12, 2008.

  1. john903 macrumors member

    Apr 11, 2008
    How do you have your program associate certain file types by default with it? So, if you have how do I tell Mac to associate any *.xyz file with your program even if the file was just downloaded from the internet and not created by your program?

    Also, when you double click an associated file that opens your program, how do you get the path to that file?

  2. Doctor Q Administrator

    Doctor Q

    Staff Member

    Sep 19, 2002
    Los Angeles
    Hi john903.

    To set the application:

    Click on your file ( to select it. Pick "Get Info" from the File menu (or press Command-I) to open the Info window for that file. Find the section named "Open with". Pick an application in the menu or pick "Other..." and navigate to the application that you want to have open files with that extension. Click the "Change All..." button, read the prompt, and click "Continue."

    To find the path to an openeed file (for most applications):

    Hold the Command :)apple:) key while clicking and holding on the title bar of the window for that file.

    With some applications, you can pick File ->Save As and it will go to the folder where the file resides, but that's less foolproof.
  3. john903 thread starter macrumors member

    Apr 11, 2008
    Is there a way to do this programmatically through a carbon call so once they load my program I could have it associate all *.xyz by default?

    For the second part, what I mean is how do I get the path? So, if a user clicks that opens my program. How do I get the path to that file in my program?
  4. gnasher729 macrumors P6


    Nov 25, 2005
    Doing that would be considered very, very rude. If you put the right things into your plist then the OS will know that you can open .xyz file and start your application if nobody else does, but it won't change the default unless the user says so.

    In Carbon, you'll receive an AppleEvent telling you that a document was opened. There is probably sample code available at Cocoa will open the document automatically.
  5. yeroen macrumors 6502a


    Mar 8, 2007
    Cambridge, MA
    Sadly, it's a common practice.
  6. john903 thread starter macrumors member

    Apr 11, 2008
    Ok, thank you for the additional information! Can you give me more about what right things to put into your plist? I'm coming from Windows so I'm still a little unsure about the Mac way to do things. Thanks!
  7. john903 thread starter macrumors member

    Apr 11, 2008
    One more question. How do you assign an icon to your associated files?
  8. Krevnik macrumors 68040


    Sep 8, 2003

    That is a document (Cocoa oriented, but it works for Carbon too) on how you can use XCode to edit the file associations in a table rather than hand-editing your Info.plist file.

    So select your target, get info, and go to the properties tab.
  9. john903 thread starter macrumors member

    Apr 11, 2008
    Thank you for the link!

    I'm still unable to find the event that gets called when you open a file associated with your program that opens it. I'm using cocoa.
  10. Krevnik macrumors 68040


    Sep 8, 2003
    In Cocoa, the class and NIB for the document type is automatically loaded and opened for you. You should associate the file type with the right NSDocument subclass you want to use. Your class will get created, and then told to load the data from an NSData object.
  11. john903 thread starter macrumors member

    Apr 11, 2008
    Is there a way to do this that will support pre-10.4 systems? That method says it will only support 10.4 and later.

    Also, I don't understand what you mean by automatically load and open the file for you. I associated the file type with my program which worked with the icon and everything but I have custom file routines to handle my files. I just want to get the path to the file. I'm using Cocoa as a front-end and doing everything else in c++. It's an OpenGL project.
  12. Krevnik macrumors 68040


    Sep 8, 2003
    Look at the NSDocument reference, there are variations that work on pre-10.4 systems. Dig in a bit and your answer is right there in the same class reference document I linked in my last post.

    And Cocoa will not parse the file for you, but rather in the method I suggested, it passes you the raw contents of the file in an NSData object, other variations give you the NSURL to the file itself and let you load it from the filepath.
  13. john903 thread starter macrumors member

    Apr 11, 2008
    Thank you for all the help! The function I was looking for was called readFromURL for anyone stumbling on this thread with the same problem.
  14. mmcelhinney macrumors newbie

    Sep 10, 2008
    Just a note about the problem of getting only the file path when a file is double clicked. This is particularly relevant if you have an application that is not NSDocument based, but can still open files.

    Once you have the file associations set up as mentioned in this thread (by setting your pList corrently) then double clicking a file will call

    - (BOOL)application: (NSApplication * )theApplication openFile: (NSString * )filename;

    of your application delegate.

    However, if double clicking that file causes your app to launch, then this function is actually called before your
    - (void)applicationDidFinishLaunching: (NSNotification * )aNotification
    method. So, if you have significant initialization in the applicationDidFinishLaunching you should save the filename variable into a member variable of your delegate, and process it once the initialization is complete.

    - (BOOL)application: (NSApplication * )theApplication openFile: (NSString * )filename
    // if the file opening launches us, we have not initialized pApp_G, or our controllers, etc
    // so we we need to store the launch path and then load the file later.
    // store it in an array (or just set an NSString)
    m_openedFilesArray = [NSMutableArray arrayWithCapacity: 1];
    [m_openedFilesArray addObject: filename];
    //handle the file opening directly here

    return TRUE;

    Hope this helps some of you that have also had this issue.

Share This Page