Reveal file in Explorer

Is there a way to reveal a file in Explorer.

For OSX you can do this:

dim a as AppleEvent = New AppleEvent("misc", "slct", "com.apple.finder") a.FolderItemParam("----") = f If a.send then a = New AppleEvent("misc", "actv", "com.apple.finder") If a.send then end if End if

How about doing this for Windows?

Open the containing folder with launch ?

dim f as folderitem = specialfolder.UserHome f.launch

Tried this already. If the file is a movie or picture it launches the media player or image preview.
I only wants it to open the Explorer and selects the file.

 explorer.exe /select,"C:\\Folder\\subfolder\\file.txt"

or write a declare to use SHOpenFolderAndSelectItems

That’s great. Works perfectly. Thank you Shao Sean :slight_smile:

I do not have any experience writing declares for Windows.
Any help for this case?

The shell command Shao Sean showed works perfectly.

dim f as folderitem = getfolderitem("C:\\Folder\\subfolder\\file.txt") dim s as new shell s.execute("explorer.exe /select, "+chr(34)+f.shellpath+chr(34))

I added the chr(34) in case the path contained spaces.

Now if you absolutely want to use declares there is no example in WFS, but you can see
http://www.pinvoke.net/default.aspx/shell32/SHOpenFolderAndSelectItems.html

This gives a declare for VB, but it should not be too difficult to translate to Xojo.

Don’t think you need chr(34)
.shellpath adds escaping chars when needed, if I am not mistaken.

thanks for the api pointer, I think I can make a plugin function to do this. :slight_smile:

[quote=132109:@Christoph De Vocht]Don’t think you need chr(34)
.shellpath adds escaping chars when needed, if I am not mistaken.[/quote]

Just tried. It is inconclusive. No quotes added, but yet folders created with spaces are replaced by DOS names. So quotes appear not to be necessary, and it works without, but with as well. The LR does not say the long path is escaped, which would mean with quotes. From what I can see, NativePath does not place the quotes for you, unlike the Linux or Mac OS ShellPath escaped string.

On Windows, ShellPath returns the short path, if the item exists. Otherwise, ShellPath returns the long path as returned by AbsolutePath. For discussion of short v. long paths, see [1].

Should be fairly easy for you, and useful.

Among other ideas, I am just in the process of completing a Windows app, and I could not find in your Windows plugin the equivalent of NSWorkspaceMBS.iconForFile(f) to get the icon of Windows documents.

We still have folderitem.IconMBS to get icon on Mac and Windows.

My bad. I did not see it. Thank you.

and added for next prerelease: WinOpenFolderAndSelectItemsMBS

you can email me if you want a copy in advance.

Or use this
It has some limitations

Sub ShowFileOnDisk(extends f as FolderItem, activate as boolean = true)

  #if TargetMacOS then
    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
    dim workspace as ptr = sharedWorkspace( objc_getClass( "NSWorkspace" ) )
    assert ( workspace <> nil, CurrentMethodName + " is Nil")
    call selectFile( workspace, f.PosixPath, "")
    
  #elseif TargetWin32 then
    // ***** This is a cheap way of doing things, which has its problems *****
    //  1. Can only select one file
    //  2. If folder is already opened, it doesn't select the file
    Declare Function ShellExecuteW lib "shell32" (hwnd as Integer, lpOperation as WString, lpFile as WString, lpParameters as WString, lpDirectory as Integer, nShowCmnd as Integer) as Integer
    
    Dim err as Integer
    Dim param As String
    
    param = "/select, """ + f.AbsolutePath + """"
    
    err = ShellExecuteW(Window(0).WinHWND, "Open", "explorer", param, 0, 1)
  #elseif 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 + " " + f.Parent.ShellPath )
        Exit
      End If
    Next
  #endif
End Sub

Just tried it. Works beautifully with documents as well as folders, including customized. Congratulations, Christian.

About escaping chars:

It is pretty easy for OSX and Linux. But fro Windows it is a hell to use. Sometimes you need quotes without escaping, sometimes quotes with escaping, sometimes escaping without quotes.
There is no consistency at all.

For porting my app to Windows I got really frustrated with this. I really had to use the trail and error approach.

[quote=132204:@Christoph De Vocht]About escaping chars:

It is pretty easy for OSX and Linux. But fro Windows it is a hell to use. Sometimes you need quotes without escaping, sometimes quotes with escaping, sometimes escaping without quotes.
There is no consistency at all.

For porting my app to Windows I got really frustrated with this. I really had to use the trail and error approach.[/quote]

As a matter of course, I always start by experimenting in the Command Prompt (equivalent of Terminal) utility, until I am sure it works, then I do exactly the same in Mac.

But there is an option to get rid of quotes, and that is to transform the string with space into shellPath I placed into another thread already :

dim f as folderitem = GetFolderItem(<stringwithspaces>) Dim thePath as string = f.shellpath

thePath now contains the Windows equivalent of a Mac escaped path, no need for quotes. In general, this means you can use pretty much the same command line.

I can give you examples that it works fine in the Command Prompt and doing the same via shell class it does not work because I had to escape the chars or use quotes differently. :slight_smile:
As I said, a hell to use. :slight_smile:

[quote=132215:@Christoph De Vocht]I can give you examples that it works fine in the Command Prompt and doing the same via shell class it does not work because I had to escape the chars or use quotes differently. :slight_smile:
As I said, a hell to use. :-)[/quote]

Now you got a way to construct your argument line the same way as on a Mac.

The first rule, one way or the other, which means going from Mac to Windows or the opposite, it to leave preconceptions at the door. When you are accustomed, grown into a particular system, it comes naturally and it feels right and logical. Furthermore, the widespread use of declares on one of the platform renders even more difficult the transition to the other.

Using a different platform is not so much hell, as it does require opening to the plain and simple fact that it is like a foreign language. If you keep trying to refer to your native tongue and to translate, or resist because grammar or vocabulary does not feel right, you will fail. If you simply try to speak, with all the stumbling involved, you will soon think in that new language, and it will become natural.

You got to accept the mere fact that what you know on Mac cannot necessarily be applied as is on Windows. And dump the idea that one is better than the other. At least in a business sense, if you are serious about selling to Windows users. What you are going through is a learning period, that’s it. Everything has a learning curve, even if Xojo makes it one thousand times easier than it would be without.

Then do not forget some people here are “bilingual” like me, or some mostly Windows, and willing to help the best they can. Both environment can do the same thing if you approach it in good faith. It is just a question of adaptation. Do not get discouraged or antagonized. You will be surprised.

As a matter of fact, a while ago, I was attacked rather fiercely for writing that Windows could emulate the notion of bundle, where the system opens a folder like a file and hands that to an app. I did not know it at the time, but Windows has a bundle class for the web. And I have implemented the behavior for desktop, where a given app is launched with OpenDoc when a folder containing the right /Contents/PkgInfo information or a particular extension is opened.

Fact is, an open mind does wonder.

I’ve always used showurl f.parent.urlpath for mac and PC

The only time it doesn’t work is if the (windows!) security system is set up so that url’s cannot be opened (even local ones).