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

john903

macrumors member
Original poster
Apr 11, 2008
66
0
How do you have your program associate certain file types by default with it? So, if you have file.xyz 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?

Thanks!
 
Hi john903.

To set the application:

Click on your file (file.xyz) 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.
 
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 file.xyz that opens my program. How do I get the path to that file in my program?
 
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?

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.

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

In Carbon, you'll receive an AppleEvent telling you that a document was opened. There is probably sample code available at developer.apple.com. Cocoa will open the document automatically.
 
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!
 
One more question. How do you assign an icon to your associated files?
 
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.
 
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.

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.

http://developer.apple.com/document...c/instm/NSDocument/readFromData:ofType:error:
 
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.
 
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.

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.
 
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.
 
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.
if(!pApp_G)
{
// store it in an array (or just set an NSString)
m_openedFilesArray = [NSMutableArray arrayWithCapacity: 1];
[m_openedFilesArray addObject: filename];
}
else
{
//handle the file opening directly here
}

return TRUE;
}

Hope this helps some of you that have also had this issue.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.