Determining File Handlers on OSX

Is there a way, given a filetype of determining the default handler for the file and launching the application with the file.

For instance if I had a folder item which had test.xls as the file, can I determine if the user has an xls file handler and attempt to launch it with the file I have in the folder item.

Its late, I hope that made sense…

for OSX
Xojo Example Projects Folder -> Files -> MacOSXFSRefs

With declares (untested):

[code]declare function NSClassFromString lib “Foundation” (clsName as cfstringref) as ptr
declare function sharedWorkspace lib “Foundation” selector “sharedWorkspace” (clsRef as ptr) as ptr
declare function openFile lib “Foundation” selector “openFile:” (obj_id as ptr, file as cfstringref) as boolean

// f is the folder item to open

if openFile(sharedWorkspace(NSClassFromString(“NSWorkspace”)),f.path) then
//successfully opened the file
else
//failed to open the file
end if[/code]

[quote=159245:@Axel Schneider]for OSX
Xojo Example Projects Folder -> Files -> MacOSXFSRefs[/quote]
Aren’t all FS functions deprecated?

They are and in 10.10 some are missing, which is why Xojo can no longer resolve Aliases.

In this circumstance you could just try folder item.launch

I think he wants to know the name of the app that opens the file

An in that case, Jason is close… But the May want the following instead.

  • (NSURL *)URLForApplicationToOpenURL:(NSURL *)url

I’m not at my computer, otherwise I’d translate this one. You give it a file and it returns the default application to open said file.

I thought that he wanted to get the name of the app which opens the file and then use that information to launch the file. Since you don’t need to know the application to attempt to open the file I ignored that part. If he wants the app name then I think he can do (using Sam’s function, and including the same declares from above):

declare function URLForApplicationToOpenURL lib "Foundation" selector "URLForApplicationToOpenURL:" (obj_id as ptr, url as ptr) as ptr declare function URLWithString lib "Foundation" selector "URLWithString:" (clsRef as ptr, str as cfstringref) as ptr declare function lastPathComponent lib "Foundation" selector "lastPathComponent" (obj_id as ptr) as cfstringref dim appURL as ptr = URLForApplicationToOpenURL(sharedWorkspace(NSClassFromString("NSWorkspace")),URLWithString(NSClassFromString("NSURL"), f.Path)) dim appName as Text = lastPathComponent(appURL)

Thanks, I have fixed some small mistakes (f.path)

  Dim f As FolderItem = GetOpenFolderItem("")
  
  declare function NSClassFromString lib "Foundation" (clsName as cfstringref) as ptr
  declare function sharedWorkspace lib "Foundation" selector "sharedWorkspace" (clsRef as ptr) as ptr
  declare function openFile lib "Foundation" selector "openFile:" (obj_id as ptr, file as cfstringref) as boolean
  
  declare function URLForApplicationToOpenURL lib "Foundation" selector "URLForApplicationToOpenURL:" (obj_id as ptr, url as ptr) as ptr
  declare function URLWithString lib "Foundation" selector "URLWithString:" (clsRef as ptr, str as cfstringref) as ptr
  declare function lastPathComponent lib "Foundation" selector "lastPathComponent" (obj_id as ptr) as cfstringref
  dim appURL as ptr = URLForApplicationToOpenURL _
  (sharedWorkspace(NSClassFromString("NSWorkspace")),URLWithString(NSClassFromString("NSURL"), f.UrlPath))
  dim appName as String = lastPathComponent(appURL)
  
  OpenFileLabel.Text = appName

Thank you all very much

f.launch did the trick but there is some very useful code above which I will keep hold of. Thanks again.

I have changed the old example,
maybe Xojo can use it as a replacement for MacOSXFSRefs.

AppForFile