Reveal in Finder (Mac OS X)

How can I reveal a file in Finder on MacOS?

There used to be this:
ActivateFinder (f.AbsolutePath)

I know that I should use NativePath instead of AbsolutePath, but is there a simple way to reveal a file in the Finder?

Thanks,
Michael

You can use selectFile in NSWorkspaceMBS class with MBS Xojo Plugins:

[code] dim f as FolderItem

f=SpecialFolder.Desktop.Child(“test.txt”)

if NSWorkspaceMBS.selectFile(f) then
MsgBox “Ok”
else
MsgBox “failed”
end if[/code]

Thank you, Christian. Are you saying there is no easy way to do this without a plugin?

Search for the declare line or an AppleScript…

[quote=487924:@Michael Reade]How can I reveal a file in Finder on MacOS?

There used to be this:
ActivateFinder (f.AbsolutePath)

I know that I should use NativePath instead of AbsolutePath, but is there a simple way to reveal a file in the Finder?

Thanks,
Michael[/quote]

[code]Protected Sub ShowInFinder(theItem as Folderitem)
#If TargetMacOS
Declare Function NSClassFromString Lib “Cocoa” (name As CFStringRef) As Ptr
Declare Function sharedWorkspace Lib “AppKit” selector “sharedWorkspace” ( obj As ptr ) As ptr
Declare Function selectFile Lib “AppKit” selector “selectFile:inFileViewerRootedAtPath:” ( obj As ptr, fPath As CFStringRef, rootFullPath As CFStringRef ) As Boolean

Dim workspace As ptr = sharedWorkspace( NSClassFromString( "NSWorkspace" ) )

Call selectFile( workspace, theItem.NativePath, "")

#Else
#pragma Unused theItem
Dim Err As New PlatformNotSupportedException
Err.Message = CurrentMethodName + “: Is only supported on a Macintosh”
Raise Err
#Endif
End Sub[/code]

Add this to your function, Karen! :smiley:

#elseif TargetWindows then
  dim o as new Shell
  o.Execute("explorer.exe", "/select," + theItem.ShellPath)

[quote=487941:@Tim Parnell]Add this to your function, Karen! :smiley:

[/quote]

First let me say I got all the code from some place else (I did not originally write it!) and repackaged it for my use.

The mac method I posted above is not meant to be called on nay other oS, hence the exception… I argued my code differently than you may have expected.

I have my OS specific stuff in a module called OS. That is where I try to keep most of my Xplatform stuff that have OS specific underpinnings.

My OS module has Mac, Win an Linux sub modules. Each of those submodule has the methods and classes specific to that platform and when called directly should only be used on that platform.

The OS module levelX-Platform methods and classes that call into the OS specific submodules for OS specific code…

That way I can have an XPlatform API for common functionality, but still have more detailed platform specific methods in the submodules.

In this case my my Xplatform method in teh OS module is just:

[code]Public Sub ShowFile(Extends theFile As Folderitem)
#If TargetWindows
Win.ShowInExplorer(theFile.Parent.NativePath, theFile.Name)
#Endif

#if TargetMacOS
Mac.ShowInFinder(theFile)
#Endif

#if TargetLinux
Linux.ShowFile(theFile)
#endif
End Sub
[/code]

The Mac method is what I posted above;

Windows allows you select multiple files in the same folder at once, so I have bunch of overloads passing in param Arrays (or arrays) of folderItems or paths or filenames. If there is is only one item that is the target… If there are multiple items passed then the first is the folder and the rest items in that folder

All the overloads (after a little massaging) call this core method in OS.Win is:

[code]Protected Sub ShowInExplorer(path As String, files() As String)
#if TargetWindows

Declare Function CoInitialize Lib "Ole32.dll" Alias "CoInitialize" (pvReserved As Integer) As Int32
Declare Function ILCreateFromPathW Lib "Shell32.dll" Alias "ILCreateFromPathW" (pszPath As WString) As Ptr
Declare Function SHOpenFolderAndSelectItems Lib "Shell32.dll" Alias "SHOpenFolderAndSelectItems" (pidlFolder As Ptr, cidl As UInt32, apidl As Ptr, dwFlags As UInt32) As Int32
Declare Sub ILFree Lib "Shell32.dll" Alias "ILFree" (pidl As Ptr)

Call CoInitialize(0)

Dim pidl As Ptr = ILCreateFromPathW(path)
If pidl = Nil Then 
  Dim Err As NilObjectException
  Err.Message = CurrentMethodName + " Declare ILCreateFromPathW Returned NIL"
  Raise Err
End If
Static PtrSize As UInt8 =  Win.SizeOfPtr
Dim mb As New MemoryBlock(PtrSize * (files.Ubound + 1))

'Get a pidl for each file and store it
For i As Integer = 0 To files.Ubound
  mb.Ptr(i * PtrSize) = ILCreateFromPathW(files(i))
Next

Dim ok As Int32 = SHOpenFolderAndSelectItems(pidl, files.Ubound + 1, mb, 0)

ILFree(pidl)

If OK <> 0 then
  Dim Err As RuntimeException
  Err.ErrorNumber = ok
  Err.Message = CurrentMethodName + " Error #: " + Str(OK) + "  From Declare SHOpenFolderAndSelectItems"
  Raise Err
End if

#else
#pragma unused path
#pragma Unused files

Dim Err As New PlatformNotSupportedException
Err.Message = CurrentMethodName+ "  Is Windows Only"
raise Err

#endif

End Sub
[/code]

For Linux (which I have never used so don’t know if it works) the core method in OS.Linux is:

Protected Sub ShowFile(theFile as Folderitem)
  
  #if TargetLinux
    // Use xdg-open which is desktop-independent, and should be available in most newer distros
    // Otherwise fallback on the two major desktop dependent command tools
    Dim sh As New Shell
    Dim commandTools() As String = Array( "xdg-open", "gnome-open", "kde-open" )
    
    For Each tool As String In commandTools
      sh.Execute( "which " + tool )
      If Left( sh.Result, 1 ) = "/" Then
        sh.Execute( tool + " " + theFile.Parent.ShellPath )
        Exit
      End If
    Next
  #else
    #pragma Unused theFile
    Dim Err as New PlatformNotSupportedException
    Err.Message = CurrentMethodName + " may only be used on Linux"
    Raise Err
  #endif
  
End Sub

Ah wow! Glad you have a great system :smiley:

Thank you Christian, Karen & Tim. I really appreciate your help.

It’s been some years since I dived into Xojo, and I was never very proficient at it. So now I find myself relearning, but dealing with the changes that have happened to the platform during this time.

Karen & Tim: I’m a little surprised at how complicated this is. I would assume that Xojo would figured out an easier way to do it. It’s a little beyond my current abilities. But thank you very much anyway.

Christian: I tried downloading the plugins. The first 2 times Xojo froze, and afterwards it was very very slow. But thank you for your help.

I guess I really need to dive into learning Xojo before I can do this.

Thank you very much.

As it is a fairly common need, IMO this is something Xojo could and should provide as part of the framework for X- Platform desktop… But it It seems to me Xojo has not spent much time on or thinking about new X-Platform DESKTOP features in many years.

  • karen

don’t install all plugins for one function. Just the plugin parts you need.

OK! I got this to work! Got the code from another post. How can I put a link to it?

It was a reply by @Dave S to this post:
https://forum.xojo.com/45805-show-in-finder/2#p371911

And here is the code he submitted:
Dim sh As New shell
sh.Execute "open -R "+chrb(34)+f.NativePath+chr(34)