Reveal file in Explorer

  1. ‹ Older
  2. 4 years ago

    Christoph D

    27 Sep 2014 Pre-Release Testers, Xojo Pro

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

  3. Michel B

    27 Sep 2014 Pre-Release Testers RubberViews.com
    Edited 4 years ago

    @Christoph Dnbsp;Vocht I do not have any experience writing declares for Windows.

    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.

  4. Christoph D

    27 Sep 2014 Pre-Release Testers, Xojo Pro

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

  5. Christian S

    27 Sep 2014 Pre-Release Testers, Xojo Pro, XDC Speakers Germany

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

  6. Michel B

    27 Sep 2014 Pre-Release Testers RubberViews.com
    Edited 4 years ago

    @Christoph Dnbsp;Vocht Don't think you need chr(34)
    .shellpath adds escaping chars when needed, if I am not mistaken.

    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].

    @ChristianSchmitz thanks for the api pointer, I think I can make a plugin function to do this. :-)

    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.

  7. Christian S

    27 Sep 2014 Pre-Release Testers, Xojo Pro, XDC Speakers Germany

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

  8. Michel B

    27 Sep 2014 Pre-Release Testers RubberViews.com

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

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

  9. Christian S

    27 Sep 2014 Pre-Release Testers, Xojo Pro, XDC Speakers Germany

    and added for next prerelease: WinOpenFolderAndSelectItemsMBS

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

  10. Norman P

    27 Sep 2014 Xojo Inc North of 50

    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
  11. Michel B

    27 Sep 2014 Pre-Release Testers RubberViews.com

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

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

  12. Christoph D

    27 Sep 2014 Pre-Release Testers, Xojo Pro

    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.

  13. Michel B

    28 Sep 2014 Pre-Release Testers RubberViews.com

    @Christoph Dnbsp;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.

    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.

  14. Christoph D

    28 Sep 2014 Pre-Release Testers, Xojo Pro

    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. :-)
    As I said, a hell to use. :-)

  15. Michel B

    28 Sep 2014 Pre-Release Testers RubberViews.com

    @Christoph Dnbsp;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. :-)
    As I said, a hell to use. :-)

    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.

  16. Lee B

    1 Oct 2014 Pre-Release Testers, Xojo Pro

    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).

  17. 3 months ago

    Emile S

    Jul 12 Europe (France, Strasbourg)

    The code below simply opens the owning folder, it does not select the passed item, as I suspected:

     Dim f As FolderItem
      
      f = GetFolderItem("Network Interfaces.app") // Beside the running project
      
      showurl f.parent.urlpath
  18. Christian S

    Jul 12 Pre-Release Testers, Xojo Pro, XDC Speakers Germany

    Or use NSWorkspaceMBS .selectFile(file as folderitem) as boolean in MBS Plugin for Mac:

    dim f as FolderItem
    
    f=SpecialFolder.Desktop.Child("test.txt")
    
    if NSWorkspaceMBS.selectFile(f) then
      MsgBox "Ok"
    else
      MsgBox "failed"
    end if
  19. Christian S

    Jul 12 Pre-Release Testers, Xojo Pro, XDC Speakers Germany

    for Windows, you can use WindowsShellExecuteMBS :

    dim e as Integer
    dim f as FolderItem
    
    // show documents folder
    
    f = SpecialFolder.Documents
    e = WindowsShellExecuteMBS(0, "explore", f.AbsolutePath, "", "", 10)
  20. I prefer my Mac

    Dim f As FolderItem
    f= SpecialFolder.Pictures // or other folder
    If f <> Nil Then
    f.Launch
    End If

    Harry

  21. Emile S

    Jul 12 Europe (France, Strasbourg)

    @Rudolf J I prefer my Mac

    but it does not highlight the file as requested by the op. :(

or Sign Up to reply!