MacOS Get default application for file type - macos

I am working on a Mac app. I ultimately want to use default app icons within my app. From the Info.plist and the Resource folder of an app I can get the .icns file and convert that to the image format I need. But I need to know the default application associated with the particular file extension, if any.
So how to get the default application that the system currently associates with a given file extension?

Don't go digging in other apps' bundles. It's always best to work at the level of abstraction that suits the question you want to ask. If you want to get the icon that the Finder (or a Mail attachment, etc) would display for a file of a particular type, use the NSWorkspace iconForFileType: method.

I think what you're looking for is part of the OSX Launch Services: LSCopyDefaultApplicationURLForContentType API. This returns the info on apps that can open specific Uniform Type Identifiers. There's also a similar API called LSCopyDefaultApplicationURLForURL to check which app opens a specific known file.

Related

Check if an OS X app exists to open a particular file

Is there any way to check if a user has an app installed on their Mac? I need to check if the user has a necessary program anywhere to open a file with a particular extension.
You probably want to use NSWorkspace. See the section Manipulating Uniform Type Identifier Information about getting and converting file extensions into uniform type identifiers, then discover what applications support those identifiers.
But if you just want to see if a file can be opened by an application and what the application is, use -URLForApplicationToOpenURL:.
If you want a list of all applications that can open a file, you'll have to drop down the launch services API: LSCopyApplicationURLsForURL

Opening an app using another app

I'm just wondering is it possible?
Let's say I wrote an app and I want to use a button to open (from the app I wrote) another app (sing its directory) on my Mac
Any idea where to start off? I've been thinking about NSFileManager but it seems like a wrong way.
You should use the methods of NSWorkspace. Which method exactly depends a bit on exactly what you want to do. For example, if you have the URL for a document file and you want to open it in the appropriate app, you would use -openURL:.
If you just want to open a specific app (and not any particular document), then you should use -launchAppWithBundleIdentifier:options:additionalEventParamDescriptor:launchIdentifier:. Using a bundle identifier is the most reliable way to identify the app, rather than using its name or its URL, either of which can be changed.

How can I associate a file with my app?

I have a Cocoa app "PDFHistory" on Mac OS X that uses the NSDocument architecture to save and load PDF files that are internally formatted specially for my app. I want to make it so whenever I save a file (e.g., "mydoc.pdf") from PDFHistory, then subsequently double-clicking on mydoc.pdf will automatically open it in PDFHistory.app. However, I don't want to make it so all .pdf files are automatically opened in PDFHistory, but rather use the system default (probably Preview.app). The .pdf suffix is a requirement, though, since I need the user to be able to e-mail the files to other users who can view the file in their default PDF viewer.
The problem is that if I set the LSHandlerRank to "Owner", then all .pdf files will be opened with PDFHistory, which is bad (since I only understand the internals of the .pdf file that PDFHistory wrote out). But if I set LSHandlerRank to "Alternate", then all .pdf files will be opened to the system default app (Preview.app), which is confusing for the user who had just created the file using my app.
Once upon a time, "creator codes" could be used to implement this sort of capability, but launch services started ignoring them back in Snow Leopard (see http://tidbits.com/article/10537). UTIs are not a substitute that provide this capability (see http://boredzo.org/blog/archives/2009-09-22/how-not-to-use-utis).
Using Finder to get info on the file allows the user to specify a specific app to use to open the specific file. This supposedly works by setting a "usro" property in a the file's resource. There is some open-source code to mimic this behavior (https://github.com/AlanQuatermain/SetAppAffinity), but is uses deprecated functions, and so would cause Apple to reject the app from the App Store. Similarly, people have posted AppleScript to set this property (https://discussions.apple.com/thread/2597365), but sandboxing would prevent me from invoking it.
Although the .pdf suffix is a requirement in order to be able to send the files to users on other systems/platforms, I considered trying to have the suffix registered with two extensions as ".phistory.pdf", which would allow "file.phistory.pdf" to be opened in PDFHistory, but "file.pdf" would be opened in the default PDF viewer. However, this simply didn't work: it appears that the final suffix is the only one used by launch services, and everything before that is ignored.
So is there any way to have my app be the default app for opening files that it created itself?

inverse of LSGetApplicationForItem

I am writing a image viewer application, which I need to set as default application for jpeg/gif files. There is an API LSGetApplicationForItem/Info for getting information on what is the default application.
But I am not able to find an API where I can set my application as the default viewer for image files.
Can you please help
You want the LSSetDefaultRoleHandlerForContentType function. Pass kUTTypeGIF for GIF, kUTTypeJPEG for JPEG.
Please only do this when the user explicitly tells you to. Otherwise, they will hate you.
There's not a public API to do that, as far as I know. Instead, you register, using Info.plist (or the Document Types section of the Info editor in Xcode), that you are a provider of viewing and/or editing for whatever file types you are interested in. This will cause the system to choose your app if there's no other app who can open those file types, and will put you in the running if the user chooses Open With from the context menu when selecting a file in the Finder.

File extension icon on Mac

My application uses new proprietary file formats with extensions never been used before. I would like to associate specific icons to display my files in finder with nice iconography. As far as I know LaunchService is responsible to handle all these data, however I'm confused where, when and how shall I create associations.
Which entries I have to add to plist?
Where I need to actually register this extension - during installation? Is there any script for this?
Add a CFBundleDocumentTypes key to your plist, see
Storing Document Types Information in the Application's Property List

Resources