How to reveal a file on desktop

from David Cox

(How do you reveal a file? - #3 by David_Cox - macOS - Xojo Programming Forum)

Here is what I use:

Protected Sub doRevealFolderItem(f As FolderItem)
  'www.xojoitaliablog.com/evidenziare-un-file-sul-desktop
  If f = Nil Or Not f.Exists Then Return
  #If TargetDesktop Then
    If f.IsFolder Then
      'if f.Name.IndexOf(0, ".") >= 0 then f = f.parent 'dot in folder name therefore reveal the parent e.g. pages
      If f.isBundleMBS Then f = f.Parent
      f.Open(True)
      Return
      
    Else
      #If TargetMacOS Then
        '[Fix] Reveal in Finder now highlights the file itself, not just opens the folder, 
        'using shell.execute(""open --reveal /path/to/my\ file"") or Var ok as Boolean, ok = sendItemAE(""misc"", ""mvis"", ""com.apple.finder"", f) or NSWorkSpaceMBS.selectFile"
        'tempString = "open -R """ + f.NativePath + """"
        'tempShell.Execute(tempString)
        'if tempShell.ExitCode <> 0 then
        'f.parent.Open
        'end if
        'tempShell
        'Return
        
        Call NSWorkSpaceMBS.selectFile(f)
        Return
        
      #ElseIf TargetWindows Then
        Var tempShell As New Shell 'used by Linux
        Var tempString As String 'used by Linux
        
        'returnValue = WinOpenFolderAndSelectItemsMBS(f.parent, Array(f))
        'if returnValue <> 0 then
        'f.parent.Open
        'end if
        
        'tempString = "explorer.exe /select," + f.NativePath 'not revealing folder for MVV
        'tempString = "explorer.exe /select,""" + f.NativePath + """"
        tempString = "explorer.exe /root,/separate,/select,""" + f.NativePath + """" 'run on separate new process
        tempShell.Execute(tempString)
        'If tempShell.ExitCode <> 0 Then
        If tempShell.ExitCode > 1 Then 'Error 1 seems to be success, so don't open the window twice!
          f.Parent.Open(True)
        End If
        tempShell.Close
        
        Return
        
      #ElseIf TargetLinux Then
        Var tempShell As New Shell 'used by Linux
        Var tempString As String 'used by Linux
        
        tempString = "xdg-open """ + f.Parent.NativePath + """"
        tempShell.Execute(tempString)
        If tempShell.ExitCode <> 0 Then
          f.parent.Open
        End If
        Return
        
      #Else
        f = f.Parent
        f.Open
      #EndIf
    End If
  #EndIf
End Sub

Christian Schmitz

(How do you reveal a file? - #4 by Christian_Schmitz - macOS - Xojo Programming Forum)

For Windows, you can use WinOpenFolderAndSelectItemsMBS function in MBS Xojo Plugin, which may be better than your explorer.exe trick.

from Grzegorz Pawlik
I’m using the following in macOS:

Public Sub showInFinder(f as FolderItem)
  declare function objc_getClass lib "libobjc.dylib" (name as CString) 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
  var workspace as ptr = sharedWorkspace(objc_getClass("NSWorkspace"))
  try
    call selectFile(workspace, f.NativePath, "")
  catch
    System.DebugLog("showInFinder: error")
  end try
End Sub