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)
  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
      #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"", """", f) or NSWorkSpaceMBS.selectFile"
        'tempString = "open -R """ + f.NativePath + """"
        'if tempShell.ExitCode <> 0 then
        'end if
        Call NSWorkSpaceMBS.selectFile(f)
      #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
        '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
        'If tempShell.ExitCode <> 0 Then
        If tempShell.ExitCode > 1 Then 'Error 1 seems to be success, so don't open the window twice!
        End If
      #ElseIf TargetLinux Then
        Var tempShell As New Shell 'used by Linux
        Var tempString As String 'used by Linux
        tempString = "xdg-open """ + f.Parent.NativePath + """"
        If tempShell.ExitCode <> 0 Then
        End If
        f = f.Parent
    End If
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"))
    call selectFile(workspace, f.NativePath, "")
    System.DebugLog("showInFinder: error")
  end try
End Sub