How can I get the Default Application name for a given file

Right clicking a file in Finder and selecting ‘Get Info’ shows the Default App associated with the file.

Is there some command I can use to find out what the Default App for a file is? I’d hoped I could ask mdls for this but no luck.

May be GetAppThatWillOpenFolderItem (macoslib needed)?

LaunchServices give you this information. If you use MBS:

[code]dim file,appf as FolderItem

file=SpecialFolder.Desktop.Child(“d.rb”) // some Realbasic file
appf=LaunchServicesApplicationForItemMBS(file, 0)

MsgBox appf.AbsolutePath[/code]

Thanks a lot for the pointers!
I don’t use MBS and MacOSLib but it put me on the right track.

For future searchers, I was able to make this FolderItem Extend that returns a FolderItem of the Default App for the File (or Nil if nothing).
Not sure if this is the right way but it seems to work for me.

[code]Function fiDefaultAppForFile(Extends f As FolderItem) As FolderItem
#If TargetCocoa Then

Declare Function NSClassFromString Lib "Foundation"(aClassName As CFStringRef) As Ptr
Declare Function URLForApplicationToOpenURL Lib "Foundation" selector "URLForApplicationToOpenURL:"(obj As Ptr, url As Ptr) As Ptr
Declare Function sharedWorkspace Lib "Foundation" selector "sharedWorkspace"(ref As Ptr) As Ptr
Declare Function URLWithString Lib "Foundation" selector "URLWithString:"(ref As Ptr, str As CFStringRef) As Ptr
Declare Function absoluteString Lib "Foundation" selector "absoluteString"(obj As Ptr) As CFStringRef
Dim AppAbsPath As String = absoluteString(URLForApplicationToOpenURL(sharedWorkspace(NSClassFromString("NSWorkspace")), URLWithString(NSClassFromString("NSURL"), f.UrlPath)))
Return GetFolderItem(AppAbsPath, FolderItem.PathTypeURL)

#EndIf
End Function
[/code]

You beat me to it, this is what I’ve been using. I’m fairly confident it’s right… lol

That’s good to hear!
I wasn’t sure about the absoluteString step and got stuck searching for a more direct way.

I don’t know if it’s the best way, but it’s what I’ve been using to convert NSURLs to Xojo folderitems for years.

The only thing that’s done differently is that I use the following for creating NSURLs from folderitems, passing in folderitem.directory.

Function NSURLFromPath(nativePath as string, directory as boolean) As Ptr #if TargetCocoa then declare function fileURLWithPath lib "Foundation" selector "fileURLWithPath:isDirectory:" ( NSURLClassRef as Ptr, filePath as CFStringRef, isDir as boolean ) as Ptr return fileURLWithPath( NSClassFromString( "NSURL" ), nativePath, directory ) #endif End Function

I don’t know if it technically makes any difference or not.